Field Priority on full text search - elasticsearch

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" } }
]
}
}
]
}
}
}

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.

Query string embeded with bool query

HI iam using the following query with bool must
{
"query": {
"bool" : {
"must" : [ {
"match" : {
"orgid" : {
"query" : 13831,
"type" : "boolean"
}
}
}, {
"query_string" : {
"query" : "*07* AND *fres*"
}
} ]
}
}
}
It's hitting null even though there documents with org id as 13831 & in documents there is one data in all fields as 07 and fres.Is anything wrong in this query
Edit: A problem might be you're using a match query instead of a term query. Term queries are used for exact matches. The following will filter the result set to only the org_id you want, and it will then look for 07 and fres. Please give it a try.
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "*07* AND *fres*"
}
},
"filter": {
"term" : { "orgid" : 13831 }
}
}
}

multiple match must fields not working in elastic search

below query is fetching result if i give existing record that is fine , but if i change name field from 'John' to 'John1' then still record is fetching.
{
"query" : {
"bool" : {
"must" : [
{ "match" : {"employeeId" : "1234"}},
{ "match" : {"name" : "John"}}
]
}
}
}
I tried another alternative query as well but still giving result.which query is correct in terms of performance?but both are giving results if i change name record from 'John' to 'John1'
{
"filter": {
"bool" : {
"must" : {
"term" : {
"employeeId" : "1234"
}
}
}
},
"query": {
"match" : {
"name" : {
"query" : "John",
"type" : "phrase"
}
}
}
}
This because you are doing match, if you want do exact search you need to use filter
Notice we assuce the mapping of name column is analyzed
{
"query" :{
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"employeeId" : "1234"}},
{ "term" : {"name" : "john"}}
]
}
}
}
}
}

Elasticsearch: [filtered] query does not support [highlight]

I am new to Elasticsearch. I have a filtered query as follows
{
"query": {
"filtered" : {
"query" : {
"term" : {
"title" : "crime"
}
},
"highlight" : {
"fields" : {
"title" : {}
}
},
"filter" : {
"term" : { "year" : 1961 }
}
}
}
}
When I tried this query and got the error:
[filtered] query does not support [highlight]
Does filtered query support highlight? If not, how can I achieve highlight in query with filters? I have to use filters.
Thanks and regards!
The "highlight" parameter should go at the same level as the "query" parameter, not embedded within it. In your case it should look something like this:
{
"query": {
"filtered" : {
"query" : {
"term" : {
"title" : "crime"
}
},
"filter" : {
"term" : { "year" : 1961 }
}
}
},
"highlight" : {
"fields" : {
"title" : {}
}
}
}
Highlighting reference
Highlights problems with a filtered query

Filter Query Without Score in Elastic Search

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"
}
}
}

Resources