Highlight not working with elasticsearch script_score query? - elasticsearch

I am trying to get query result along with highlight but highlights are not working script_score query in elasticsearch version 7.13. I am tryig following query
{
"_source": {"excludes": ["tag","document_vector"]},
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.query_vector, 'document_vector') + 1.0",
"params": {
"query_vector": query_vector
}
}
}
},
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"highlight": {
"order": "score",
"number_of_fragments": 3,
"fragment_size": 150,
"pre_tags": [
"<mark>"
],
"post_tags": [
"</mark>"
],
"fields": {
"content": {}
}
}

Related

ElasticSearch custom script score that does not replace the natural scoring

I have an ElasticSearch query with a custom script score written in Painless language. For now the ES request looks like this, with the natural ES _score being totally replaced by a custom scoring from my script:
{
"_source": {
"excludes": [
"field_to_exclude",
]
},
"from": 0,
"size": 100,
"query": {
"script_score": {
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"field_to_filter": 4
}
}
]
}
},
"script": {
"lang": "painless",
"source": "COMPLEX_PAINLESS_SCRIPT"
}
}
},
"sort": [
{
"price": {
"order": "asc"
}
},
"_score"
]
}
Depending on some parameter from the frontend, I want to be able to still calculate the ES natural scoring separately, and keep this custom scoring to be calculated in another field, and even if possible being used as a secondary sorting criteria.
Is this possible?
I finally figured out. This can actually be done if you use a custom _script field in the sort array, along with the _score field for the natural ES score.
And aside from that, we can keep the custom scoring in a specific field in the response by using script_fields.
Both the ES requests, depending on the sorting criteria, would look like:
{
"_source": {
"excludes": [
"field_to_exclude",
]
},
"from": 0,
"size": 100,
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"field_to_filter": 4
}
}
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_score": {
"order": "desc"
}
},
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "COMPLEX_PAINLESS_SCRIPT"
},
"order": "desc"
}
}
],
"script_fields": {
"custom_score": {
"script": {
"lang": "painless",
"source": "COMPLEX_PAINLESS_SCRIPT"
}
}
}
}
and
{
"_source": {
"excludes": [
"field_to_exclude",
]
},
"from": 0,
"size": 100,
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": [
{
"term": {
"field_to_filter": 4
}
}
]
}
},
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "COMPLEX_PAINLESS_SCRIPT"
},
"order": "desc"
}
},
{
"_score": {
"order": "desc"
}
}
],
"script_fields": {
"fit_score": {
"script": {
"lang": "painless",
"source": "COMPLEX_PAINLESS_SCRIPT"
}
}
}
}

Set filter in Kibana 7/6.7.2 for latest _id in DSL with size 1

I want the filter to be set on dashboard which will fetch only latest record
So Used below query in DSL
{
"query": {
"match_all": {}
},
"size": 1,
"sort": [
{
"_id": {
"order": "desc"
}
}
]
}
This is not working in Elasticsearch Kibana 7.0 and I have checked in 6.7.1 also , it is not working
"size" : 1 not working
The same this works when I post the request in developer tool like
POST index/_search
{
"query": {
"match_all": {}
},
"size": 1,
"sort": [
{
"_id": {
"order": "desc"
}
}
]
}
But it does not work in DSL query.
Ref : Get last document from ElasticSearch
Return the most recent record from ElasticSearch index
When I click on inspect and check the request I get below which does not have my size filter
{
"version": true,
"size": 500,
"sort": [
{
"timestamp": {
"order": "desc",
"unmapped_type": "boolean"
}
}
],
"_source": {
"excludes": []
},
"aggs": {
"2": {
"date_histogram": {
"field": "timestamp",
"interval": "12h",
"time_zone": "Asia/Calcutta",
"min_doc_count": 1
}
}
},
"stored_fields": [
"*"
],
"script_fields": {
"StartTime": {
"script": {
"source": "return doc['timestamp'].value",
"lang": "painless"
}
},
"ExecDurn": {
"script": {
"source": "try{\n\tif (doc['endTime'].size() != 0 && doc['timestamp'].size() != 0) {\n\t\tChronoUnit.MILLIS.between(doc['timestamp'].value.toInstant(),doc['endTime'].value.toInstant())\n\t}\n}\ncatch (Exception ignored) {\n}",
"lang": "painless"
}
},
"EndTime": {
"script": {
"source": "try{\n\tif (doc['endTime'].size() != 0) {\n\t\tdoc['endTime'].value\n\t}\n\t}\ncatch (Exception ignored) {\n}",
"lang": "painless"
}
}
},
"docvalue_fields": [
{
"field": "endTime",
"format": "date_time"
},
{
"field": "timestamp",
"format": "date_time"
}
],
"query": {
"bool": {
"must": [
{
"range": {
"timestamp": {
"format": "strict_date_optional_time",
"gte": "2019-04-06T09:51:57.824Z",
"lte": "2019-04-21T09:51:57.824Z"
}
}
}
],
"filter": [
{
"match_all": {}
}
],
"should": [],
"must_not": []
}
},
"highlight": {
"pre_tags": [
"#kibana-highlighted-field#"
],
"post_tags": [
"#/kibana-highlighted-field#"
],
"fields": {
"*": {}
},
"fragment_size": 2147483647
}
}
By default, the Discover view will show 500 records. You can modify that by going to Management > Advanced Settings and changing the "Number of rows" setting to 1.

