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*"}}}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With