【问题标题】:elasticsearch boost query in feild having multiple value具有多个值的字段中的弹性搜索提升查询
【发布时间】:2017-06-06 08:00:24
【问题描述】:

我在弹性搜索索引中有一些文档。这是示例文档

文档1

{
"feild1":["hi","hello","goodmorning"]
"feild2":"some string"
"feild3":{}
}

文档2

{
"feild1":["hi","goodmorning"]
"feild2":"some string"
"feild3":{}
}

文档3

{
"feild1":["hi","hello"]
"feild2":"some string"
"feild3":{}
}

我想查询具有值“hi”和“hello”的 feild1,如果两者都存在,那么如果存在任何一个文档,则该文档应该首先出现,然后它应该在之后出现。 例如: 结果应按 DOC1、DOC3、DOC2 的顺序排列。我尝试了提升查询。但它不是按照我想要的顺序重新调整。这是我正在尝试的查询。

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                       "avail_status": true
                    }
                },
               {
                   "bool": {
                       "should": [
                          {
                               "constant_score": {
                                  "filter": {
                                  "terms": {
                                     "feild1": [
                                        "hi"
                                     ]
                                  }
                                  },
                                  "boost": 20
                               }
                           },
                           {
                               "constant_score": {
                                  "filter": {
                                  "terms": {
                                     "feild1": [
                                        "hello"
                                     ]
                                  }
                                  },
                                  "boost": 18
                               }
                           }
                       ]
                   }
               }
            ]
        }
    }
}

这是首先向我返回那些带有“hi”的文档,然后是那些带有“hello”的文档。提前致谢!

【问题讨论】:

  • 如果您可以检查当前查询,您可以看到 doc3 和 doc1 得分相等,因为在 custom_score 中包装过滤器忽略了 TDF/IDF。我不认为 ES 将 doc3 放在 doc1 之上。如果您正在应用一种模式,以便将具有更多字段的数组放置在具有较少字段的数组之上,同时提升匹配值,那么我建议开始查看函数分数。让我知道您是否适合这种情况
  • 是的,我只在找那个,文档的匹配分数很高
  • 我发布了功能分数

标签: elasticsearch solr-boost


【解决方案1】:

要为field1 较大的文档添加额外的提升,您可以输入funtion_score 脚本分数。

映射

{
  "mappings": {
    "document_type" : {
      "properties": {
        "field1" : {
          "type": "text",
          "fielddata": true
        },
        "field2" : {
          "type": "text"
        },
        "field3" : {
          "type": "text"
        }
      }
    }
  }
}

索引文档

POST custom_score_index1/document_type

{
"feild1":["hi","hello","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","hello"],
"feild2":"some string",
"feild3":{}
}

使用函数 score 的查询添加额外的 _score 以获得更大的 field1

POST custom_score_index1/document_type/_search
{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [{
                            "match_phrase": {
                                "avail_status": true
                            }
                        },
                        {
                            "bool": {
                                "should": [{
                                        "constant_score": {
                                            "filter": {
                                                "terms": {
                                                    "feild1": [
                                                        "hi"
                                                    ]
                                                }
                                            },
                                            "boost": 20
                                        }
                                    },
                                    {
                                        "constant_score": {
                                            "filter": {
                                                "terms": {
                                                    "feild1": [
                                                        "hello"
                                                    ]
                                                }
                                            },
                                            "boost": 18
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "functions": [{
                "script_score": {
                    "script": {
                        "inline": "_score + 10000 * doc['field1'].length"
                    }
                }
            }],
            "score_mode": "sum",
            "boost_mode": "sum"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多