I have the following query:
get all the documents that its field field1 contains a specific value, but I want to give more priority to the documents that its another field field2 contains a specific value.
I came up with the following two solutions:
First one (Function score query):
GET /myIndexName/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"field1": {
"value": "ValueHere"
}
}
}
]
}
},
"functions": [
{
"script_score": {
"script": "_score * (doc['field2'].value == 'AnotherValue' ? 2 : 1)"
}
}
]
}
}
}
Second query (query boosting):
GET myIndexName/doc/_search
{
"query": {
"bool": {
"must": [
{"match": {
"field1": {
"query": "ValueHere", "operator": "and"
}
}}
],
"should": [
{"match": {
"building": {
"query": "AnotherValue",
"boost": 2
}
}
}
]
}
}
}
Do you have any idea which one is better please ?