Adding a condition to query - elasticsearch

I have a query that works grate. How ever I need to add one condition.
I want to get a documents that also have the field "marked" :"true"
This is my query.
{
"from": 0,
"size": 100,
"min_score": 0.6,
"query": {
"bool": {
"should": [
{ "multi_match" : {
"fields" : ["_all"],
"query" : " Test " ,
"fuzziness" : "1.5" ,
"prefix_length" : "2"
}
}
],
"must": { "bool": { "must": [
{ "terms": { "language.id":["1"] }},
{ "term": { "forbidden":"false" }}
]
}}}
}, "sort": [{ "_score": { "order": "desc"}} ]
}
I have been trying ti add a should clause after both must clauses.
Where can I put this condition?

try this
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"fields": [
"_all"
],
"query": " Test ",
"fuzziness": "1.5",
"prefix_length": "2"
}
}
],
"must": [
{
"terms": {
"language.id": [
"1"
]
}
},
{
"term": {
"forbidden": "false"
}
},
{
"term": {
"marked": "true"
}
}
]
}
}
}

Related

Minimum should match with filter doesn't return any result

I have a complicated query which works fine.the proble is that I'm going to add a condition(filter) to it to filter the result.I need the exact result that I currently get with filtering based on the field called "field7".
"query": {
"bool": {
"should": [
{
"match_bool_prefix": {
"field1": {
"query": "test",
"fuzziness": "auto",
"boost": 1
}
}
},
{
"match": {
"field2": {
"query": "test",
"boost": 10
}
}
},
{
"exists": {
"field": "field3",
"boost": 15
}
},
{
"exists": {
"field": "field4",
"boost": 10
}
},
{
"match_phrase_prefix": {
"field5": {
"query": ""
}
}
}
],
"must": [
{
"bool": {
"filter": [
{
"match": {
"field6": "A"
}
},
{"terms": { "field7": [3,4,5]}}
]
}
}
],
"minimum_should_match": 3
}
},
"size": 20
I have to use "minimum_should_match": 3,to meet my requirements(If i remove it I get unrelated results) but when i use it with filter the result gets notthing.Is there any suggestion how to get current result and filter it based on field7?
#Paris I believe you can use filter term query for field7 since you want to apply filter on the result-set from should+must query. So basically this should suffice:
"query": {
"bool": {
"should": [
{
"match_bool_prefix": {
"field1": {
"query": "test",
"fuzziness": "auto",
"boost": 1
}
}
},
{
"match": {
"field2": {
"query": "test",
"boost": 10
}
}
},
{
"exists": {
"field": "field3",
"boost": 15
}
},
{
"exists": {
"field": "field4",
"boost": 10
}
},
{
"match_phrase_prefix": {
"field5": {
"query": ""
}
}
}
],
"must": {
{"match": {"field6": "A"}}
},
"filter": {
{"term" : {"field7" : 3}},
{"term" : {"field7" : 4}},
{"term" : {"field7" : 5}},
}
}
},
"size": 20

Has there been any change in the format of using function_score in ES 6.8?

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)

Elastic Search Relavance Scores are different for Direct Query and hasChild query

We have some data with parent child relationship. But as we search through two scenarios the scores are different.
For this case we have only one record and only one child record in the index.
a) Search with Direct Query on the child IndexType
Direct Query :
{
"from": 0,
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"query_string": {
"query": "search\\ text",
"fields": [
"TYPECHILD.MAINTITLE.edgengramanalyzed^4"
],
"default_operator": "and",
"boost": 10
}
}
]
}
}
]
}
}
}
}
}
We get the child record with some score.
b) Search with HasChild for the same record [has_child : score_mode-max].
HasChild Query :
{
"from": 0,
"size": 20,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"has_child": {
"type": "TYPE",
"score_mode": "max",
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"query_string": {
"query": "search\\ text",
"fields": [
"MAINTITLE.edgengramanalyzed^4"
],
"default_operator": "and",
"boost": 10
}
}
]
}
}
]
}
}
}
}
]
}
}
}
}
}
Here we get the parent record for the child but the score for the same is different.

ES Must match filter

I have this fairly simple es query and filter, using ES 2.3.5:
{
"query": {
"multi_match": {
"query": "image",
"fields": [
"ToRecipients"
"From",
"Subject"
]
}
},
"filter": {
"bool": {
"must": [
{
"match": {
"ToRecipients": "johndoe"
}
}
]
}
},
"sort": [
{
"DateTimeSent": {
"order": "desc"
}
}
]
}
For some reason it is not filtering by the ToRecipients field. The results coming back have all kinds of values for the field not just johndoe.
Where have I gone wrong?
Try this query instead:
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "image",
"fields": [
"ToRecipients",
"From",
"Subject"
]
}
}
],
"filter": {
"bool": {
"must": [
{
"match": {
"ToRecipients": "johndoe"
}
}
]
}
}
}
},
"sort": [
{
"DateTimeSent": {
"order": "desc"
}
}
]
}

random_score does not work properly with "should"

In the following code I always get "Alexander McQueen" products coming first, no matter what I set the seed to.
How can I change my search query to properly shuffle results?
{
"query": {
"function_score": {
"random_score": {
"seed": 99287
},
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"query_string": {
"query": "(adidas originals)",
"default_operator": "AND",
"fields": [
"name^4",
"description"
]
}
},
{
"terms": {
"category": [
"Fashion",
"Sports",
"Other",
""
]
}
},
{
"term": {
"currency": {
"term": "USD"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"query_string": {
"query": "(alexander mcqueen)",
"default_operator": "AND",
"fields": [
"name^4",
"description"
]
}
},
{
"terms": {
"category": [
"Fashion"
]
}
},
{
"term": {
"currency": {
"term": "USD"
}
}
}
]
}
}
]
}
}
}
},
"size": 40,
"from": 0
}
That's because the random score is being multiplied by the _score from the original query. If you want the results to be purely based on the random score, then set the boost_mode to replace (instead of the default multiply).
See the function_score documentation.

Resources