Elasticsearch Exclude documents which does not match a criteria - elasticsearch

user_profile index has branches mapping with a propery country_iso
{
"_index": "user_profile",
"_type": "user",
"_id": "188",
"_score": 1.0042034,
"_source": {
"user_id": 188,
"phone": "971550000322",
"profile_set_date": "Apr 6, 2017",
"phone_country": "AE",
"branches": [
{
"id": 27,
"brand_id": 20,
"country_iso": "KW",
"city_iso": "KW-02-145162",
"area_id": 33
},
{
"id": 45,
"brand_id": 56,
"country_iso": "AE",
"city_iso": "AE-01-206944",
"area_id": 18
},
{
"id": 46,
"brand_id": 56,
"country_iso": "AE",
"city_iso": "AE-01-206944",
"area_id": 18
}
],
"prog_id": 13,
"email": "971550000322#email.com"
}
}
I need to find the users who has branches mapping with country_iso=AE and not with anything else
must and must_not combination did not work for me
{
"query": {
"bool": {
"must": [
{
"match": {
"prog_id": 13
}
}
],
"should": [
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.country_iso"
],
"query": "AE"
}
}
}
}
],
"minimum_number_should_match": 1,
"must_not": [
{
"bool": {
"must_not": [
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.country_iso"
],
"query": "AE"
}
}
}
}
]
}
}
]
}
}
}

You were close. This one worked for me (notice the negated text search in the must_not statement):
{
"query": {
"bool": {
"must": [
{
"match": {
"prog_id": 13
}
},
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.country_iso"
],
"query": "AE"
}
}
}
}
],
"must_not": [
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.country_iso"
],
"query": "-AE"
}
}
}
}
]
}
}
}

Related

script_score not works on ElasticSearch 7

I upgraded my ES from 2.1 to 7.10, when I tried to use the same query way to search, it always show
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "script_score: the script could not be loaded",
"index": "designs",
"index_uuid": "jqvLIrY2TIyJU2bdAsGgkQ"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "designs",
"node": "jkt6cdUBTKOJ_HTA83wgWQ",
"reason": {
"type": "query_shard_exception",
"reason": "script_score: the script could not be loaded",
"index": "designs",
"index_uuid": "jqvLIrY2TIyJU2bdAsGgkQ",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "script_lang not supported [native]"
}
}
}
]
},
"status": 400
}
Old query string:
{
"from": 0,
"size": 20,
"explain": false,
"_source": {
"include": [
"designId",
"memberId",
"isPersonalizable",
"sellerTags",
"products.productId",
"products.productTypeId",
"products.imageOneId",
"products.imageTwoId",
"products.hasPersonalizableSvg",
"products.storeId"
]
},
"sort": [
{
"_score": {}
},
{
"designId": {
"order": "desc"
}
}
],
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": {
"inline": "combined-conversion-scorer",
"lang": "native",
"params": {
"query": "peanuts",
"productTypeIds": [
128,
134,
755,
96,
113,
1230,
1231
]
}
}
}
}
],
"query": {
"bool": {
"must": [
{
"multi_match": {
"type": "most_fields",
"query": "peanuts",
"minimum_should_match": "3<75%",
"fields": [
"sellerTags",
"sellerTags.shingles",
"sellerTags.stemmed",
"editorialTags",
"editorialTags.shingles",
"editorialTags.stemmed"
]
}
}
],
"filter": [
{
"bool": {
"must": [
{
"nested": {
"query": {
"terms": {
"products.productTypeId": [
128,
134,
755,
96,
113,
1230,
1231
]
}
},
"path": "products"
}
},
{
"nested": {
"query": {
"term": {
"products.locations": {
"value": "US-1"
}
}
},
"path": "products"
}
}
],
"must_not": [
{
"multi_match": {
"query": "some query",
"fields": [
"sellerTags",
"editorialTags"
]
}
}
]
}
}
]
}
},
"boost_mode": "multiply"
}
}
}
New query string:
{
"explain": true,
"from": 0,
"query": {
"function_score": {
"boost_mode": "multiply",
"functions": [
{
"script_score": {
"script": {
"source": "combined-conversion-scorer",
"lang": "native",
"params": {
"query": "peanuts"
}
}
}
}
],
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"nested": {
"path": "products",
"query": {
"term": {
"products.locations": {
"value": "US-1"
}
}
}
}
}
],
"must_not": [
{
"multi_match": {
"fields": [
"sellerTags",
"editorialTags"
],
"query": "another query"
}
}
]
}
}
],
"must": [
{
"multi_match": {
"fields": [
"sellerTags",
"sellerTags.shingles",
"sellerTags.stemmed",
"editorialTags",
"editorialTags.shingles",
"editorialTags.stemmed"
],
"minimum_should_match": "3<75%",
"query": "peanuts",
"type": "most_fields"
}
}
]
}
}
}
},
"size": 200,
"sort": [
{
"_score": {}
},
{
"designId": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"designId",
"memberId",
"isPersonalizable",
"sellerTags",
"products.productId",
"products.productTypeId",
"products.imageOneId",
"products.imageTwoId",
"products.hasPersonalizableSvg",
"products.storeId"
]
}
}
Actually I'm not sure if combined-conversion-scorer is custom script or not, I can't find it on old version by GET _scripts/combined-conversion-scorer.
How can I update my script to let it works? Thank you
native scripts are actually compiled Java classes bundled as plugins.
So you need to look for a JAR in your plugins folder. If you have the source files then you're good otherwise you might have to decompile it. In the end, you'll need to recompile and rebuild the plugin for your new ES version... or simply reimplement it in Painless.

