From 136ff8172a3882309d2b5d004be5c5ea93b349c4 Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Tue, 7 Jul 2015 11:49:13 +0300 Subject: [PATCH] Add bool filter doc. --- docs/Filter/Bool.md | 96 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 docs/Filter/Bool.md diff --git a/docs/Filter/Bool.md b/docs/Filter/Bool.md new file mode 100644 index 0000000..51bb746 --- /dev/null +++ b/docs/Filter/Bool.md @@ -0,0 +1,96 @@ +# Bool Filter + +> More info about filter query is in the [official elasticsearch docs][1] + +A filter that matches documents matching boolean combinations of other queries. Similar in concept to +[Boolean query][2], 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. + +```JSON +{ + "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: + +```php +$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. + +```php +$matchAllFilter = new MatchAllFilter(); + +$search = new Search(); +$search->addFilter($matchAllFilter); + +$queryArray = $search->toArray(); +``` + +You will get this query: +```JSON +{ + "query": { + "filtered": { + "filter": { + "match_all": {} + } + } + } +} +``` + +> More info about `Search` look in the [How to search](../HowTo/HowToSearch.md) chapter. + + + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html +[2]: ../Query/Bool.md \ No newline at end of file -- GitLab