Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search in multiple fields with OR statement in ElasticSearch

A have a string, let's say "Ab Cd"

I have documents with fields: ['a', 'b', 'c', 'd', ... 'z'] (not all documents have all fields)

I want search only in fields 'a', 'c', 'f', 'x' but I want to return all fields in document.

Hit is successful if ANY of fields 'a', 'c', 'f', 'x' contains string beginning 'Ab' or 'Cd'.

Now I use this but it searches in ALL fields not in selected ones.

{'query': {'query_string': {'query': "Ab* Cd*"}}}
like image 685
Jiri Knesl Avatar asked Mar 13 '12 14:03

Jiri Knesl


1 Answers

It's possible to specify list of fields that you want to search by default as part of the query:

{ 
    "query": { 
        "query_string" : {
            "fields" : ["a", "c", "f", "x"],
            "query" : "Ab* Cd*"
        } 
    }
}

Alternatively, you can prefix every term with the name of the field:

{'query': {'query_string': {'query': "a:Ab* c:Ab* f:Ab* x:Ab* a:Cd* c:Cd* f:Cd* x:Cd*"}}}

Check Query String Query page of elasticsearch Guide that describes these and other useful options.

like image 102
imotov Avatar answered Sep 30 '22 05:09

imotov