From b6f4eba5dc0c88c9f47098872052d295866ee509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com> Date: Tue, 30 Jun 2015 15:44:20 +0300 Subject: [PATCH] added bool query documentation --- docs/Query/Bool.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++ docs/index.md | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 docs/Query/Bool.md diff --git a/docs/Query/Bool.md b/docs/Query/Bool.md new file mode 100644 index 0000000..f318243 --- /dev/null +++ b/docs/Query/Bool.md @@ -0,0 +1,76 @@ +# Bool query + +> More info about Bool query is in the [official elasticsearch docs][1] + +It's a query that matches documents matching boolean combinations of other queries. + +To create a bool query unlike other queries you don't have to create `BoolQuery` object. Just add queries to the search object and it will form bool query automatically. + +Lets take an example to write a bool query with Elasticsearch DSL. + +```JSON +{ + "bool" : { + "must" : { + "term" : { "user" : "kimchy" } + }, + "must_not" : { + "range" : { + "age" : { "from" : 10, "to" : 20 } + } + }, + "should" : [ + { + "term" : { "tag" : "wow" } + }, + { + "term" : { "tag" : "elasticsearch" } + } + ], + "minimum_should_match" : 1, + "boost" : 1.0 + } +} +``` + +And now the query via DSL: + +```php +$termQueryForUser = new TermQuery("user", "kimchy"); +$termQueryForTag1 = new TermQuery("tag", "wow"); +$termQueryForTag2 = new TermQuery("tag", "elasticsearch"); +$rangeQuery = new RangeQuery("age", ["from" => 10, "to" => 20]); + +$search = new Search(); +$search->addQuery($termQueryForUser, BoolQuery::MUST); +$search->addQuery($rangeQuery, BoolQuery::MUST_NOT); +$search->addQuery($termQueryForTag1, BoolQuery::SHOULD); +$search->addQuery($termQueryForTag2, BoolQuery::SHOULD); +$search->setBoolQueryParameters(["minimum_should_match" => 1, "boost" => 1]); + +$queryArray = $search->toArray(); +``` + +There is also an exception due adding queries to the search. If you add only one query without type it will form simple query. e.g. lets try to create match all query. + +```php +$search = new Search(); +$matchAllQuery = new MatchAllQuery(); +$search->addQuery($matchAllQuery); +$queryArray = $search->toArray(); +``` + +You will get this query: +```JSON +{ + "query": { + "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-query.html diff --git a/docs/index.md b/docs/index.md index bab2efd..34aa692 100644 --- a/docs/index.md +++ b/docs/index.md @@ -2,7 +2,7 @@ Welcome to Elasticsearch DSL library. The main purpose of this library is to provide objective query builder for [elasticsearch-php][1] client. -Everything starts from the `Search` object. We recommend first to take a look at the [Search](How to/HowToSearch.md) chapter. +Everything starts from the `Search` object. We recommend first to take a look at the [Search](HowTo/HowToSearch.md) chapter. ### Topics: - [Build Queries](Query/index.md) -- GitLab