123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- [[query-dsl-queries]]
- == Query DSL - Queries
- elasticsearch provides a full Java query dsl in a similar manner to the
- REST {ref}/query-dsl.html[Query DSL]. The factory for query
- builders is `QueryBuilders`. Once your query is ready, you can use the
- <<search,Search API>>.
- See also how to build <<query-dsl-filters,Filters>>
- To use `QueryBuilders` or `FilterBuilders` just import them in your class:
- [source,java]
- --------------------------------------------------
- import static org.elasticsearch.index.query.QueryBuilders.*;
- import static org.elasticsearch.index.query.FilterBuilders.*;
- --------------------------------------------------
- Note that you can easily print (aka debug) JSON generated queries using
- `toString()` method on `QueryBuilder` object.
- The `QueryBuilder` can then be used with any API that accepts a query,
- such as `count` and `search`.
- [[match]]
- === Match Query
- See {ref}/query-dsl-match-query.html[Match Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = matchQuery(
- "name", <1>
- "kimchy elasticsearch" <2>
- );
- --------------------------------------------------
- <1> field
- <2> text
- [[multimatch]]
- === MultiMatch Query
- See {ref}/query-dsl-multi-match-query.html[MultiMatch
- Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = multiMatchQuery(
- "kimchy elasticsearch", <1>
- "user", "message" <2>
- );
- --------------------------------------------------
- <1> text
- <2> fields
- [[bool]]
- === Boolean Query
- See {ref}/query-dsl-bool-query.html[Boolean Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = boolQuery()
- .must(termQuery("content", "test1")) <1>
- .must(termQuery("content", "test4")) <1>
- .mustNot(termQuery("content", "test2")) <2>
- .should(termQuery("content", "test3")); <3>
- --------------------------------------------------
- <1> must query
- <2> must not query
- <3> should query
- [[boosting]]
- === Boosting Query
- See {ref}/query-dsl-boosting-query.html[Boosting Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = boostingQuery()
- .positive(termQuery("name","kimchy")) <1>
- .negative(termQuery("name","dadoonet")) <2>
- .negativeBoost(0.2f); <3>
- --------------------------------------------------
- <1> query that will promote documents
- <2> query that will demote documents
- <3> negative boost
- [[ids]]
- === IDs Query
- See {ref}/query-dsl-ids-query.html[IDs Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = idsQuery().ids("1", "2"); <1>
- --------------------------------------------------
- <1> document ids
- [[constant-score]]
- === Constant Score Query
- See {ref}/query-dsl-constant-score-query.html[Constant
- Score Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = constantScoreQuery(
- termFilter("name","kimchy") <1>
- )
- .boost(2.0f); <2>
- --------------------------------------------------
- <1> you can use a filter
- <2> filter score
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = constantScoreQuery(
- termQuery("name","kimchy") <1>
- )
- .boost(2.0f); <2>
- --------------------------------------------------
- <1> you can use a query
- <2> query score
- [[dismax]]
- === Disjunction Max Query
- See {ref}/query-dsl-dis-max-query.html[Disjunction Max
- Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = disMaxQuery()
- .add(termQuery("name","kimchy")) <1>
- .add(termQuery("name","elasticsearch")) <2>
- .boost(1.2f) <3>
- .tieBreaker(0.7f); <4>
- --------------------------------------------------
- <1> add your queries
- <2> add your queries
- <3> boost factor
- <4> tie breaker
- [[flt]]
- === Fuzzy Like This (Field) Query (flt and flt_field)
- See:
- * {ref}/query-dsl-flt-query.html[Fuzzy Like This Query]
- * {ref}/query-dsl-flt-field-query.html[Fuzzy Like This Field Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = fuzzyLikeThisQuery("name.first", "name.last") <1>
- .likeText("text like this one") <2>
- .maxQueryTerms(12); <3>
- --------------------------------------------------
- <1> fields
- <2> text
- <3> max num of Terms in generated queries
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = fuzzyLikeThisFieldQuery("name.first") <1>
- .likeText("text like this one") <2>
- .maxQueryTerms(12); <3>
- --------------------------------------------------
- <1> field
- <2> text
- <3> max num of Terms in generated queries
- [[fuzzy]]
- === FuzzyQuery
- See {ref}/query-dsl-fuzzy-query.html[Fuzzy Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = fuzzyQuery(
- "name", <1>
- "kimzhy" <2>
- );
- --------------------------------------------------
- <1> field
- <2> text
- [[has-child-parent]]
- === Has Child / Has Parent
- See:
- * {ref}/query-dsl-has-child-query.html[Has Child Query]
- * {ref}/query-dsl-has-parent-query.html[Has Parent]
- [source,java]
- --------------------------------------------------
- // Has Child
- QueryBuilder qb = hasChildQuery(
- "blog_tag", <1>
- termQuery("tag","something") <2>
- );
- --------------------------------------------------
- <1> child type to query against
- <2> query (could be also a filter)
- // Has Parent
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = hasParentQuery(
- "blog", <1>
- termQuery("tag","something") <2>
- );
- --------------------------------------------------
- <1> parent type to query against
- <2> query (could be also a filter)
- [[match-all]]
- === MatchAll Query
- See {ref}/query-dsl-match-all-query.html[Match All
- Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = matchAllQuery();
- --------------------------------------------------
- [[mlt]]
- === More Like This Query (mlt)
- See:
- * {ref}/query-dsl-mlt-query.html[More Like This Query]
- [source,java]
- --------------------------------------------------
- // mlt Query
- QueryBuilder qb = moreLikeThisQuery("name.first", "name.last") <1>
- .likeText("text like this one") <2>
- .minTermFreq(1) <3>
- .maxQueryTerms(12); <4>
- --------------------------------------------------
- <1> fields
- <2> text
- <3> ignore threshold
- <4> max num of Terms in generated queries
- [[prefix]]
- === Prefix Query
- See {ref}/query-dsl-prefix-query.html[Prefix Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = prefixQuery(
- "brand", <1>
- "heine" <2>
- );
- --------------------------------------------------
- <1> field
- <2> prefix
- [[query-string]]
- === QueryString Query
- See {ref}/query-dsl-query-string-query.html[QueryString Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch"); <1>
- --------------------------------------------------
- <1> text
- [[java-range]]
- === Range Query
- See {ref}/query-dsl-range-query.html[Range Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = rangeQuery("price") <1>
- .from(5) <2>
- .to(10) <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`
- === Span Queries (first, near, not, or, term)
- See:
- * {ref}/query-dsl-span-term-query.html[Span Term Query]
- * {ref}/query-dsl-span-first-query.html[Span First Query]
- * {ref}/query-dsl-span-near-query.html[Span Near Query]
- * {ref}/query-dsl-span-not-query.html[Span Not Query]
- * {ref}/query-dsl-span-or-query.html[Span Or Query]
- [source,java]
- --------------------------------------------------
- // Span Term
- QueryBuilder qb = spanTermQuery(
- "user", <1>
- "kimchy" <2>
- );
- --------------------------------------------------
- <1> field
- <2> value
- [source,java]
- --------------------------------------------------
- // Span First
- QueryBuilder qb = spanFirstQuery(
- spanTermQuery("user", "kimchy"), <1>
- 3 <2>
- );
- --------------------------------------------------
- <1> query
- <2> max end position
- [source,java]
- --------------------------------------------------
- // Span Near
- QueryBuilder qb = spanNearQuery()
- .clause(spanTermQuery("field","value1")) <1>
- .clause(spanTermQuery("field","value2")) <1>
- .clause(spanTermQuery("field","value3")) <1>
- .slop(12) <2>
- .inOrder(false) <3>
- .collectPayloads(false); <4>
- --------------------------------------------------
- <1> span term queries
- <2> slop factor: the maximum number of intervening unmatched positions
- <3> whether matches are required to be in-order
- <4> collect payloads or not
- [source,java]
- --------------------------------------------------
- // Span Not
- QueryBuilder qb = spanNotQuery()
- .include(spanTermQuery("field","value1")) <1>
- .exclude(spanTermQuery("field","value2")); <2>
- --------------------------------------------------
- <1> span query whose matches are filtered
- <2> span query whose matches must not overlap those returned
- [source,java]
- --------------------------------------------------
- // Span Or
- QueryBuilder qb = spanOrQuery()
- .clause(spanTermQuery("field","value1")) <1>
- .clause(spanTermQuery("field","value2")) <1>
- .clause(spanTermQuery("field","value3")); <1>
- --------------------------------------------------
- <1> span term queries
- [[term]]
- === Term Query
- See {ref}/query-dsl-term-query.html[Term Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = termQuery(
- "name", <1>
- "kimchy"); <2>
- --------------------------------------------------
- <1> field
- <2> value
- [[java-terms]]
- === Terms Query
- See {ref}/query-dsl-terms-query.html[Terms Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = termsQuery("tags", <1>
- "blue", "pill") <2>
- .minimumMatch(1); <3>
- --------------------------------------------------
- <1> field
- <2> values
- <3> how many terms must match at least
- [[wildcard]]
- === Wildcard Query
- See {ref}/query-dsl-wildcard-query.html[Wildcard Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = wildcardQuery("user", "k?mc*");
- --------------------------------------------------
- [[nested]]
- === Nested Query
- See {ref}/query-dsl-nested-query.html[Nested Query]
- [source,java]
- --------------------------------------------------
- QueryBuilder qb = nestedQuery(
- "obj1", <1>
- boolQuery() <2>
- .must(matchQuery("obj1.name", "blue"))
- .must(rangeQuery("obj1.count").gt(5))
- )
- .scoreMode("avg"); <3>
- --------------------------------------------------
- <1> path to nested document
- <2> your query. Any fields referenced inside the query must use the complete path (fully qualified).
- <3> score mode could be `max`, `total`, `avg` (default) or `none`
- [[indices]]
- === Indices Query
- See {ref}/query-dsl-indices-query.html[Indices Query]
- [source,java]
- --------------------------------------------------
- // Using another query when no match for the main one
- QueryBuilder qb = indicesQuery(
- termQuery("tag", "wow"), <1>
- "index1", "index2" <2>
- )
- .noMatchQuery(termQuery("tag", "kow")); <3>
- --------------------------------------------------
- <1> query to be executed on selected indices
- <2> selected indices
- <3> query to be executed on non matching indices
- [source,java]
- --------------------------------------------------
- // Using all (match all) or none (match no documents)
- QueryBuilder qb = indicesQuery(
- termQuery("tag", "wow"), <1>
- "index1", "index2" <2>
- )
- .noMatchQuery("all"); <3>
- --------------------------------------------------
- <1> query to be executed on selected indices
- <2> selected indices
- <3> `none` (to match no documents), and `all` (to match all documents). Defaults to `all`.
- [[geo-shape]]
- === GeoShape Query
- See {ref}/query-dsl-geo-shape-query.html[GeoShape Query]
- 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>
- <version>0.4.1</version> <1>
- </dependency>
- <dependency>
- <groupId>com.vividsolutions</groupId>
- <artifactId>jts</artifactId>
- <version>1.13</version> <2>
- <exclusions>
- <exclusion>
- <groupId>xerces</groupId>
- <artifactId>xercesImpl</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- --------------------------------------------------
- <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]
- [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
- QueryBuilder qb = geoShapeQuery(
- "location", <1>
- new RectangleImpl(0,10,0,10,SpatialContext.GEO) <2>
- )
- .relation(ShapeRelation.WITHIN); <3>
- --------------------------------------------------
- <1> field
- <2> shape
- <3> relation
- [source,java]
- --------------------------------------------------
- // Intersect shapes
- QueryBuilder qb = geoShapeQuery(
- "location", <1>
- new PointImpl(0, 0, SpatialContext.GEO) <2>
- )
- .relation(ShapeRelation.INTERSECTS); <3>
- --------------------------------------------------
- <1> field
- <2> shape
- <3> relation
- [source,java]
- --------------------------------------------------
- // Using pre-indexed shapes
- QueryBuilder qb = geoShapeQuery(
- "location", <1>
- "New Zealand", <2>
- "countries") <3>
- .relation(ShapeRelation.DISJOINT); <4>
- --------------------------------------------------
- <1> field
- <2> indexed shape id
- <3> index type of the indexed shapes
- <4> relation
|