2013-08-28 19:24:34 -04:00
|
|
|
|
[[query-dsl-filters]]
|
|
|
|
|
== Query DSL - Filters
|
|
|
|
|
|
2015-05-24 08:13:51 -04:00
|
|
|
|
elasticsearch provides a full Java query DSL in a similar manner to the
|
2013-09-03 10:15:28 -04:00
|
|
|
|
REST {ref}/query-dsl.html[Query DSL]. The factory for filter
|
2013-08-28 19:24:34 -04:00
|
|
|
|
builders is `FilterBuilders`.
|
|
|
|
|
|
|
|
|
|
Once your query is ready, you can use the <<search,Search API>>.
|
|
|
|
|
|
|
|
|
|
See also how to build <<query-dsl-queries,Queries>>.
|
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
To use `QueryBuilders` or `FilterBuilders` just import them in your class:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
|
|
|
|
import static org.elasticsearch.index.query.FilterBuilders.*;
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Note that you can easily print (aka debug) JSON generated queries using
|
|
|
|
|
`toString()` method on `FilterBuilder` object.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[and-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== And Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-and-filter.html[And Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = andFilter(
|
|
|
|
|
rangeFilter("postDate").from("2010-03-01").to("2010-04-01"), <1>
|
|
|
|
|
prefixFilter("name.second", "ba")); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> filters
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`AndFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[bool-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Bool Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-bool-filter.html[Bool Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = boolFilter()
|
|
|
|
|
.must(termFilter("tag", "wow")) <1>
|
|
|
|
|
.mustNot(rangeFilter("age").from("10").to("20")) <2>
|
|
|
|
|
.should(termFilter("tag", "sometag")) <3>
|
|
|
|
|
.should(termFilter("tag", "sometagtag")); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> must filter
|
|
|
|
|
<2> must not filter
|
|
|
|
|
<3> should filter
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`BoolFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[exists-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Exists Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-exists-filter.html[Exists Filter].
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = existsFilter("user"); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[ids-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Ids Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-ids-filter.html[IDs Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = idsFilter("my_type", "type2")
|
|
|
|
|
.addIds("1", "4", "100");
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = idsFilter() <1>
|
|
|
|
|
.addIds("1", "4", "100");
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> type is optional
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[limit-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Limit Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-limit-filter.html[Limit Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = limitFilter(100); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> number of documents per shard
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[type-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Type Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-type-filter.html[Type Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = typeFilter("my_type"); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> type
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[geo-bbox-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Geo Bounding Box Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-geo-bounding-box-filter.html[Geo
|
2013-08-28 19:24:34 -04:00
|
|
|
|
Bounding Box Filter]
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoBoundingBoxFilter("pin.location") <1>
|
|
|
|
|
.topLeft(40.73, -74.1) <2>
|
|
|
|
|
.bottomRight(40.717, -73.99); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> bounding box top left point
|
|
|
|
|
<3> bounding box bottom right point
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`GeoBoundingBoxFilterBuilder#cache(boolean)` method. See
|
|
|
|
|
<<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[geo-distance-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== GeoDistance Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-geo-distance-filter.html[Geo
|
2013-08-28 19:24:34 -04:00
|
|
|
|
Distance Filter]
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoDistanceFilter("pin.location") <1>
|
|
|
|
|
.point(40, -70) <2>
|
|
|
|
|
.distance(200, DistanceUnit.KILOMETERS) <3>
|
|
|
|
|
.optimizeBbox("memory") <4>
|
|
|
|
|
.geoDistance(GeoDistance.ARC); <5>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> center point
|
|
|
|
|
<3> distance from center point
|
|
|
|
|
<4> optimize bounding box: `memory`, `indexed` or `none`
|
2015-05-05 04:03:15 -04:00
|
|
|
|
<5> distance computation mode: `GeoDistance.SLOPPY_ARC` (default), `GeoDistance.ARC` (slightly more precise but
|
2014-09-23 06:39:57 -04:00
|
|
|
|
significantly slower) or `GeoDistance.PLANE` (faster, but inaccurate on long distances and close to the poles)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`GeoDistanceFilterBuilder#cache(boolean)` method. See
|
|
|
|
|
<<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[geo-distance-range-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Geo Distance Range Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-geo-distance-range-filter.html[Geo
|
2013-08-28 19:24:34 -04:00
|
|
|
|
Distance Range Filter]
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoDistanceRangeFilter("pin.location") <1>
|
|
|
|
|
.point(40, -70) <2>
|
|
|
|
|
.from("200km") <3>
|
|
|
|
|
.to("400km") <4>
|
|
|
|
|
.includeLower(true) <5>
|
|
|
|
|
.includeUpper(false) <6>
|
|
|
|
|
.optimizeBbox("memory") <7>
|
|
|
|
|
.geoDistance(GeoDistance.ARC); <8>
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
<1> field
|
|
|
|
|
<2> center point
|
|
|
|
|
<3> starting distance from center point
|
|
|
|
|
<4> ending distance from center point
|
|
|
|
|
<5> include lower value means that `from` is `gt` when `false` or `gte` when `true`
|
|
|
|
|
<6> include upper value means that `to` is `lt` when `false` or `lte` when `true`
|
|
|
|
|
<7> optimize bounding box: `memory`, `indexed` or `none`
|
2015-05-24 08:13:51 -04:00
|
|
|
|
<8> distance computation mode: `GeoDistance.SLOPPY_ARC` (default), `GeoDistance.ARC` (slightly more precise but
|
2014-09-23 06:39:57 -04:00
|
|
|
|
significantly slower) or `GeoDistance.PLANE` (faster, but inaccurate on long distances and close to the poles)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`GeoDistanceRangeFilterBuilder#cache(boolean)` method. See
|
|
|
|
|
<<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[geo-poly-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Geo Polygon Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-geo-polygon-filter.html[Geo Polygon
|
2013-08-28 19:24:34 -04:00
|
|
|
|
Filter]
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoPolygonFilter("pin.location") <1>
|
|
|
|
|
.addPoint(40, -70) <2>
|
|
|
|
|
.addPoint(30, -80) <2>
|
|
|
|
|
.addPoint(20, -90); <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> add your polygon of points a document should fall within
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`GeoPolygonFilterBuilder#cache(boolean)` method. See
|
|
|
|
|
<<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[geo-shape-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Geo Shape Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-geo-shape-filter.html[Geo Shape
|
2013-08-28 19:24:34 -04:00
|
|
|
|
Filter]
|
|
|
|
|
|
|
|
|
|
Note: the `geo_shape` type uses `Spatial4J` and `JTS`, both of which are
|
|
|
|
|
optional dependencies. Consequently you must add `Spatial4J` and `JTS`
|
|
|
|
|
to your classpath in order to use this type:
|
|
|
|
|
|
|
|
|
|
[source,xml]
|
|
|
|
|
-----------------------------------------------
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.spatial4j</groupId>
|
|
|
|
|
<artifactId>spatial4j</artifactId>
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<version>0.4.1</version> <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
</dependency>
|
|
|
|
|
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.vividsolutions</groupId>
|
|
|
|
|
<artifactId>jts</artifactId>
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<version>1.13</version> <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
<exclusions>
|
|
|
|
|
<exclusion>
|
|
|
|
|
<groupId>xerces</groupId>
|
|
|
|
|
<artifactId>xercesImpl</artifactId>
|
|
|
|
|
</exclusion>
|
|
|
|
|
</exclusions>
|
|
|
|
|
</dependency>
|
|
|
|
|
-----------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.spatial4j%22%20AND%20a%3A%22spatial4j%22[Maven Central]
|
|
|
|
|
<2> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22[Maven Central]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
// Import Spatial4J shapes
|
|
|
|
|
import com.spatial4j.core.context.SpatialContext;
|
|
|
|
|
import com.spatial4j.core.shape.Shape;
|
|
|
|
|
import com.spatial4j.core.shape.impl.RectangleImpl;
|
|
|
|
|
|
|
|
|
|
// Also import ShapeRelation
|
|
|
|
|
import org.elasticsearch.common.geo.ShapeRelation;
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
// Shape within another
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoShapeFilter(
|
|
|
|
|
"location", <1>
|
|
|
|
|
new RectangleImpl(0,10,0,10,SpatialContext.GEO) <2>
|
|
|
|
|
)
|
|
|
|
|
.relation(ShapeRelation.WITHIN); <3>
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
<1> field
|
|
|
|
|
<2> shape
|
|
|
|
|
<3> relation
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2013-08-28 19:24:34 -04:00
|
|
|
|
// Intersect shapes
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoShapeFilter(
|
|
|
|
|
"location", <1>
|
|
|
|
|
new PointImpl(0, 0, SpatialContext.GEO) <2>
|
|
|
|
|
)
|
|
|
|
|
.relation(ShapeRelation.INTERSECTS); <3>
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
<1> field
|
|
|
|
|
<2> shape
|
|
|
|
|
<3> relation
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2013-08-28 19:24:34 -04:00
|
|
|
|
// Using pre-indexed shapes
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = geoShapeFilter(
|
|
|
|
|
"location", <1>
|
|
|
|
|
"New Zealand", <2>
|
|
|
|
|
"countries") <3>
|
|
|
|
|
.relation(ShapeRelation.DISJOINT); <4>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> indexed shape id
|
|
|
|
|
<3> index type of the indexed shapes
|
|
|
|
|
<4> relation
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[has-child-parent-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Has Child / Has Parent Filters
|
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
|
See:
|
2013-09-03 10:15:28 -04:00
|
|
|
|
* {ref}/query-dsl-has-child-filter.html[Has Child Filter]
|
|
|
|
|
* {ref}/query-dsl-has-parent-filter.html[Has Parent Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
// Has Child
|
2014-09-23 06:39:57 -04:00
|
|
|
|
QueryBuilder qb = hasChildFilter(
|
|
|
|
|
"blog_tag", <1>
|
|
|
|
|
termFilter("tag","something") <2>
|
|
|
|
|
);
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
<1> child type to query against
|
|
|
|
|
<2> filter (could be also a query)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2013-08-28 19:24:34 -04:00
|
|
|
|
// Has Parent
|
2014-09-23 06:39:57 -04:00
|
|
|
|
QueryBuilder qb = hasParentFilter(
|
|
|
|
|
"blog", <1>
|
|
|
|
|
termFilter("tag","something") <2>
|
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> parent type to query against
|
|
|
|
|
<2> filter (could be also a query)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[match-all-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Match All Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-match-all-filter.html[Match All Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = matchAllFilter();
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[missing-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Missing Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-missing-filter.html[Missing Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = missingFilter("user") <1>
|
|
|
|
|
.existence(true) <2>
|
|
|
|
|
.nullValue(true); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> find missing field that doesn’t exist
|
|
|
|
|
<3> find missing field with an explicit `null` value
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[not-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Not Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-not-filter.html[Not Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = notFilter(
|
|
|
|
|
rangeFilter("price").from("1").to("2") <1>
|
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> filter
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[or-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Or Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-or-filter.html[Or Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = orFilter(
|
|
|
|
|
termFilter("name.second", "banon"), <1>
|
|
|
|
|
termFilter("name.nick", "kimchy") <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
);
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> filters
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`OrFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[prefix-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Prefix Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-prefix-filter.html[Prefix Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = prefixFilter(
|
|
|
|
|
"user", <1>
|
|
|
|
|
"ki" <2>
|
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> prefix
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`PrefixFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[query-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Query Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-query-filter.html[Query Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = queryFilter(
|
2015-06-03 11:08:32 -04:00
|
|
|
|
queryStringQuery("this AND that OR thus") <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
);
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> query you want to wrap as a filter
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`QueryFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[range-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Range Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-range-filter.html[Range Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = rangeFilter("age") <1>
|
|
|
|
|
.from("10") <2>
|
|
|
|
|
.to("20") <3>
|
|
|
|
|
.includeLower(true) <4>
|
|
|
|
|
.includeUpper(false); <5>
|
|
|
|
|
--------------------------------------------------
|
|
|
|
|
<1> field
|
|
|
|
|
<2> from
|
|
|
|
|
<3> to
|
|
|
|
|
<4> include lower value means that `from` is `gt` when `false` or `gte` when `true`
|
|
|
|
|
<5> include upper value means that `to` is `lt` when `false` or `lte` when `true`
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2013-08-28 19:24:34 -04:00
|
|
|
|
// A simplified form using gte, gt, lt or lte
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = rangeFilter("age") <1>
|
|
|
|
|
.gte("10") <2>
|
|
|
|
|
.lt("20"); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> set `from` to 10 and `includeLower` to true
|
|
|
|
|
<3> set `to` to 20 and `includeUpper` to false
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can ask not to cache the result using
|
|
|
|
|
`RangeFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[script-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Script Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-script-filter.html[Script Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = scriptFilter(
|
|
|
|
|
"doc['age'].value > param1" <1>
|
|
|
|
|
).addParam("param1", 10); <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> script to execute
|
|
|
|
|
<2> parameters
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can cache the result using
|
|
|
|
|
`ScriptFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[term-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Term Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-term-filter.html[Term Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = termFilter(
|
|
|
|
|
"user", <1>
|
|
|
|
|
"kimchy" <2>
|
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> value
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can ask not to cache the result using
|
|
|
|
|
`TermFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[terms-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Terms Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-terms-filter.html[Terms Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = termsFilter(
|
|
|
|
|
"user", <1>
|
|
|
|
|
"kimchy", <2>
|
|
|
|
|
"elasticsearch" <2>
|
|
|
|
|
)
|
2014-09-23 07:30:08 -04:00
|
|
|
|
.execution("plain"); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> field
|
|
|
|
|
<2> terms
|
|
|
|
|
<3> execution mode: could be `plain`, `fielddata`, `bool`, `and`, `or`, `bool_nocache`, `and_nocache` or `or_nocache`
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can ask not to cache the result using
|
|
|
|
|
`TermsFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
|
[[nested-filter]]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
=== Nested Filter
|
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
|
See {ref}/query-dsl-nested-filter.html[Nested Filter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = nestedFilter("obj1", <1>
|
|
|
|
|
boolFilter() <2>
|
|
|
|
|
.must(termFilter("obj1.name", "blue"))
|
|
|
|
|
.must(rangeFilter("obj1.count").gt(5))
|
2013-08-28 19:24:34 -04:00
|
|
|
|
);
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> path to nested document
|
|
|
|
|
<2> filter
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
|
|
Note that you can ask not to cache the result using
|
|
|
|
|
`NestedFilterBuilder#cache(boolean)` method. See <<query-dsl-filters-caching>>.
|
|
|
|
|
|
|
|
|
|
[[query-dsl-filters-caching]]
|
|
|
|
|
=== Caching
|
|
|
|
|
|
|
|
|
|
By default, some filters are cached or not cached. You can have a fine
|
|
|
|
|
tuning control using `cache(boolean)` method when exists. For example:
|
|
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
FilterBuilder filter = andFilter(
|
|
|
|
|
rangeFilter("postDate").from("2010-03-01").to("2010-04-01"),
|
|
|
|
|
prefixFilter("name.second", "ba")
|
|
|
|
|
)
|
|
|
|
|
.cache(true); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
|
<1> force caching filter
|