Get documents where id is 'ID1' OR 'ID2' - elasticsearch

I'm asking to ES for documents that id is one or another:
{
"query" : {
"bool" : {
"should" : [ {
"term" : {
"id" : "9a2646d4-e124-11e5-a976-382c4ab9e433"
}
}, {
"term" : {
"id" : "d7bf6f3b-27c0-11e6-b2ce-382c4ab9e433"
}
} ]
}
}
}
These documents with these id exist in ES index.
Shortly, I want to get whichever document that its id == 'x' OR id == 'y'.
What am I doint wrong?

Use ids query for that:
GET /my_index/my_type/_search
{
"query": {
"ids": {
"values": [
"9a2646d4-e124-11e5-a976-382c4ab9e433",
"d7bf6f3b-27c0-11e6-b2ce-382c4ab9e433"
]
}
}
}

Related

Elasticsearch - use a field match to boost only and not to fetch the document

I have a query phrase that needs to match in either of the fields - name, summary or description or the exact match on the name field.
Now, I have one more new field brand. Match in this field should be used only to boost results. Meaning if there is a match only in the brand field, the doc should not be in the result set.
To solve the without brand I have the below query:
query: {
bool: {
minimum_should_match: 1,
should: [
multi_match:{
query : "Cadbury chocklate milk",
fields : [name, summary, description]
},
term: {
name_keyword: {
value: "Cadbury chocklate milk"
}
}
]
}
}
This works fine for me.
How do I fetch the data using the same query but boost docs that have brand:cadbury, without increasing the recall set(match based on brand:cadbury).
Thanks!
Using a bool inside must should work for you.
multi_match has multiple types and for phrase you have to use type:phrase.
{
"query": {
"bool": {
"must": [
{ "bool" :
{ "should" : [ {
"multi_match" :{
"type" : "phrase",
"query" : "Cadbury chocklate milk",
"fields" : ["name", "summary", "description"]
} }, {
"term": {
"name_keyword": {
"value": "Cadbury chocklate milk"
} }
}
]
}
}
],
"should" : {
"term" : {
"brand" : {
"value" : "cadbury"
}
}
}
}
}

ElasticSearch Delete By Query - delete multiple values

Using the following ElasticSearch query to delete all documents with sourceId 1:
POST http://{{elasticip}}:9200/index2/index2_doc/_delete_by_query
{
"query": {
"match": {
"sourceId": 1
}
}
}
What is the proper syntax of the body if I wanted to delete from sourceId 1, 2, and 3 all at once?
Use bool terms filter:
{ "query" : { "bool" : { "filter" : { "terms" : { "sourceId" : [1,2,3] } } } } }
note: jaspreet advices are also corrects.

What's the functionality of "path" in ES_dsl.Q()?

I have a statement: ES_dsl.Q('nested', path='student', query=nest_filter)
What kind of role does the "path" play in the above one?
The path is simply the path to the nested field you're using in your query.
In nest_filter, you need to reference your nested field as student.xyz.
Check the equivalence in the query below:
GET /_search
{
"query": {
"nested" : {
"path" : "student", <--- this is the path
"query" : { <--- this is nest_filter
"bool" : {
{ "match" : {"student.name" : "john"} },
{ "range" : {"student.age" : {"gt" : 20}} }
]
}
}
}
}
}

Elasticsearch: can this complex filtere query be simplified?

I have the following filtered query:
{
"query": {
"filtered" : {
"query": {
....
},
"filter" : {
"bool" : {
"should" : [
{
bool : {
"must" : [
{ "term1" : { "name1" : "value1" } },
{ "term2" : { "name2" : "value2" } }
]
}
},
{
"bool" : {
"must" : [
......
]
}
},
{
"bool" : {
"must" : [
......
]
}
},
{
"bool" : {
"must" : [
.......
]
}
}
]
}
}
}
}
}
Is there any room for me to improve the complex filters without harming performance? How?
I feel the filter part can be simplified, but not sure. Also not sure about any performance impact.
Thanks!
UPDATE
Under each must clause, there is a GROUP of clauses. See the first must clause for an example.
UPDATE 2
According to Duc's input and Mario's updated answer, I am very happy that what I have so far is the right way. No changes are is needed.
I choose Mario's response as the answer because it confirms that mine is right.
You don't need Bool with only one must clause. You put your conditions directly within the outer should.
{
"query": {
"filtered" : {
"query": {
....
},
"filter" : {
"bool" : {
"should" : [
{
.......
},
{
.......
},
{
.......
},
{
.......
}
]
}
}
}
}
}
UPDATE: since I see from your update that you have several clauses within your nested must statements, my suggestion is no longer valid. Your query filter is ok like it is (except that term1 and term2 aren't valid, but that's not the point of the question)

Elastic Search NEST - How to have multiple levels of filters in search

I would like to have multiple levels of filters to derive a result set using NEST API in Elastic Search. Is it possible to query the results of another filter...? If yes can I do that in multiple levels?
My requirement is like a User is allowed to select / unselect options of various fields.
Example: There are totally 1000 documents in my index 'people'. There may be 3 ListBoxs, 1) City 2) Favourite Food 3) Favourite Colour. If user selects a city it filters out 600 documents. Out of those 600 documents I would like to filter Favourite food, which may result with some 300 documents. Now further I would like to filter with resp. to favourite movie to retrieve 50 documents out of previously derived 300 documents.
You don't need to query within filters to achieve what you want. Just use filtered queries, http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html, and provide several filters. In your instance I would assume you would do something like this for your first query:
{
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"and" : [
{
"term" : {
"city" : "some city"
}
}
]
}
}
}
You would then return the results from that and display them. You'd then let them select the next filter and do the following:
{
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"and" : [
{
"term" : {
"city" : "some city"
}
},
{
"term" : {
"food" : "some food"
}
}
]
}
}
}
You'd then rinse and repeat for the 3 filter param:
{
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"and" : [
{
"term" : {
"city" : "some city"
}
},
{
"term" : {
"food" : "some food"
}
},
{
"term" : {
"colour" : "some colour"
}
}
]
}
}
}
I haven't tested this, but the principle is sound and will work.

Resources