Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

fuzziness in bool query with multimatch elasticsearch

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
              }
            }
          }
        }   
} }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...