Elasticsearch - return only the source data part - javascript

I want to get only the json data from elasticsearch through node js.
I used
client.search({
index: 'dashboard',
type: 'test',
filterPath: 'hits.hits._source',
body: {
query: { match_all: {} }
}
})
It will show the result as:
{"_source":{"datatime":"2017-08-21 16:03:00","time_of_day":11},{"_source":{"datatime":"2017-08-21 16:03:00","time_of_day":222}]
May I know how to select only the data part in the source through node js, without "_source"? Only {"datatime":"2017-08-21 16:03:00","time_of_day":11},{"datatime":"2017-08-21 16:03:00","time_of_day":222}

Ok, solved.
can use hits.hits._source.theDataName, such as hits.hits._source.datatime , it will show "datatime":"2017-08-21 16:03:00"

Related

How can I limit the objects from a group in a query in Gatsby?

I have this query in my code which allows me to build a tag cloud for this blog front page
tagCloud:allContentfulBlogPost {
group(field: tags, limit: 8) {
fieldValue
}
}
It's passing data that I map in my component using {data.tagCloud.group.map(tag => (...))};. The code works nicely, but it won't be limited by the filter I'm passing above in the group(fields: tags, limit: 8) in my query. It renders all the tags and not only the first eight.
I've unsuccessfully tried the skip filter as well for the sake of seeing if it works.
Is this the proper way to limit the count to my mapping component in Gatsby?
The Contentful source plugin doesn't define arguments on any of the nodes it creates, unfortunately. Instead you would need to create these yourself. The easiest way to do that is through the createResolvers API.
Here's a similar example from a project of mine:
// in gatsby-node.js
exports.createResolvers = ({ createResolvers }) => {
createResolvers({
SourceArticleCollection: {
// Add articles from the selected section(s)
articles: {
type: ["SourceArticle"],
args: {
// here's where the `limit` argument is added
limit: {
type: "Int",
},
},
resolve: async (source, args, context, info) => {
// this function just needs to return the data for the field;
// in this case, I'm able to fetch a list of the top-level
// entries that match a particular condition, but in your case
// you might want to instead use the existing data in your
// `source` and just slice it in JS.
const articles = await context.nodeModel.runQuery({
query: {
filter: {
category: {
section: {
id: {
in: source.sections.map((s) => s._ref),
},
},
},
},
},
type: "SourceArticle",
})
return (articles || []).slice(0, args.limit || source.limit || 20)
},
},
},
})
}
Because resolvers run as part of the data-fetching routines that support the GraphQL API, this will run server-side at build-time and only the truncated/prepared data will be sent down to the client at request time.

Send object as get parameter with Axios

The API expects the following GET request:
/resource?labels[team1]=corona&labels[team2]=virus
My issue is to generate this URL using the Axios params option.
I tried the following params structures and they both generate the wrong url:
{
labels: {
team1: 'corona',
team2: 'virus'
}
}
{
labels: [
team1: 'corona',
team2: 'virus'
]
}
I at least thought it would work with the string indexed array but that generates no get params at all.
So, can anyone tell me how to generate the desired URL?
The solution was to use the paramsSerializer with the same setup as in the axios readme. And I used the first params object from my post above..
paramsSerializer: (params) => {
return qs.stringify(params, { arrayFormat: 'brackets' });
}

Modify collection value in Meteor

I want to write a query that would change the type of a project field from string to object.
So, if project field has the value abcd now, I want it to have an object like this:
{id: 'abcd'}
So:
project: 'abcd'
Turns to:
project: {id: 'abcd'}
I have no problems doing it in mongo:
db.hello.find({}).forEach((project) => {
project.project = {
id: x.project
}
db.hello.save(x)
})
But I don't know how to do it in Meteor. So far I have:
Projects.update($set: { client: ??? } }, { multi: true });
My 2 main problems are:
I don't know how to get the current value of client
I don't know how to change type
First of all, if you already ran the query, then you are aware that the db has already been adjusted yes? Because if you did run that, it would have updated all of the documents in that collection!
Please note that this should be ran server-side, I don't think that the $type is supported by all versions of minimongo.
// grab the cursor all string typed `project` fields
const cursor = Projects.find({ project: { $type : "string" } });
// grab the data from the cursor
const projects = cursor.fetch();
// Loop on each project and update
projects.forEach( project => Projects.update(project._id, {
$set: {
project: { id: project }
}
}) )

Using elasticsearch terminate after in query body in javascript

I am trying to get only one result back for a query I know may return more than one result, so I would like to use the the terminate_after parameter, but I can't work out how to use it in a query body in javascript. I have tried putting it in the body, like below, but I get a parsing_exception: "Unknown key for a VALUE_NUMBER in [terminateAfter]."
client.search({
index: 'myindex',
type: 'mytype',
body: {
terminateAfter: 1,
query: {
term: {
searchField: searchString
}
}
}
}, function(error, results){}
I have also tried putting it inside the query, but this also causes a parsing_exception: "[terminateAfter] query malformed, no start_object after query name"
I am using version 5.3 of elasticsearch and version 13.3.1 of the elasticsearch npm module.
You've almost got it right, terminateAfter should go outside the body, like this:
client.search({
index: 'myindex',
type: 'mytype',
terminateAfter: 1,
body: {
query: {
term: {
searchField: searchString
}
}
}
}, function(error, results){}

Magento filters SOAP v2 with Node JS / soap package

I am trying to use the filter for Magento SOAP v2 (Magento 1) but my code doesn't seem to work. I tried several ways of building to object with arrays but none of them seems to affect the returning results.
Can anybody explain me the right way to do that?
What I want to do is pull in all invoices but for example with a specific invoice ID or date.
Link to official Magento documentation:
http://devdocs.magento.com/guides/m1x/api/soap/sales/salesOrderInvoice/sales_order_invoice.list.html
This is my current code:
const filter = {
'complex_filter': [
{
key: 'invoice_id',
value: {
key: 'eq',
value: '94'
}
}
]
};
client.salesOrderInvoiceList(res, filter, function(error, result) {
console.log(result.result)
});
In the above example I only tried to use the filter for the invoice ID but I also tried with the date but that didn't work out either.
Thanks in advance.
For me, the easiest solution was to just map exactly to what the XML document would look like if this was done by PHP SoapClient.
const args = {
sessionId: session_id,
storeView: store_id,
filters: {
complex_filter: {
complexObjectArray: {
key: 'created_at',
value: {
key: 'from',
value: '2017-01-01'
}
}
}
}
};
client.catalogProductList(args, (err, result) => { ... }

Categories