【问题标题】:fuzziness in bool query with multimatch elasticsearch具有多匹配弹性搜索的布尔查询中的模糊性
【发布时间】:2018-11-21 07:29:25
【问题描述】:

我使用的是弹性搜索 6.3.0 版。我想将模糊性与多匹配一起使用。但没有选择。谁能给我一个解决方案?提前致谢 查询:

    {   "query": {
        "bool": {
          "must": [
            {"function_score": {
              "query": {
                "multi_match": {
                  "query": "local",
                  "fields": [
                      "user.name^3", 
                      "main_product"
                    ],
                    "type": "phrase"
                }
              }
            }}
          ], 
          "filter": {
            "geo_distance": {
              "distance": "1000km",

              "user.geolocation": {
                "lat": 25.55,
                "lon": -84.44
              }
            }
          }
        }   
} }

【问题讨论】:

    标签: elasticsearch elasticsearch-6


    【解决方案1】:

    查看您现有的查询,您正在寻找混合的

    • 基于字段的提升
    • 多字段匹配
    • 词组匹配
    • 模糊匹配

    如果不是phrase_match,您只需在现有查询中添加"fuzziness": "AUTO""fuzziness":1 or whatever number based on your requirement,您就会得到所需的内容。

    没有短语的模糊

    POST <your_index_name>/_search
    {  
       "query":{  
          "bool":{  
             "must":[  
                {  
                   "function_score":{  
                      "query":{  
                         "multi_match":{  
                            "query":"local",
                            "fields":[  
                               "user.name^3",
                               "main_product"
                            ],
                            "fuzziness":"AUTO"
                         }
                      }
                   }
                }
             ],
             "filter":{  
                "geo_distance":{  
                   "distance":"1000km",
                   "user.geolocation":{  
                      "lat":25.55,
                      "lon":-84.44
                   }
                }
             }
          }
       }
    }
    

    模糊与短语:

    在这种情况下,你需要使用Span Queries

    为了简单起见,我放弃了过滤部分,并提出了以下查询。假设我正在搜索名为 pearl jam 的短语。

    POST <your_index_name>/_search
    {  
       "query":{  
          "function_score":{  
             "query":{  
                "bool":{  
                   "should":[  
                      {  
                         "bool":{  
                            "boost":3,
                            "must":[  
                               {  
                                  "span_near":{  
                                     "clauses":[  
                                        {  
                                           "span_multi":{  
                                              "match":{  
                                                 "fuzzy":{  
                                                    "user.name":"pearl"
                                                 }
                                              }
                                           }
                                        },
                                        {  
                                           "span_multi":{  
                                              "match":{  
                                                 "fuzzy":{  
                                                    "user.name":"jam"
                                                 }
                                              }
                                           }
                                        }
                                     ],
                                     "slop":0,
                                     "in_order":true
                                  }
                               }
                            ]
                         }
                      },
                      {  
                         "bool":{  
                            "boost":1,
                            "must":[  
                               {  
                                  "span_near":{  
                                     "clauses":[  
                                        {  
                                           "span_multi":{  
                                              "match":{  
                                                 "fuzzy":{  
                                                    "main_product":"pearl"
                                                 }
                                              }
                                           }
                                        },
                                        {  
                                           "span_multi":{  
                                              "match":{  
                                                 "fuzzy":{  
                                                    "main_product":"jam"
                                                 }
                                              }
                                           }
                                        }
                                     ],
                                     "slop":0,
                                     "in_order":true
                                  }
                               }
                            ]
                         }
                      }
                   ]
                }
             }
          }
       }
    }
    

    所以我正在做的是基于模糊匹配的多字段短语中的字段为名为@9​​87654328@ 的短语执行提升。

    拥有slop: 0in_order:true 将使我能够对我在条款中指定的单词进行词组匹配。

    如果您有任何疑问,请告诉我。

    【讨论】:

    • 嗨,Kamal,您在两个不同的必须查询中明确提到了两个不同的字段。我必须对 100 个字段执行相同的查询。如何执行这种搜索?
    • 恐怕跨度查询是不可能的。您需要重写字段的查询。如果您在服务层上创建查询,您可以想出一个循环来遍历所有字段,从而为您要查找的每个字段构建查询。
    • 感谢您的回复 Kamal,不幸的是,我的服务层不是这种情况,因为我没有服务层的字段信息。
    【解决方案2】:

    是什么让您认为在多匹配查询中没有模糊性选项?

    例如,下面的数据:

    http://localhost:9200/question_1/doc/_bulk
    {"index":{}}
    {"name" : "John Lazy", "text": "lazzi"}
    {"index":{}}
    {"name" : "John Lassi", "text": "lasso"}
    {"index":{}}
    {"name" : "Joan Labbe", "text": "lazzy"}
    

    这个查询:

    http://localhost:9200/question_1/_search
    {
      "query": {
        "multi_match" : {
          "query" : "lazi",
          "fields" : [ "name", "text" ],
          "fuzziness": 1
        }
      }
    }
    

    然后我得到一个结果,但是如果我将fuzziness 参数更改为2 我会得到三个结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多