问题
I am working in a products index on elasticsearch. I have a bool query with groups of multi-match for retrieving the results I want:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "term",
"fields": [
"name^3.0",
"name.fullName^10.0",
"description",
"description.fullDesc",
"detail",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "AND",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 10
}
},
{
"multi_match": {
"query": "term",
"fields": [
"name.fullName",
"description.fullDesc",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 6
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
I need to score each group of documents retrieved in multi-match query separately. I've considered using score function but it allows me to define one score function for the whole bool query like this:
{
"from": 0,
"size": 500,
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "term",
"fields": [
"name^3.0",
"name.fullName^10.0",
"description",
"description.fullDesc",
"detail",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "AND",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 10
}
},
{
"multi_match": {
"query": "term",
"fields": [
"name.fullName",
"description.fullDesc",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 6
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"boost": "5",
"field_value_factor": {
"field": "gain",
"factor": 1.2,
"modifier": "sqrt",
"missing": 1
},
"boost_mode": "multiply"
}
},
"version": true
}
I need to define different score functions (one for each multi-match query) to score results independently.
回答1:
You can wrap each of the multi_match
query in function_score
query. Then you can easily define the different functions for each multi_match
. This is how your query will look like:
{
"from": 0,
"size": 500,
"query": {
"bool": {
"should": [
{
"function_score": {
"query": {
"multi_match": {
"query": "term",
"fields": [
"name^3.0",
"name.fullName^10.0",
"description",
"description.fullDesc",
"detail",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "AND",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 10
}
},
"boost": "5",
"field_value_factor": {
"field": "gain",
"factor": 1.2,
"modifier": "sqrt",
"missing": 1
},
"boost_mode": "multiply"
}
},
{
"function_score": {
"query": {
"multi_match": {
"query": "term",
"fields": [
"name.fullName",
"description.fullDesc",
"detail.fullDetail"
],
"type": "cross_fields",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 6
}
},
"boost": "5",
"field_value_factor": {
"field": "gain",
"factor": 1.2,
"modifier": "sqrt",
"missing": 1
},
"boost_mode": "multiply"
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"version": true
}
Please note that in the above query I have used same score logic in both multi_match
queries. You can modify them as per your needs.
来源:https://stackoverflow.com/questions/56736999/different-score-functions-in-bool-query