Elasticsearch msearch query with no hits

I'm new to Elastic 5.1, (new to elastic in general) and I have a list which I send using msearch to elastic.
However the following does not return any hits, but my documents in the index look like:
{
"_index": "all_items",
"_type": "product",
"_id": "1000002007900",
"_version": 2,
"found": true,
"_source": {
"doc": {
"title": "title here",
"brand": null,
"updatedOn": "2016-12-22T14:00:26.016290",
"price": 49,
"viewed7": 0,
"idInShop": "11",
"active": true,
"model": null,
"_id": 1000002007900,
"purchased7": 0
},
"doc_as_upsert": true
}
}
and here is the body sent to msearch
[
{
"index": "all_items",
"type": "product"
},
{
"sort": [
{
"_score": "desc"
}
],
"query": {
"function_score": {
"query": {
"bool": {
"filter": [
{
"term": {
"active": true
}
}
],
"should": [],
"must_not": [],
"must": []
}
},
"functions": [
{
"script_score": {
"script": {
"lang": "painless",
"inline": "_score * params.constant * (doc['discountPrice'] > 0 ? doc['price'] / doc['discountPrice'] : 0)",
"params": {
"constant": 1.2
}
}
}
}
],
"score_mode": "multiply"
}
},
"from": 0,
"size": 3
}
]
If I only send {"query":{"match_all":{}}} I get hits.
You can use match query to get the result you want.
[
{
"index": "all_items",
"type": "product"
},
{
"sort": [
{
"_score": "desc"
}
],
"query": {
"function_score": {
"query": {
"match": {
"active": true
}
},
"functions": [
{
"script_score": {
"script": {
"lang": "painless",
"inline": "_score * params.constant * (doc['discountPrice'] > 0 ? doc['price'] / doc['discountPrice'] : 0)",
"params": {
"constant": 1.2
}
}
}
}
],
"score_mode": "multiply"
}
},
"from": 0,
"size": 3
}
]
You can read more about match query and term based query (which you used) at this link.

elastic search highlight not working with filter query

I can't get this to highlight for the life of me. It seems like elastic search is broken with regards to highlighting on fields that are mentioned in a filtered clause. The below query just does not highlight. Is this an issue with Elastic? What can I do to make this work?
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"term": {
"language": "Austrian"
}
}
]
}
},
"filter": {
"and": [
{
"query": {
"query_string": {
"fields": [
"standard_analyzed_name",
"standard_analyzed_message"
],
"query": "Arnold AND Schwarz"
}
}
}
]
}
}
},
"size": 20,
"sort": [
{
"total_interactions": {
"order": "desc"
}
}
],
"highlight": {
"fields": {
"standard_analyzed_name": {
"pre_tags": [
"<strong>"
],
"post_tags": [
"</strong>"
],
"fragment_size": 500,
"number_of_fragments": 1
},
"standard_analyzed_message": {
"pre_tags": [
"<strong>"
],
"post_tags": [
"</strong>"
],
"fragment_size": 500,
"number_of_fragments": 1
}
}
}
}
UPDATE
The above query does highlight in elastic search 2.0.0 - Yayyy
Well, simply put, you have your query and filter parts swapped. Your query_string should go inside the query part and the term should go inside the filter part, then all will be well and you'll see the highlighted fields.
{
"query": {
"filtered": {
"query": {
"query_string": {
"fields": [
"standard_analyzed_name",
"standard_analyzed_message"
],
"query": "arnold"
}
},
"filter": {
"term": {
"language": "austrian"
}
}
}
},
"size": 20,
"sort": [
{
"total_interactions": {
"order": "desc"
}
}
],
"highlight": {
"fields": {
"standard_analyzed_name": {
"pre_tags": [
"<strong>"
],
"post_tags": [
"</strong>"
],
"fragment_size": 500,
"number_of_fragments": 3
},
"standard_analyzed_message": {
"pre_tags": [
"<strong>"
],
"post_tags": [
"</strong>"
],
"fragment_size": 500,
"number_of_fragments": 3
}
}
}
}

Elasticsearch error - No query registered for [query]

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.

Resources