Query with AND operator in Elasticsearch

Asked

Viewed 38 times

3

I have a query of Elasticsearch would be using the operator AND to find the two words (fast AND car) which must have the two words in the result, no matter if the two words are in the title or if the two are in the description or if one word is in the title and the other is in the description, doesn’t matter, but it has to have two words in the result.

The query below is working as OR bringing results that have the two words and results that have only one of the words.

{
"from" : 0, "size" : 10,

    "query": {

       "bool": {
           "must": [
               {
                   "match": {
                       "titulo": {
                       "query": "carro veloz",
                       "fuzziness": 1

                       }
                   }
               },
               {
                   "match": {
                       "descricao": {
                           "query": "carro veloz",
                       "fuzziness": 1
                       }
                   }
               }
       ]
    }
}
}

1 answer

2


Use multi_match with a cross Fields, I believe that solves your problem

{
    "query":{
        "bool":{
            "must":{
                "multi_match":{
                    "query":"carro veloz",
                    "operator":"and",
                    "type": "cross_fields",
                    "fields":["descricao", "titulo"]
                }
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.