Skip to content
Snippets Groups Projects
Commit 034fde02 authored by Mantas Varatiejus's avatar Mantas Varatiejus
Browse files

Remove filters

parent 766668b6
No related branches found
No related tags found
No related merge requests found
Showing
with 6 additions and 896 deletions
......@@ -5,6 +5,7 @@ v2.0.0 (2016-x)
---
- [BC break] Aggregation name is not prefixed anymore
- [BC break] Removed all filters and filtered query
v1.1.1 (2016-01-26)
---
......
......@@ -10,6 +10,7 @@ If you have any questions, don't hesitate to ask them on [![Join the chat at htt
[![Latest Stable Version](https://poser.pugx.org/ongr/elasticsearch-dsl/v/stable)](https://packagist.org/packages/ongr/elasticsearch-dsl)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchDSL/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchDSL/?branch=master)
__This component requires Elasticsearch 2.0 or newer.__
## Documentation
......@@ -21,8 +22,8 @@ If you have any questions, don't hesitate to ask them on [![Join the chat at htt
Install library with [composer](https://getcomposer.org):
```
composer require ongr/elasticsearch-dsl "~1.0"
```bash
$ composer require ongr/elasticsearch-dsl
```
### Search
......@@ -33,8 +34,8 @@ If you dont want to use Symfony or Elasticsearch bundle, no worries, you can use
Install `elasticsearch-php`:
```
composer require "elasticsearch/elasticsearch": "~2.0"
```bash
$ composer require elasticsearch/elasticsearch
```
Create search:
......
# And Filter
> More info about and filter is in the [official elasticsearch docs][1]
A filter that matches documents using the AND boolean operator on other filters.
Can be placed within queries that accept a filter.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"term" : { "name.first" : "shay" }
},
"filter" : {
"and" : [
{
"range" : {
"postDate" : {
"from" : "2010-03-01",
"to" : "2010-04-01"
}
}
},
{
"prefix" : { "name.second" : "ba" }
}
]
}
}
}
```
And now the query via DSL:
```php
$rangeFilter = new RangeFilter('postDate', ['from' => '2010-03-01', 'to' => '2010-04-01']);
$prefixFilter = new PrefixFilter('name.second', 'ba');
$andFilter = new AndFilter([$rangeFilter, $prefixFilter]);
$termQuery = new TermQuery('name.first', 'shay');
$search = new Search();
$search->addQuery($termQuery);
$search->addFilter($andFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-filter.html
# 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
# Exists Filter
> More info about exists filter is in the [official elasticsearch docs][1]
Returns documents that have at least one non-null value in the original field.
## Simple example
```JSON
{
"constant_score" : {
"filter" : {
"exists" : { "field" : "user" }
}
}
}
```
And now the query via DSL:
```php
$existsFilter = new ExistsFilter('user');
$constantScoreQuery = new ConstantScoreQuery($existsFilter);
$search = new Search();
$search->addQuery($constantScoreQuery);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html
# Geo Bounding Box Filter
> More info about geo bounding box filter is in the [official elasticsearch docs][1]
A filter allowing to filter hits based on a point location using a bounding box.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_bounding_box" : {
"pin.location" : {
"top_left" : {
"lat" : 40.73,
"lon" : -74.1
},
"bottom_right" : {
"lat" : 40.01,
"lon" : -71.12
}
}
}
}
}
}
```
And now the query via DSL:
```php
$geoBoundingBoxFilter = new GeoBoundingBoxFilter(
'pin.location',
[
['lat' => 40.73, 'lon' => -74.1],
['lat' => 40.01, 'lon' => -74.12],
]
);
$search = new Search();
$search->addFilter($geoBoundingBoxFilter);
$queryArray = $search->toArray();
```
Other format
```JSON
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_bounding_box" : {
"pin.location" : {
"top" : -74.1,
"left" : 40.73,
"bottom" : -71.12,
"right" : 40.01
}
}
}
}
```
In DSL
```php
$geoBoundingBoxFilter = new GeoBoundingBoxFilter(
'pin.location',
[
-74.1,
40.73,
-71.12,
40.01,
]
);
$search = new Search();
$search->addFilter($geoBoundingBoxFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-filter.html
# Geo Distance Filter
> More info about geo distance filter is in the [official elasticsearch docs][1]
Filters documents that include only hits that exists within a specific distance from a geo point.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "200km",
"pin.location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
```
And now the query via DSL:
```php
$geoDistanceFilter = new GeoDistanceFilter(
'pin.location',
'200km',
['lat' => 40, 'lon' => -70]
);
$search = new Search();
$search->addFilter($geoDistanceFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html
# Geo Distance Range Filter
> More info about geo distance range filter is in the [official elasticsearch docs][1]
Filters documents that exists within a range from a specific point.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance_range" : {
"from" : "200km",
"to" : "400km",
"pin.location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
```
And now the query via DSL:
```php
$geoDistanceRangeFilter = new GeoDistanceRangeFilter(
'pin.location',
['from' => '200km', 'to' => '400km'],
['lat' => 40, 'lon' => -70]
);
$search = new Search();
$search->addFilter($geoDistanceRangeFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-filter.html
# Geo Polygon Filter
> More info about geo polygon range filter is in the [official elasticsearch docs][1]
A filter allowing to include hits that only fall within a polygon of points.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_polygon" : {
"person.location" : {
"points" : [
{"lat" : 40, "lon" : -70},
{"lat" : 30, "lon" : -80},
{"lat" : 20, "lon" : -90}
]
}
}
}
}
}
```
And now the query via DSL:
```php
$geoPolygonFilter = new GeoPolygonFilter(
'person.location',
[
['lat' => 40, 'lon' => -70],
['lat' => 30, 'lon' => -80],
['lat' => 20, 'lon' => -90],
]
);
$search = new Search();
$search->addFilter($geoPolygonFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html
# Geo Shape Filter
> More info about geo shape filter is in the [official elasticsearch docs][1]
Filter documents indexed using the geo shape type.
## Simple example
```JSON
{
"query":{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "envelope",
"coordinates" : [[13.0, 53.0], [14.0, 52.0]]
}
}
}
}
}
}
}
```
And now the query via DSL:
```php
$geoShapeFilter = new GeoShapeFilter();
$geoShapeFilter->addShape('location', 'envelope', [[13.0, 53.0], [14.0, 52.0]]);
$search = new Search();
$search->addFilter($geoShapeFilter);
$queryArray = $search->toArray();
```
## Pre Indexed Shape
```JSON
{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"indexed_shape": {
"id": "DEU",
"type": "countries",
"index": "shapes",
"path": "location"
}
}
}
}
}
}
```
And now the query via DSL:
```php
$geoShapeFilter = new GeoShapeFilter();
$geoShapeFilter->addPreIndexedShape(
'location',
'DEU',
'countries',
'shapes',
'location'
);
$search = new Search();
$search->addFilter($geoShapeFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-filter.html
# Geohash Cell Filter
> More info about geohash cell filter is in the [official elasticsearch docs][1]
The geohash cell filter provides access to a hierarchy of geohashes.
By defining a geohash cell, only geopoints within this cell will match this filter.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geohash_cell": {
"pin": {
"lat": 13.4080,
"lon": 52.5186
},
"precision": 3,
"neighbors": true
}
}
}
}
```
And now the query via DSL:
```php
$geohashCellFilter = new GeohashCellFilter(
'pin',
[
'lat' => 13.4080,
'lon' => 52.5186,
],
[
'precision' => 3,
'neighbors' => true,
]
);
$search = new Search();
$search->addFilter($geohashCellFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geohash-cell-filter.html
# Has Child Filter
> More info about has child filter is in the [official elasticsearch docs][1]
The has child filter accepts a query and the child type to run against,
and results in parent documents that have child docs matching the query.
## Simple example
```JSON
{
"has_child" : {
"type" : "blog_tag",
"query" : {
"term" : {
"tag" : "something"
}
}
}
}
```
And now the query via DSL:
```php
$termQuery = new TermQuery('tag', 'something');
$hasChildFilter = new HasChildFilter(
'blog_tag',
$termQuery
);
$search = new Search();
$search->addFilter($hasChildFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-filter.html
# Has Parent Filter
> More info about has parent filter is in the [official elasticsearch docs][1]
The has parent filter accepts a query and a parent type.
The query is executed in the parent document space, which is specified by the parent type.
This filter returns child documents which associated parents have matched.
## Simple example
```JSON
{
"has_child" : {
"type" : "blog_tag",
"query" : {
"term" : {
"tag" : "something"
}
}
}
}
```
And now the query via DSL:
```php
$termQuery = new TermQuery('tag', 'something');
$hasParentFilter = new HasParentFilter(
'blog',
$termQuery
);
$search = new Search();
$search->addFilter($hasParentFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-filter.html
# Ids Filter
> More info about ids filter is in the [official elasticsearch docs][1]
Filters documents that only have the provided ids.
## Simple example
```JSON
{
"ids" : {
"type" : "my_type",
"values" : ["1", "4", "100"]
}
}
```
And now the query via DSL:
```php
$idsFilters = new IdsFilter(
['1', '4', '100'],
['type' => 'my_type']
);
$search = new Search();
$search->addFilter($idsFilters);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-filter.html
# Indices Filter
> More info about indices filter is in the [official elasticsearch docs][1]
The indices filter can be used when executed across multiple indices,
allowing to have a filter that executes only when executed on an index
that matches a specific list of indices, and another filter that
executes when it is executed on an index that does not match the listed
indices.
## Simple example
```JSON
{
"indices" : {
"indices" : ["index1", "index2"],
"filter" : {
"term" : { "tag" : "wow" }
},
"no_match_filter" : {
"term" : { "tag" : "kow" }
}
}
}
```
And now the query via DSL:
```php
$termFilter1 = new TermFilter('tag', 'wow');
$termFilter2 = new TermFilter('tag', 'kow');
$indicesFilter = new IndicesFilter(
['index1', 'index2'],
$termFilter1,
$termFilter2
);
$search = new Search();
$search->addFilter($indicesFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-filter.html
# Limit Filter
> More info about limit filter is in the [official elasticsearch docs][1]
A limit filter limits the number of documents (per shard) to execute on.
## Simple example
```JSON
{
"filtered" : {
"filter" : {
"limit" : {"value" : 100}
},
"query" : {
"term" : { "name.first" : "shay" }
}
}
}
```
And now the query via DSL:
```php
$limitFilter = new LimitFilter(100);
$termQuery = new TermQuery('name.first', 'shay');
$filteredQuery = new FilteredQuery($termQuery, $limitFilter);
$search = new Search();
$search->addQuery($filteredQuery);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-limit-filter.html
# Match All Filter
> More info about match all filter is in the [official elasticsearch docs][1]
A filter that matches on all documents.
## Simple example
```JSON
{
"filter" : {
"match_all" : { }
}
}
```
And now the query via DSL:
```php
$matchAllFilter = new MatchAllFilter();
$search = new Search();
$search->addFilter($matchAllFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-filter.html
# Missing Filter
> More info about missing filter is in the [official elasticsearch docs][1]
Returns documents that have only null values or no value in the original field.
## Simple example
```JSON
{
"filter" : {
"missing" : { "field" : "user" }
}
}
```
And now the query via DSL:
```php
$missingFilter = new MissingFilter('user');
$search = new Search();
$search->addFilter($missingFilter);
$queryArray = $search->toArray();
```
[1]: hhttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-missing-filter.html
# Nested Filter
> More info about nested filter is in the [official elasticsearch docs][1]
Nested filter allows to filter nested objects / documents (see nested mapping).
The filter is executed against the nested objects / documents as if they
were indexed as separate documents (they are, internally) and resulting
in the root parent doc (or parent nested mapping).
## Simple example
```JSON
{
"filtered" : {
"query" : { "match_all" : {} },
"filter" : {
"nested" : {
"path" : "obj1",
"filter" : {
"bool" : {
"must" : [
{
"term" : {"obj1.name" : "blue"}
},
{
"range" : {"obj1.count" : {"gt" : 5}}
}
]
}
},
"_cache" : true
}
}
}
}
```
And now the query via DSL:
```php
$termFilter = new TermFilter('obj1.name', 'blue');
$rangeFilter = new RangeFilter('obj1.count', ['gt' => 5]);
$boolFilter = new BoolFilter();
$boolFilter->add($termFilter);
$boolFilter->add($rangeFilter);
$nestedFilter = new NestedFilter('obj1', $boolFilter, ['_cache' => true]);
$search = new Search();
$search->addFilter($nestedFilter);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html
# Not Filter
> More info about not filter is in the [official elasticsearch docs][1]
A filter that filters out matched documents using a query.
## Simple example
```JSON
{
"filtered" : {
"query" : {
"term" : { "name.first" : "shay" }
},
"filter" : {
"not" : {
"range" : {
"postDate" : {
"from" : "2010-03-01",
"to" : "2010-04-01"
}
}
}
}
}
}
```
And now the query via DSL:
```php
$rangeFilter = new RangeFilter(
'postDate',
[
'from' => '2010-03-01',
'to' => '2010-04-01',
]
);
$notFilter = new NotFilter($rangeFilter);
$termQuery = new TermQuery('name.first', 'shay');
$filteredQuery = new FilteredQuery($termQuery, $notFilter);
$search = new Search();
$search->addQuery($filteredQuery);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-not-filter.html
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