malformed query, expected [END_OBJECT] but found [FIELD_NAME]

The original query looks like this
{
"query": {
"bool": {
"must": [
{
"has_parent": {
"parent_type": "doc",
"query": {
"bool": {
"must": [
{
"terms": {
"id": [
713
]
}
},
{
"range": {
"created": {
"lte": "now/d"
}
}
},
{
"range": {
"expires": {
"gte": "now/d"
}
}
}
]
}
}
}
},
{
"term": {
"doc_type": "item"
}
},
{
"bool": {
"should": [
{
"term": {
"have_prices": true
}
},
{
"term": {
"is_folder": true
}
}
]
}
}
],
"must_not": {
"exists": {
"field": "folder"
}
}
}
},
"sort": [
{
"is_folder": {
"order": "desc"
}
},
{
"title_low.order": {
"order": "asc"
}
}
],
"size": 1000
}
And I got some result
"hits": {
"total": 19,
"max_score": null,
"hits": [
{
"_index": "prices",
"_type": "doc",
"_id": "item-6800004",
"_score": null,
"_routing": "1",
"_source": {
"id": 6800004,
"id_pricedoc": 713,
"title": "\"водка №1\" 1",
"title_low": "\"водка №1\" 1",
"supplier": {
"id": 7831,
"type": null
},
"supplier_nom": {
"id": 1375697,
"market_nom": {
"id": null
},
"codes": null,
"sup_code": "7a6713a5-73c1-3acb-9b62-9e38b2314dce",
"manufacturer": {
"id": null,
"title": null
}
},
"is_folder": false,
"folder": null,
"path": null,
"pricedoc_created": "2016-03-21",
"prices": [
{
"currency": "RUR",
"id_offer": 15735967,
"id_prcknd": 167,
"value": "391.50"
}
],
"have_prices": true,
"market": {
"title": null,
"uuid": null,
"folder": null,
"path": null
},
"_join_field_name": "doc_type",
"doc_type": {
"name": "item",
"parent": "doc-713"
}
},
"sort": [
0,
"\"водка №1\" 1"
]
}
Now I also would like get result where "id_prcknd": 167
Modified query looks like this
{
"query": {
"bool": {
"must": [
{
"has_parent": {
"parent_type": "doc",
"query": {
"bool": {
"must": [
{
"terms": {
"id": [
713
]
}
},
{
"range": {
"created": {
"lte": "now/d"
}
}
},
{
"range": {
"expires": {
"gte": "now/d"
}
}
}
]
}
}
}
},
{
"term": {
"doc_type": "item"
}
},
{
"bool": {
"should": [
{
"term": {
"have_prices": true
}
},
{
"term": {
"is_folder": true
}
}
]
}
}
],
"must_not": {
"exists": {
"field": "folder"
}
}
},
"nested": {
"path": "prices",
"query": {
"bool": {
"must": [
{
"match": {
"prices.id_prcknd": 167
}
}
]
}
}
},
"sort": [
{
"is_folder": {
"order": "desc"
}
},
{
"title_low.order": {
"order": "asc"
}
}
],
"size": 1000
}}
But I got an error Elasticsearch malformed query, expected [END_OBJECT] but found [FIELD_NAME]
Where am I wrong?
I wanna match objects where "id_prcknd": 167
The stackoverflow says the I post mostly the code, but it's because of large queries in elastic search.
What Elasticsearch was trying to say is that it did not expect to see other keys but bool in the dictionary (the value under "query").
In your example code you have something like this:
{
"query": {
"bool": {
"must": [
{...},
{...},
{...}
],
"must_not": {...},
"nested": {...}, // this should go under "must"
"sort": [...], // this should go on the same level as "query"
"size": 1000 // this should go on the same level as "query"
}
}
"bool" here refers to a bool query, and should be the only key in the dictionary.
What you should do is to move "nested" into its own dictionary and the fourth element of the must array (if I understood the logic of what you are trying to achieve correctly). Please note that also "sort" and "size" should be moved - this time, to the same level as "query".
The full query will look like this:
{
"query": {
"bool": {
"must": [
{
"has_parent": {
"parent_type": "doc",
"query": {
"bool": {
"must": [
{
"terms": {
"id": [
713
]
}
},
{
"range": {
"created": {
"lte": "now/d"
}
}
},
{
"range": {
"expires": {
"gte": "now/d"
}
}
}
]
}
}
}
},
{
"term": {
"doc_type": "item"
}
},
{
"bool": {
"should": [
{
"term": {
"have_prices": true
}
},
{
"term": {
"is_folder": true
}
}
]
}
},
{
"nested": {
"path": "prices",
"query": {
"bool": {
"must": [
{
"match": {
"prices.id_prcknd": 167
}
}
]
}
}
}
}
],
"must_not": {
"exists": {
"field": "folder"
}
}
}
},
"sort": [
{
"is_folder": {
"order": "desc"
}
},
{
"title_low.order": {
"order": "asc"
}
}
],
"size": 1000
}
Your json is incorrect: Error:Expecting closing } at end[Code 22, Structure 183]
Use a json validator (https://jsonformatter.curiousconcept.com/) for example.

function_score for query spanning multiple indices / filter not working as expected

I implemented function_score successfully for queries running against one index, but have trouble to apply function_score to a query spanning multiple indices with different fields in ElasticSearch 6.2.4.
This query:
POST /web_document,web_part/_search
{
"from": 0,
"size": 100,
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"multi_match": {
"type": "best_fields",
"query": "sampling probe",
"fields": [
"titleD.autocomplete^5",
"titleE.autocomplete^5"
]
}
}
],
"filter": [
{
"term": {
"_index": {
"value": "web_document"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"multi_match": {
"type": "best_fields",
"query": "sampling probe",
"fields": [
"textD.autocomplete^5",
"textE.autocomplete^5"
]
}
}
],
"filter": [
{
"term": {
"_index": {
"value": "web_part"
}
}
}
]
}
}
]
}
},
"functions": [
{
"filter": {
"bool": {
"must_not": [
{ "exists": { "field": "revDestDate" } },
{ "exists": { "field": "mutationDate" } }
]
}
},
"script_score": {
"script": "100"
}
},
{
"filter": {
"term": {
"_index": "web_part"
}
},
"gauss": {
"mutationDate": {
"origin": "now",
"scale": "90d",
"decay": 0.5
}
},
"weight": 20
},
{
"filter": {
"term": {
"_index": "web_document"
}
},
"gauss": {
"revDestDate": {
"origin": "now",
"scale": "90d",
"decay": 0.5
}
},
"weight": 20
}
],
"score_mode" : "first",
"boost_mode": "sum"
}
}
}
Returns the following error:
{
"error": {
…
"failed_shards": [
{
"index": "web_document",
…
"reason": {
"type": "parsing_exception",
"reason": "unknown field [mutationDate]",
…
}
},
{
"index": "web_part",
…
"reason": {
"type": "parsing_exception",
"reason": "unknown field [revDestDate]",
…
}
}
]
},
"status": 400
}
I also tried to incorporate the function_score in the induvidual query like this,
which returns the same error:
POST /web_document,web_part/_search
{
"from": 0,
"size": 100,
"query": {
"bool": {
"should": [
{
"function_score": {
"query": {
"bool": {
"must": [
{
"multi_match": {
"type": "best_fields",
"query": "sampling probe",
"fields": [
"titleD.autocomplete^5",
"titleE.autocomplete^5"
]
}
}
],
"filter": [
{
"term": {
"_index": {
"value": "web_document"
}
}
}
]
}
},
"functions": [
{
"gauss": {
"revDestDate": {
"origin": "now",
"scale": "90d",
"decay": 0.5
}
}
}
]
}
},
{
"function_score": {
"query": {
"bool": {
"must": [
{
"multi_match": {
"type": "best_fields",
"query": "sampling probe",
"fields": [
"textD.autocomplete^5",
"textE.autocomplete^5"
]
}
}
],
"filter": [
{
"term": {
"_index": {
"value": "web_part"
}
}
}
]
}
},
"functions": [
{
"gauss": {
"mutationDate": {
"origin": "now",
"scale": "90d",
"decay": 0.5
}
}
}
]
}
}
]
}
}
}
According to the comment from Darshan Pratil to this answer to the same problem this has worked in past versions.
How can this be done in elastic search 6.2.4?

