elastica - Elasticsearch bool filter with AND operator -
- i trying write query match multiple names, below query fine doesn't exact match e.g gives results name 'x a' or 'b y' want name exact or exact b.can tell me m doing wrong.
also possible match multiple fields name=a or surname=a or name=b or surname=b
{ "filter": { "bool": { "should": [ { "term": { "name": "a" } },{ "term": { "name": "b" } } ] } },"sort": [ { "pub": { "rank": "desc" } } ] }
in boolean query, must means queries must evaluate true (so logical and), , should means @ least 1 must evaluate true (so logical or).
to have exact match on name, need use keyword analyzer. stop names being tokenized on whitespace, "william riker" indexed (keyword analyzer) ["william riker"] or (default analyzer) ["william", "riker"]. in default case, match term query of either "william" or "riker". in keyword case match full name.
to want, "name=a or surname=a or name=b or surname=b" make 4 should clauses.
{ "bool":{ "should":[ { "term":{ "name":"a" } }, { "term":{ "name":"b" } }, { "term":{ "surname":"a" } }, { "term":{ "surname":"b" } } ] } }
Comments
Post a Comment