Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use minimum_should_match to search in multiple fields?

I'm trying to filter my elasticsearch's result, it must return the results which are 80% compatible with the search text.

When I do this for just one key, the minimum_should_match works perfect:

{
       "size":30,
       "from":930,
       "query":{
          "filtered":{
             "query":{
                "query_string":{
                   "default_field":"campo1",
                   "query":"portugues",                  
                   "minimum_should_match":"80%"
                }
             }
          }
       }
    }

When I search using more than one key, the minimum_should_match doesn't work right, return the results with 70% compatibility:

{
       "size":30,
       "from":123420,
       "query":{
          "filtered":{
             "query":{
                "query_string":{
                   "default_operator":"or",
                   "query":"portugues",
                   "fields":[
                      "campo1",
                      "campo2^5",
                      "campo3"
                   ],
                   "minimum_should_match":"80%"
                }
             }
          }
       }
    }

As far as I can think of, I need to set minimum_should_match by key, but I don't know how to do the same. If someone can help me out in doing so, will be great.

like image 415
Alessandro Gomes Avatar asked Mar 27 '14 18:03

Alessandro Gomes


1 Answers

I needed to use bool and multi_match, this is right way:

{
   "size":"30",
   "from":0,
   "query":{
      "filtered":{
         "query":{
            "bool":{
               "should":[
                  {
                     "multi_match":{
                        "query":"portugues",
                        "type":"cross_fields",
                        "fields":[
                           "campo1^3",
                           "campo2^5",
                           "campo3^3"
                        ],
                        "minimum_should_match":"80%"
                     }
                  }
               ]
            }
         }
      }
   }
}
like image 163
Alessandro Gomes Avatar answered Nov 13 '22 14:11

Alessandro Gomes