fuzziness in bool query with multimatch elasticsearch

前端 未结 2 816
我寻月下人不归
我寻月下人不归 2021-01-17 03:15

i am using elasticsearch version 6.3.0. I want to use fuzziness along with multimatch. but there is no option for that. Can anybody provide me a solution ? Thanks in advance

相关标签:
2条回答
  • 2021-01-17 03:27

    What makes you think there is no option for fuzziness on a multi-match query?

    For example, with the data below:

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

    And this query:

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

    Then I get one result, but if I change the fuzziness parameter to 2 I'll get three results.

    0 讨论(0)
  • 2021-01-17 03:46

    Looking at your existing query, you are looking for mix of

    • Boosting based on field
    • Multifield match
    • Phrase Matching
    • Fuzzy Matching

    If it isn't phrase_match you can simply add "fuzziness": "AUTO" or "fuzziness":1 or whatever number based on your requirement in your existing query and you'd get what you are looking for.

    Fuzzy without Phrase

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

    Fuzzy with Phrase:

    In this case, you need to make use of Span Queries

    I've discarded the filtering part just for the sake of simplicity and came up with the below query. And let's say that I am searching for phrase called 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
                                  }
                               }
                            ]
                         }
                      }
                   ]
                }
             }
          }
       }
    }
    

    So what I am doing is performing boosting based on fields in multi-field phrase with fuzzy match for phrase called pearl jam.

    Having slop: 0 and in_order:true would enable me to do phrase match for the words I've specified in the clauses.

    Let me know if you have any queries.

    0 讨论(0)
提交回复
热议问题