Skip to content
Snippets Groups Projects
Commit b6f4eba5 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

added bool query documentation

parent e5ecc74f
No related branches found
No related tags found
No related merge requests found
# 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
...@@ -2,7 +2,7 @@ ...@@ -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. 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: ### Topics:
- [Build Queries](Query/index.md) - [Build Queries](Query/index.md)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment