Filter Query Without Score in Elastic Search - elasticsearch

I need to modify this query so that elastic search does not give it a score. I want my custom filter score to be the only thing giving any result a score. How do I accomplish this?
Each record should only every have a score of 0, 100, or 1000.
{
"size":50,
"from":0,
"query" : {
"custom_filters_score" : {
"query" : {
"filtered" : {
"query" : {
"bool" : {
"must" : [
{"term":{"type":"alpha"}},
{"field":{"sector":"exists"}},
{"field":{"sector.sub":"exists"}},
{"field":{"alpha_sector.sub.categories":"second"}},
{"field":{"beta_sector.sub.columns":"first"}},
{"term":{"beta_type":"beta"}},
{"term":{"area":"624"}}
]
}
},
"filter" : {
"or" : [
{
"and" : [
{"term":{"area":"624"}},
{"term":{"start":"07242013"}}
]
},
{
"and" : [
{"term":{"area":"624"}},
{"term":{"start":"blank"}}
]
}
]
}
}
},
"filters" : [
{"filter":{"term":{"resource":5726}}, "boost":"1000"},
{"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
],
"score_mode":"sum"
}
}
}

I am not quite sure what you are trying to achieve here
{"field":{"sector":"exists"}},
{"field":{"sector.sub":"exists"}},
but in general, if you don't want part of your query to affect the score, just make it a filter. It's also will be better to use "bool" with "term" filters instead of "and"/"or"/"not"
{
"size":50,
"from":0,
"query" : {
"custom_filters_score" : {
"query" : {
"filtered" : {
"query" : {
"match_all": {}
},
"filter" : {
"bool" : {
"must" : [
{"term":{"type":"alpha"}},
{"query":{"field":{"sector":"exists"}}},
{"query":{"field":{"sector.sub":"exists"}}},
{"query":{"field":{"alpha_sector.sub.categories":"second"}}},
{"query":{"field":{"beta_sector.sub.columns":"first"}}},
{"term":{"beta_type":"beta"}},
{"term":{"area":"624"}}
],
"should" : [
{
"bool" : {
"must" : [
{"term":{"area":"624"}},
{"term":{"start":"07242013"}}
]
}
},
{
"bool" : {
"must": [
{"term":{"area":"624"}},
{"term":{"start":"blank"}}
]
}
}
]
}
}
}
},
"filters" : [
{"filter":{"term":{"resource":5726}}, "boost":"1000"},
{"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
],
"score_mode":"total"
}
}
}

Related

ElasticSearch : constant_score query vs function_score query

I recently upgraded my ElasticSearch version from version 5.3 to version 5.6
"query" : {
"constant_score" : {
"query" : {
"bool" : {
"must" : {
"terms" : {
"customerId" : [ "ASERFE", "7004567457" ]
}
},
"must_not" : {
"terms" : {
"useCase" : [ "PAY", "COLLECT" ]
}
}
},
"bool" : {
"must" : {
"match" : {
"cardProductGroupName" : {
"query" : "Pre-fill Test birthday Present",
"type" : "phrase"
}
}
}
}
}
}
}
executing the query mentioned above gave me the following error -
{"root_cause":[{"type":"parsing_exception","reason":"[constant_score] query does not support [query]","line":1,"col":37}],"type":"parsing_exception","reason":"[constant_score] query does not support [query]","line":1,"col":37}
So, I searched for the solution and found this function_score query. On executing the query mentioned below I am getting the same results that I would have got with constant_score.
"query" : {
"function_score" : {
"query" : {
"bool" : {
"must" : {
"terms" : {
"customerId" : [ "ASERFE", "7004567457" ]
}
},
"must_not" : {
"terms" : {
"useCase" : [ "PAY", "COLLECT" ]
}
}
},
"bool" : {
"must" : {
"match" : {
"groupName" : {
"query" : "Pre-fill Test birthday Present",
"type" : "phrase"
}
}
}
}
},
"functions" : [ {
"script_score" : {
"script" : "1"
}
} ],
"boost_mode" : "replace"
}
}
so my question is, Does it implies that function_score with script : "1" would give same result as constant_function ?
It will give the same result indeed though performance might be worse if it will still run the "script" for each matching document.
On the other hand, constant_score still exists in 5.6 though you have to use filter+boost instead of query.

How to combine "must" and "should" in Elasticsearch query?

I need to "translate" this pseudo-SQL query in Elasticsearch query DSL:
select from invoice where invoiceType = 'REGULAR' and receiver =
'CUSTOMER' and (invoiceStatus = 'DISPATCHED' or invoiceStatus = 'PAYED')
I have this:
{
"query": {
"bool": {
"must": [
{ "match": { "invoiceType": "REGULAR" }},
{ "match": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should": [
{"match" : {"invoiceStatus": "DISPATCHED"}},
{"match" : {"invoiceStatus": "PAYED"}}
]
}
}
]
}
}
}
That query is returning 0 results, but I know there are many that matches what I'm searching for. AFAIK, must would be like 'AND' and should like 'OR'. What am I missing?
Not sure that it will work for you or not but you can make a try and see what you get? Though I did some change with match to term. Hope this will help you.
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"terms": {"invoiceStatus": ["DISPATCHED","PAYED"]}}
]
}}
]
}
}
}
}
}
OR
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"term": {"invoiceStatus": "DISPATCHED"}},
{"term": {"invoiceStatus": "PAYED"}}
]
}}
]
}
}
}
}
}

OR & AND Operators in Elastic search

Hi I want to achieve this in Elasticsearch.
select * from products where brandName = Accu or brandName = Perfor AND cat=lube(any where in any filed of an elastic search ).
I am using this query in Elasticsearch.
{
"bool": {
"must": {
"query_string": {
"query": "oil"
}
},
"should": [
{
"term": {
"BrandName": "Accu"
}
},
{
"term": {
"BrandName": "Perfor"
}
}
]
}
}
By this query m not getting the combination exact results.
You need to add minimum_should_match: 1 to your query and probably use match instead of term if your BrandName field is an analyzed string.
{
"bool" : {
"must" : {
"query_string" : {
"query" : "oil OR lube OR lubricate"
}
},
"minimum_should_match": 1, <---- add this
"should" : [ {
"match" : {
"BrandName" : "Accu"
}
}, {
"match" : {
"BrandName" : "Perfor"
}
} ]
}
}
This query satisfy your condition.
{
"bool" : {
"must" : { "term" : { "cat" : "lube" } },
"should" : [
{ "term" : { "BrandName" : "Accu" } },
{ "term" : { "BrandName" : "Perfor" } }
],
"minimum_should_match" : 1
}
}

Elasticsearch match_phrase doesn't perform the same as multi_match with type phrase?

I'm having some trouble turning a match_phrase query into a multi_match query for multiple fields. My original query:
{
"from" : 0,
"size" : 50,
"query" : {
"filtered" : {
"query" : {
"match_phrase" : {
"metadata.description" : "Search Terms"
}
},
"filter" : {
"bool" : {
"must" : [ {
"terms" : {
"collectionId" : [ "1", "2" ]
}
} ]
}
}
}
}
}
Returns results correctly, but when I rewrite the match_phrase piece as a multi_match to run against multiple fields:
{
"from" : 0,
"size" : 50,
"query" : {
"filtered" : {
"query" : {
"multi_match" : {
"query" : "Search Terms",
"fields" : [ "metadata.description", "metadata.title" ],
"type" : "phrase"
}
},
"filter" : {
"bool" : {
"must" : [ {
"terms" : {
"collectionId" : [ "1", "2" ]
}
} ]
}
}
}
}
}
I am not getting any results. Is there anything obvious I am doing wrong here?
EDIT:
It must be something to do with the filter, as
{
"from" : 0,
"size" : 50,
"query" : {
"match_phrase" : {
"metadata.description" : "Search Terms"
}
}
}
and
{
"from" : 0,
"size" : 50,
"query" : {
"multi_match" : {
"query" : "Search Terms",
"fields" : [ "metadata.description", "metadata.title" ],
"type" : "phrase"
}
}
}
both perform as expected.
I am not sure why, exactly, but not using a filtered query, and applying the filter at the top level
{
"from" : 0,
"size" : 50,
"query" : {
"multi_match" : {
"query" : "Search Terms",
"fields" : [ "metadata.description", "metadata.title" ],
"type" : "phrase"
}
},
"filter" : {
"bool" : {
"must" : [ {
"terms" : {
"collectionId" : [ "1", "2" ]
}
} ]
}
}
}
resolves the problem.

Field Priority on full text search

I have a situation in which my products are described in a few and the following structure is used:
{
"defaultDescription" : "Default Description",
"i18nDescription" : {
"pt" : "Descrição Padrão",
"de" : "Standard-Beschreibung"
}
}
Now I have the following requirement: perform a search following a list of prioritized languages (3 languages). If the first language isn't in the i18nDescription, use just the second language, if the second language isn't there use just the third one otherwise match against defaultDescription.
My solution would be something like:
// suppose request comes with the following languages: en, de, pt
{
"size":10,
"fields" : ["defaultDescription", "i18nDescription.en^50", "i18nDescription.de^20", "i18nDescription.pt^10"],
"query": {
"multi_match" : { "query" : "default", "fields" : ["description","descriptions.fr-CA"] }
}
}
But this solution will just sort the result by priority language, I would like to do something like: i18nDescription.en:search OR (i18nDescription.de:search AND _empty_:i18nDescription.en) OR (i18nDescription.pt:search AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de) OR (description:search AND _empty_:i18nDescription.pt AND _empty_:i18nDescription.en AND _empty_:i18nDescription.de)
Is there a way to represent this is a ElasticSearch query?
Playing a bit with bool queries we could reach the desired effect.
It basically needs to check if one field has the text and others (that are more important) are empty, so it would consider just the most important present field.
Query would be something similar to:
{
"size":10,
"query": {
"bool" : {
"should" : [
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["defaultDescription"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de +_missing_:i18nDescription.pt" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.pt"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en +_missing_:i18nDescription.de" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.de"], "query" : "default" } },
{ "query_string" : { "query" : "+_missing_:i18nDescription.en" } }
]
}
},
{
"bool" : {
"must" : [
{ "multi_match" : { "fields":["i18nDescription.en"], "query" : "default" } }
]
}
}
]
}
}
}

Resources