【问题标题】:Elasticsearch query to find number of matched terms in should queryElasticsearch 查询在应该查询中查找匹配项的数量
【发布时间】:2018-08-05 22:15:39
【问题描述】:

我正在寻求您的帮助来构建以下解决方案:

这是什么查询: 如果五个字段(如街道、城市、姓氏、国家、大陆)中的任何一个,则查询该关键字是否存在 所以我想检查那些有美国或英国的领域中的任何一个

我正在寻求什么帮助。

获得分数 2: 如果在这五个(“街道”、“城市”、“姓氏”、“国家”、“大陆”)字段中的任何一个中都找到了“美国”、“英国”这两个关键词

获得 1 分: 如果在这五个(“街道”、“城市”、“姓氏”、“国家”、“大陆”)字段中的任何一个字段中找到“美国”、“联合王国”中的任何一个

得到0分: 如果在这五个(“街道”、“城市”、“姓氏”、“国家”、“大陆”)字段中的任何一个字段中都找不到“美国”、“英国”这些关键词

这样我就可以在前端结果中显示 0/2 或 1/2 或 2/2

现有查询:

GET  main_index/_doc/_search
{
  "query": {
    "bool": { 
      "should": [
        { 
          "multi_match": {
            "fields": ["street","city","lastname","country","continent"],
            "query": "united states", 
            "type": "phrase"
          }
        },
        { 
           "multi_match": { 
             "fields": ["street","city","lastname","country","continent"],
             "query": "united kingdom",   
             "type": "phrase"
           }
        }]
     }
   }
}

【问题讨论】:

  • 这个答案应该会有所帮助:stackoverflow.com/questions/36843601/…(提示:使用命名查询以了解匹配的查询)
  • 太好了,非常感谢您的帮助。我按照这种方法获得了查询名称列表。稍后在程序中我必须处理计算部分。

标签: elasticsearch


【解决方案1】:

有两种方法可以解决这个问题。

(注意:我将下面的查询简化为对单个address字段进行查询,但原理保持不变)

1.命名查询 + 应用逻辑

正如 cmets 中提到的 Val,您可以使用命名查询(例如,将它们命名为 us-queryuk-query)来了解哪个项目与哪个查询匹配。

然后您可以让您的应用程序代码解析每个结果的 matched_queries 字段,以了解哪个查询匹配并在将结果返回到前端之前分配相关分数:

{
  "query": {
    "bool": { 
      "should": [
        { 
          "multi_match": {
            "fields": ["address"],
            "query": "united states", 
            "type": "phrase",
            "_name": "us-query"
          }
        },
        { 
           "multi_match": { 
             "fields": ["address"],
             "query": "united kingdom",   
             "type": "phrase",
             "_name": "uk-query"
           }
        }]
     }
   }
}

2。功能评分

您可以将script_score 类型的function scorefilter 参数结合使用,为每个查询分配正确的分数。 我个人不喜欢这种解决方案,因为它使用过于复杂的查询来获得相当简单的结果,并在查询中引入了一些重复。

{
  "query": {
        "function_score": {
            "query": { "match_all": {} },
            "functions": [
                {
                    "filter": {
                        "multi_match": {
                        "fields": ["address"],
                        "query": "united states", 
                        "type": "phrase",
                      }
                    },
                    "script_score" : {
                        "script" : {
                          "source": "1"
                        }
                    }
                },
                {
                    "filter": {
                        "multi_match": {
                        "fields": ["address"],
                        "query": "united kingdom", 
                        "type": "phrase",
                      }
                    },
                    "script_score" : {
                        "script" : {
                          "source": "1"
                        }
                    }
                },
                {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "multi_match": {
                                        "fields": ["address"],
                                        "query": "united kingdom", 
                                        "type": "phrase",
                                    }
                                },
                                {
                                    "multi_match": {
                                        "fields": ["address"],
                                        "query": "united states", 
                                        "type": "phrase",
                                    }
                                }
                            ]   
                        }
                    },
                    "script_score" : {
                        "script" : {
                          "source": "2"
                        }
                    }
                }
            ]
        }
    }
}

【讨论】:

  • 感谢您的帮助。昨天我通过 Val 链接,它工作并给了我一个列表,就像你在解决方案 1 中提到的一样。我也喜欢你的第二个解决方案,因为我会得到它作为直接分数。但是会得到总分,如果我在顶部有一个“必须”和两个单独的“应该”块,每个块都有两个检查美国,英国查询...示例:必须 { { 应该 { usquery1 , ukquery1}, {usquery2, ukquery2}} - 如果全部匹配,我需要 4 分,3 只有三个匹配,2 如果只有任何两个匹配,0 如果它们都不匹配,等等,谢谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
相关资源
最近更新 更多