I have this ES query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "test",
"fields": [
"name^-1.0",
"id^-1.0",
"address.city^-1.0",
"address.street^-1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1.0,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
and currently what happens is, when I search for person with the name john
, I will get bunch of results that the id, address.city, address.street
contains john in them, which is fine, but I want name
to be more important, and also if I have in the es 2 people john
and someone with 2 names like george john
I would want the just john
to come up first.
can I do that? :)
To make any field more important than other(s), you can set its boost to a higher value. So if fieldA^4
and fieldB^1
it implies that fieldA
is 4 times more important than fieldB
. Therefore you can give higher boost value to name
field to make it more important for scoring.
For second point the document with name
field value as john
will have higher score than with a document having name
field value as george john
(assuming that other fields have same data in both documents). The reason you are get the second doc (george john) higher in result is because you have boosted all the fields with negative value.
So to cater to both of your points
name
So the query should look as below:
{
//"explain": true,
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "john",
"fields": [
"name^4.0",
"id^1.0",
"address.city^1.0",
"address.street^1.0"
],
"type": "phrase_prefix",
"lenient": "true"
}
}
],
"boost": 1,
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
To understand more on how the score for the matching document is calculated by elastic, you can use the "explain": true
in your query. This will give detailed steps in result, taken by elastic to calculate the score.