I hope someone can help, I'm getting the following error when I try to query Elasticsearch
No query registered for [query]
The query I'm using is:
{
"query": {
"bool": {
"must": {
"terms": {
"facet_1": [
"1",
"2"
]
},
"function_score": {
"query": {
"multi_match": {
"query": "fat dog food",
"fields": [
"family_name^20",
"parent_categories^2",
"description^0.2",
"product_suffixes^8",
"facet_values^10"
],
"operator": "and",
"type": "best_fields",
"tie_breaker": 0.3
}
},
"functions": [
{
"script_score": {
"script": "_score + ((_score * 0.3) + (log(1 + doc[\"popularity_score\"].value) * 2))"
}
}
],
"score_mode": "sum"
}
},
"must_not": {
"terms": {
"facet_1": [
"8"
]
}
}
}
},
"fields": [
"family_id",
"family_name",
"parent_categories",
"description",
"image",
"url",
"price_from",
"price_to",
"price_from_id",
"price_to_id",
"products_ids",
"popularity_score"
],
"from": 0,
"size": 48,
"sort": {
"_score": "desc"
}
}
I've tried loads of variations on this but cannot quite seem to get there. I'd really appreciate some help.
I've figured it out, to add extras to a must, must_not or should, they must be in an outer array like:
"query": {
"bool": {
"must": [
{
"terms": {
"facet_1": [
"1",
"2"
]
}
},
{
"function_score": {
"query": {
"multi_match": {
"query": "fat food",
"fields": [
"family_name^20",
"parent_categories^2",
"description^0.2",
"product_suffixes^8",
"facet_values^10"
],
"operator": "and",
"type": "best_fields",
"tie_breaker": 0.3
}
},
"functions": [
{
"script_score": {
"script": "_score + ((_score * 0.3) + (log(1 + doc[\"popularity_score\"].value) * 2))"
}
}
],
"score_mode": "sum"
}
}
]
}
},
"fields": [
"family_id",
"family_name",
"parent_categories",
"description",
"image",
"url",
"price_from",
"price_to",
"price_from_id",
"price_to_id",
"products_ids",
"popularity_score"
],
"from": 0,
"size": 48,
"sort": {
"_score": "desc"
}
}
Thanks for look anyway.
Related
I have the query in below format and it runs in ES 2.4
{"query":{"function_score":{"filter":{"bool":{"must":[{"exists":{"field":"x"}},{"query_string":{"query":"en","fields":["locale"]}},{"query_string":{"query":"US","fields":["channel"]}},{"query_string":{"query":"UG","fields":["usergroups"]}}]}},"query":{"bool":{"should":{"multi_match":{"query":"refund","fields":["doc","key","title","title.standard_analyzed^3","x"],"type":"phrase","slop":20}},"must":{"multi_match":{"fuzziness":"0","query":"refund","prefix_length":"6","fields":["doc","key","title","title.standard_analyzed^3","x"],"max_expansions":"30"}}}},"functions":[{"field_value_factor":{"field":"usage","factor":1,"modifier":"log2p","missing":1}}]}},"from":0,"size":21}
But when I try the same query in 6.8 it returns errors
{"error":{"root_cause":[{"type":"parsing_exception","reason":"no [query] registered for [function_score]",
If I put filters inside query, I get the response but the order of the docs don't match due to the difference in score
There should only be the "query" key below the function score. You have to add the filter in the bool query.
I don't know about your mapping but I would use the "Term" query instead of the query string.
{
"query": {
"function_score": {
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "x"
}
},
{
"query_string": {
"query": "en",
"fields": [
"locale"
]
}
},
{
"query_string": {
"query": "US",
"fields": [
"channel"
]
}
},
{
"query_string": {
"query": "UG",
"fields": [
"usergroups"
]
}
}
]
}
},
"should": {
"multi_match": {
"query": "refund",
"fields": [
"doc",
"key",
"title",
"title.standard_analyzed^3",
"x"
],
"type": "phrase",
"slop": 20
}
},
"must": {
"multi_match": {
"fuzziness": "0",
"query": "refund",
"prefix_length": "6",
"fields": [
"doc",
"key",
"title",
"title.standard_analyzed^3",
"x"
],
"max_expansions": "30"
}
}
}
},
"functions": [
{
"field_value_factor": {
"field": "usage",
"factor": 1,
"modifier": "log2p",
"missing": 1
}
}
]
}
},
"from": 0,
"size": 21
}
About FunctionScore (doc 6.8)
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.
I am pretty new to elasticsearch and have not really got the hold of it. So I have a search, the results of which will be weighed according to the weight of their tags, which works absolutely fine, but later when I introduced a filter, the search always gives me empty results. Here is what I have tried:
{
"nested": {
"path": "tags",
"score_mode": "sum",
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match_phrase_prefix": {
"tags.tag": "big"
}
}
],
"filter": {
"term": {
"type.primary": "audio"
}
}
}
},
"field_value_factor": {
"field": "tags.weight"
},
"boost_mode": "multiply",
"boost": 10
}
}
}
}
The example result with the filter should be something like this:
{
"_index": "assets",
"_type": "Asset",
"_id": "5a1dc3c0848662ee49e36f43s",
"_score": 886.8744,
"_source": {
"name": "And Action Breakbeat",
"meta_data": {
"type": "audio/mp3",
"file_name": "music_zapsplat_and_action_breakbeat.mp3"
},
"file_key": "music_zapsplat_and_action_breakbeat.mp3",
"src": {
"url": "https://exapmle.com/music_zapsplat_and_action_breakbeat.mp3"
},
"type": {
"primary": "AUDIO",
"secondary": "mp3"
},
"thumbnail_url": "https://example.com/thumbnail/audio.jpg",
"tags": [
{
"tag": "big",
"weight": 10
},
{
"tag": "beat",
"weight": 5
},
{
"tag": "music",
"weight": 3.3333333333333335
}
],
"isDeleted": false,
}
}
Thank you!
You cannot match type.primary inside a nested query for tags. Try this query instead:
{
"query": {
"bool": {
"filter": {
"term": {
"type.primary": "audio"
}
},
"must": [
{
"nested": {
"path": "tags",
"query": {
"function_score": {
"query": {
"match_phrase_prefix": {
"tags.tag": "big"
}
},
"field_value_factor": {
"field": "tags.weight"
},
"score_mode": "sum",
"boost_mode": "multiply",
"boost": 10
}
}
}
}
]
}
}
}
I am new to Elasticsearch and Nest so please pardon my ignorance. I would like to use filters along with multi_match query in Elasticsearch v2.3.5 but so far I am not able to figure it out. I think once I got it working for Elasticsearch I should be able to map it to Nest.
Below is my JSON data structure:
{
"contentID":1,
"categoryID":0,
"title":"...",
"description":"...",
"contentHtml":"...",
"version":2,
"parentContentID":0,
"displayOrder":0,
"freshdeskID":0,
"isDraft":false,
"isCommentingEnabled":false,
"isArticle":false,
"grandParentContentID":0,
"isAnyParentDraft":false
}
and below is my working search query (without any filters):
POST contents/supportitem/_search?pretty=true
{
"size": 150,
"highlight": {
"fields": {
"contentHtml": {
"fragment_size": 245
}
}
},
"_source": {
"include": [
"title",
"contentID",
"description",
"thumbnailUrl",
"isDraft",
"isAnyParentDraft",
"grandParentContentID"
]
},"query": {
"multi_match": {
"type": "cross_fields",
"query": "query typed by user",
"tie_breaker": 0.3,
"fields": [
"title^1.1",
"additionalContents^1.2",
"contentHtml^1"
]
}
}
}
I would like to show only those records in search result to user where:
grandParentContentID != 0 and
isDraft != false and
isAnyParentDraft != false
I have tried may different queries but I am not able to figure out how to write this.
Couple of NOT working queries, include:
POST contents/supportitem/_search?pretty=true
{
"size": 150,
"highlight": {
"fields": {
"contentHtml": {
"fragment_size": 245
}
}
},
"_source": {
"include": [
"title",
"contentID",
"description",
"thumbnailUrl",
"isDraft",
"isAnyParentDraft",
"grandParentContentID"
]
},"query": {
"multi_match": {
"type": "cross_fields",
"query": "Tile map server resources",
"tie_breaker": 0.3,
"fields": [
"title^1.1",
"additionalContents^1.2",
"contentHtml^1"
]
},"filtered": {
"filter": {
"bool": {
"term": {
"isAnyParentDraft": "false"
}
}
}
}
}
}
POST contents/supportitem/_search?pretty=true
{
"size": 150,
"highlight": {
"fields": {
"contentHtml": {
"fragment_size": 245
}
}
},
"_source": {
"include": [
"title",
"contentID",
"description",
"thumbnailUrl",
"isDraft",
"isAnyParentDraft",
"grandParentContentID"
]
},"query": {
"multi_match": {
"type": "cross_fields",
"query": "Tile map server resources",
"tie_breaker": 0.3,
"fields": [
"title^1.1",
"additionalContents^1.2",
"contentHtml^1"
]
},"filtered": {
"query": {
"bool": {
"must": [
{
"field": {"isAnyParentDraft": "false"}
}
]
}
}
}
}
}
I get "failed to parse search source. expected field name but got [START_OBJECT]"
I got this working but I am not able figure out how to add more filters:
POST contents/supportitem/_search?pretty=true
{
"size": 150,
"highlight": {
"fields": {
"contentHtml": {
"fragment_size": 245
}
}
},
"_source": {
"include": [
"title",
"contentID",
"description",
"thumbnailUrl",
"isDraft",
"isAnyParentDraft",
"grandParentContentID"
]
},
"query": {
"filtered": {
"query": {
"multi_match": {
"type": "cross_fields",
"query": "deleted",
"tie_breaker": 0.3,
"fields": [
"title^1.1",
"additionalContents^1.2",
"contentHtml^1"
]
}
},
"filter": {
"and": {
"filters": [
{
"term": {
"isAnyParentDraft": "false"
}
}
]
}
}
}
}
}
I have referred below questions:
ElasticSearch with multi_match AND bool - Has only one filter
Elasticsearch: multi_match no effect with filters
Very good start, you're almost there:
POST contents/supportitem/_search?pretty=true
{
"size": 150,
"highlight": {
"fields": {
"contentHtml": {
"fragment_size": 245
}
}
},
"_source": {
"include": [
"title",
"contentID",
"description",
"thumbnailUrl",
"isDraft",
"isAnyParentDraft",
"grandParentContentID"
]
},
"query": {
"bool": {
"must": {
"multi_match": {
"type": "cross_fields",
"query": "deleted",
"tie_breaker": 0.3,
"fields": [
"title^1.1",
"additionalContents^1.2",
"contentHtml^1"
]
}
},
"must_not": [
{
"term": {
"isAnyParentDraft": "false"
}
},
{
"term": {
"isDraft": "false"
}
},
{
"term": {
"grandParentContentID": 0
}
}
]
}
}
}
I have inherited an Elasticsearch query that I am trying to modify. The query I have at the moment is:
{
"fields": [
],
"from": 0,
"size": 51,
"query": {
"filtered": {
"query": {
"query_string": {
"fields": [
"data.*"
],
"default_operator": "AND",
"query": "*Search term*"
}
},
"filter": [
{
"terms": {
"type": [
"typeOne",
"typeTwo",
"typeThree"
]
}
}
]
}
}
}
Now what I have been trying to do is boost one of these terms over the other 2 in the results but have not been able to get it to work. I have tried adding a "boost" value but this has oddly given me the opposite effect - it disables any type that is given a boost.
I tried the following as the "filter" object:
"filter": [
{
"bool": {
"should": [
{
"term": {
"type": "typeOne"
}
},
{
"term": {
"type": "typeTwo"
}
},
{
"term": {
"type": "typeThree",
"boost": 2
}
}
]
}
}
]
But as I said before, instead of boosting "typeThree" it removes all "typeThree" from the results.
Can anyone help me boost a specific term type?
There are multiple ways to structure the query to achieve the above , one approach would be using function_score .It would look something on these lines
Example:
"query": {
"function_score": {
"functions": [
{
"filter": {
"term": {
"type": "typeThree"
}
},
"weight": 2
}
],
"score_mode": "sum",
"boost_mode": "sum",
"query": {
"filtered": {
"query": {
"query_string": {
"fields": [
"data.*"
],
"default_operator": "AND",
"query": "*search term*"
}
},
"filter": [
{
"terms": {
"type": [
"typeOne",
"typeTwo",
"typeThree"
]
}
}
]
}
}
}
}
You can enable explain to see how this affects the scoring
While keety's answer was 98% of the way there, it took a bit of extra googling to get it all together. The problem is that "weight" doesn't work here, instead you must use "boost_factor". The final query looks like this:
{
"fields": [
],
"from": 0,
"size": 51,
"query": {
"function_score": {
"functions": [
{
"filter": {
"term": {
"type": "typeOne"
}
},
"boost_factor": 1.2
},
{
"filter": {
"term": {
"type": "typeTwo"
}
},
"boost_factor": 1.1
},
{
"filter": {
"term": {
"type": "typeThree"
}
},
"boost_factor": 1
}
],
"score_mode": "sum",
"boost_mode": "sum",
"query": {
"filtered": {
"query": {
"query_string": {
"fields": [
"data.*"
],
"default_operator": "AND",
"query": "*search term*"
}
},
"filter": [
{
"terms": {
"type": [
"typeOne",
"typeTwo",
"typeThree"
]
}
}
]
}
}
}
}
}