-
Aivaras Gotovskis authoredAivaras Gotovskis authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Bool.md 2.55 KiB
Bool Filter
More info about filter query is in the official elasticsearch docs
A filter that matches documents matching boolean combinations of other queries. Similar in concept to Boolean query, except that the clauses are other filters.
To create a bool filter unlike other queries you don't have to create BoolFilter
object. Just add queries to the
search object and it will form bool filter automatically.
Lets take an example to write a bool query with Elasticsearch DSL.
{
"filtered" : {
"query" : {
"queryString" : {
"default_field" : "message",
"query" : "elasticsearch"
}
},
"filter" : {
"bool" : {
"must" : {
"term" : { "tag" : "wow" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lt" : 20 }
}
},
"should" : [
{
"term" : { "tag" : "sometag" }
},
{
"term" : { "tag" : "sometagtag" }
}
]
}
}
}
}
And now the query via DSL:
$queryStringQuery = new QueryStringQuery('elasticsearch', ['default_field' => 'message']);
$termFilter1 = new TermFilter('tag', 'wow');
$rangeFilter = new RangeFilter('age', ['gte' => 10, 'lt' => 20]);
$termFilter2 = new TermFilter('tag', 'sometag');
$termFilter3 = new TermFilter('tag', 'sometagtag');
$search = new Search();
$search->addQuery($queryStringQuery);
$search->addFilter($termFilter1);
$search->addFilter($rangeFilter, BoolFilter::MUST_NOT);
$search->addFilter($termFilter2, BoolFilter::SHOULD);
$search->addFilter($termFilter3, BoolFilter::SHOULD);
$queryArray = $search->toArray();
There is also an exception due adding filters to the search. If you add only one filter without type it will form simple query. e.g. lets try to create match all filter.
$matchAllFilter = new MatchAllFilter();
$search = new Search();
$search->addFilter($matchAllFilter);
$queryArray = $search->toArray();
You will get this query:
{
"query": {
"filtered": {
"filter": {
"match_all": {}
}
}
}
}
More info about
Search
look in the How to search chapter.