Hi all !
I'm trying to make a query with a custom weights for fields and custom result score.
Suppose we have 3 fields:
- field1 : Text,
- field2 : Text,
- phrase : Text.
Each field must have a different weight in the resulting score.
- field1 (exact match, weight 70) +
- field2 (exact match, weight 20) +
- phrase (partial match, weight in range 0-10)
I expect to get a final score of 100 with a complete match.
I made an implementation through a "function_score" and "weight" attribute,
but this approach does not work for field "phrase".
For this field I would like to get a relevance score in the range from 0 to 10.
How can I do that ?
Below are my data and query.
Will be glad to any comments.
Thanks.
=== MY DATA ===
POST /_bulk
{"create":{"_index":"test_score","_id":"1"}}
{"text_field1":"v1","text_field2":"v10","phrase":"The best of both worlds"}
{"create":{"_index":"test_score","_id":"2"}}
{"text_field1":"v2","text_field2":"v20","phrase":"The best thing since sliced bread"}
{"create":{"_index":"test_score","_id":"3"}}
{"text_field1":"v3","text_field2":"v30","phrase":"The best song in the world"}
=== MY QUERY ===
GET /test_score/_search
{
"query":
{
"function_score":
{
"query": {
"match_all": {}
},
"functions": [
{
"filter":
{
"term": {
"text_field1": "v1"
}
},
"weight": 70
},
{
"filter":
{
"term": {
"text_field2": "v10"
}
},
"weight": 20
},
{
"filter":
{
"match":{
"phrase": "world"
}
},
"weight": 10
}
],
"max_boost": 105,
"min_score": 0,
"score_mode": "sum",
"boost_mode": "replace"
}
}
}