What is wrong with my elasticsearch query ? Getting a expected end object error

I'm trying to do a elasticsearch query that does geolocation filter and does some matching on nested documents, but I'm getting this error whenever I add in the nested query.
"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"
{
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "10km",
"geolocation": [
-73.980090948125,
40.747844918436
]
}
},
"must": {
"multi_match": {
"query": "New York",
"fields": [
"name^2",
"city",
"state",
"zip"
],
"type": "best_fields"
}
}
},
"nested": {
"path": "amenities",
"query": {
"bool": {
"must": [
{
"match": {
"amenities.name": "Pool"
}
}
]
}
}
}
},
"aggs": {
"reviews": {
"nested": {
"path": "reviews"
},
"aggs": {
"avg_rating": {
"avg": {
"field": "reviews.rating"
}
}
}
}
}
}
You just has misplaced the nested query, try like this:
{
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "10km",
"geolocation": [
-73.980090948125,
40.747844918436
]
}
},
"must": [
{
"multi_match": {
"query": "New York",
"fields": [
"name^2",
"city",
"state",
"zip"
],
"type": "best_fields"
}
},
{
"nested": {
"path": "amenities",
"query": {
"match": {
"amenities.name": "Pool"
}
}
}
}
]
}
},
"aggs": {
"reviews": {
"nested": {
"path": "reviews"
},
"aggs": {
"avg_rating": {
"avg": {
"field": "reviews.rating"
}
}
}
}
}
}

