gpt4 book ai didi

elasticsearch - bool 查询中的不同评分函数

转载 作者:行者123 更新时间:2023-12-02 22:21:50 27 4
gpt4 key购买 nike

我在 elasticsearch 上的产品索引中工作。我有一个带有多组匹配的 bool 查询,用于检索我想要的结果:

{
"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
}
}
}

我需要分别对多匹配查询中检索到的每组文档进行评分。我考虑过使用 score 函数,但它允许我为整个 bool 查询定义一个 score 函数,如下所示:
{
"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
}

我需要定义不同的评分函数(每个多匹配查询一个)来独立评分结果。

最佳答案

您可以包装每个 multi_match查询 function_score询问。然后您可以轻松地为每个 multi_match 定义不同的功能.这是您的查询的样子:

{
"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 中使用了相同的分数逻辑。查询。您可以根据需要修改它们。

关于elasticsearch - bool 查询中的不同评分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736999/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com