布尔查询中的不同得分函数 [英] Different score functions in bool query
问题描述
我正在研究Elasticsearch的产品索引.我有一个包含多个匹配项的布尔查询,用于检索我想要的结果:
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.
推荐答案
您可以将每个 multi_match
查询包装在 function_score
查询中.然后,您可以轻松地为每个 multi_match
定义不同的功能.这就是查询的样子:
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
}
请注意,在上述查询中,我在两个 multi_match
查询中都使用了相同的得分逻辑.您可以根据需要对其进行修改.
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.
这篇关于布尔查询中的不同得分函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!