Elastic Search Nested Query with Nested Object

This is the type of data I have stored on my index in elastic search.
I have to find Recipes with Main Ingredient Beef(and weight less than 1000) with Ingredients -(chilli powder and weight less than 250),(olive oil & weight less than 300 )and similarly for all other ingredients.
"Name": "Real beef burritos",
"Ingredients": [
{"name": "olive oil",
"id": 27,
"weight": 200},
{"name": "bonion","id": 3,"weight": 300},
{"name": "garlic",
"id": 2,
"weight": 100
},
{"name": "chilli powder",
"id": 35,
"weight": 150},
{"name": "coriander",
"id": 40,
"weight": 600},
{"name": "tortillas",
"id": 41,
"weight": 700}
],"Main_ingredient": {
"type": "Beef",
"id": 101,
"weight": 1000
}}}
Mapping of the index is
{"final":{"mappings":{"superb":{"properties":{"Cook Time":{"type":"long"},"Ingredients":{"type":"nested","properties":{"id":{"type":"short"},"name":{"type":"string"},"type":{"type":"string"},"weight":{"type":"short"}}},"Main_ingredient":{"properties":{"id":{"type":"long"},"type":{"type":"string"},"weight":{"type":"long"}}},"Name":{"type":"string"},"Prep Time":{"type":"long"},"Servings":{"type":"long"},"Tags":{"type":"string"},"Urls":{"type":"string"},"Views":{"type":"long"}}}}}}
My query is
{
"query": {
"bool": {
"must": [
{ "match": { "Main_ingredient.type": "Beef" }},
{"range":{"Main_ingredient.weight":{"lte":1000}}},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{ "match": { "Ingredients.name": "garlic" }},
{ "range": { "Ingredients.weight":{"lte":400} }},
{ "match": { "Ingredients.name": "chilli powder" }},
{ "range": { "Ingredients.weight":{"lte":400} }}
]
}}}}
]
}}}
But it gives Null.Can anyone help me out .I think I am not using nested query properly
Try this:
{
"query": {
"bool": {
"must": [
{
"match": {
"Main_ingredient.type": "Beef"
}
},
{
"range": {
"Main_ingredient.weight": {
"lte": 1000
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "garlic"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "chilli powder"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
}
]
}
}
}

Resources