fuzziness in bool query with multimatch elasticsearch

冷暖自知 提交于 2019-12-30 13:31:05

问题


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 Query :

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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/53407151/fuzziness-in-bool-query-with-multimatch-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!