2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-queries]]
|
|
|
|
== Query DSL - Queries
|
|
|
|
|
|
|
|
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 query
|
2013-08-28 19:24:34 -04:00
|
|
|
builders is `QueryBuilders`. Once your query is ready, you can use the
|
|
|
|
<<search,Search API>>.
|
|
|
|
|
|
|
|
See also how to build <<query-dsl-filters,Filters>>
|
|
|
|
|
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 `QueryBuilder` object.
|
|
|
|
|
|
|
|
The `QueryBuilder` can then be used with any API that accepts a query,
|
|
|
|
such as `count` and `search`.
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[match]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Match Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-match-query.html[Match Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = matchQuery(
|
|
|
|
"name", <1>
|
|
|
|
"kimchy elasticsearch" <2>
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> text
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[multimatch]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== MultiMatch Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-multi-match-query.html[MultiMatch
|
2014-02-07 14:59:30 -05:00
|
|
|
Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = multiMatchQuery(
|
|
|
|
"kimchy elasticsearch", <1>
|
|
|
|
"user", "message" <2>
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> text
|
|
|
|
<2> fields
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[bool]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Boolean Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-bool-query.html[Boolean Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = boolQuery()
|
|
|
|
.must(termQuery("content", "test1")) <1>
|
|
|
|
.must(termQuery("content", "test4")) <1>
|
|
|
|
.mustNot(termQuery("content", "test2")) <2>
|
|
|
|
.should(termQuery("content", "test3")); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> must query
|
|
|
|
<2> must not query
|
|
|
|
<3> should 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
|
|
|
[[boosting]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Boosting Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-boosting-query.html[Boosting Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = boostingQuery()
|
|
|
|
.positive(termQuery("name","kimchy")) <1>
|
|
|
|
.negative(termQuery("name","dadoonet")) <2>
|
|
|
|
.negativeBoost(0.2f); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> query that will promote documents
|
|
|
|
<2> query that will demote documents
|
|
|
|
<3> negative boost
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[ids]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== IDs Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-ids-query.html[IDs Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = idsQuery().ids("1", "2"); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> document ids
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[constant-score]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Constant Score Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-constant-score-query.html[Constant
|
2014-02-07 14:59:30 -05:00
|
|
|
Score Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = constantScoreQuery(
|
|
|
|
termFilter("name","kimchy") <1>
|
|
|
|
)
|
|
|
|
.boost(2.0f); <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> you can use a filter
|
|
|
|
<2> filter score
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
QueryBuilder qb = constantScoreQuery(
|
|
|
|
termQuery("name","kimchy") <1>
|
|
|
|
)
|
|
|
|
.boost(2.0f); <2>
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> you can use a query
|
|
|
|
<2> query score
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[dismax]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Disjunction Max Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-dis-max-query.html[Disjunction Max
|
2014-02-07 14:59:30 -05:00
|
|
|
Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = disMaxQuery()
|
|
|
|
.add(termQuery("name","kimchy")) <1>
|
|
|
|
.add(termQuery("name","elasticsearch")) <2>
|
|
|
|
.boost(1.2f) <3>
|
|
|
|
.tieBreaker(0.7f); <4>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> add your queries
|
|
|
|
<2> add your queries
|
|
|
|
<3> boost factor
|
|
|
|
<4> tie breaker
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[flt]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Fuzzy Like This (Field) Query (flt and flt_field)
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See:
|
|
|
|
* {ref}/query-dsl-flt-query.html[Fuzzy Like This Query]
|
|
|
|
* {ref}/query-dsl-flt-field-query.html[Fuzzy Like This Field Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
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
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
QueryBuilder qb = fuzzyLikeThisFieldQuery("name.first") <1>
|
|
|
|
.likeText("text like this one") <2>
|
|
|
|
.maxQueryTerms(12); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> text
|
|
|
|
<3> max num of Terms in generated queries
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[fuzzy]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== FuzzyQuery
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-fuzzy-query.html[Fuzzy Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = fuzzyQuery(
|
|
|
|
"name", <1>
|
|
|
|
"kimzhy" <2>
|
|
|
|
);
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> text
|
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]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Has Child / Has Parent
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See:
|
2013-09-03 10:15:28 -04:00
|
|
|
* {ref}/query-dsl-has-child-query.html[Has Child Query]
|
|
|
|
* {ref}/query-dsl-has-parent-query.html[Has Parent]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// Has Child
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = hasChildQuery(
|
|
|
|
"blog_tag", <1>
|
|
|
|
termQuery("tag","something") <2>
|
|
|
|
);
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> child type to query against
|
|
|
|
<2> query (could be also a filter)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
// Has Parent
|
2014-09-23 06:39:57 -04:00
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
QueryBuilder qb = hasParentQuery(
|
|
|
|
"blog", <1>
|
|
|
|
termQuery("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> query (could be also a 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
|
|
|
[[match-all]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== MatchAll Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-match-all-query.html[Match All
|
2014-02-07 14:59:30 -05:00
|
|
|
Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = matchAllQuery();
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[mlt]]
|
|
|
|
=== More Like This (Field) Query (mlt and mlt_field)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See:
|
2013-09-03 10:15:28 -04:00
|
|
|
* {ref}/query-dsl-mlt-query.html[More Like This Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// mlt Query
|
2014-09-23 06:39:57 -04:00
|
|
|
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
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[prefix]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Prefix Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-prefix-query.html[Prefix Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = prefixQuery(
|
|
|
|
"brand", <1>
|
|
|
|
"heine" <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
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[query-string]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== QueryString Query
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See {ref}/query-dsl-query-string-query.html[QueryString Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-11-26 09:22:59 -05:00
|
|
|
QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch"); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> text
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-10-01 12:15:49 -04:00
|
|
|
[[java-range]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Range Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-range-query.html[Range Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = rangeQuery("price") <1>
|
|
|
|
.from(5) <2>
|
|
|
|
.to(10) <3>
|
|
|
|
.includeLower(true) <4>
|
|
|
|
.includeUpper(false); <5>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<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
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Span Queries (first, near, not, or, term)
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See:
|
2014-09-23 06:39:57 -04:00
|
|
|
* {ref}/query-dsl-span-term-query.html[Span Term Query]
|
2014-02-07 14:59:30 -05:00
|
|
|
* {ref}/query-dsl-span-first-query.html[Span First Query]
|
2013-09-03 10:15:28 -04:00
|
|
|
* {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]
|
2014-09-23 06:39:57 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// Span Term
|
|
|
|
QueryBuilder qb = spanTermQuery(
|
|
|
|
"user", <1>
|
|
|
|
"kimchy" <2>
|
|
|
|
);
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> field
|
|
|
|
<2> value
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// Span First
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = spanFirstQuery(
|
|
|
|
spanTermQuery("user", "kimchy"), <1>
|
|
|
|
3 <2>
|
|
|
|
);
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> query
|
|
|
|
<2> max end position
|
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
|
|
|
// Span Near
|
2014-09-23 06:39:57 -04:00
|
|
|
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
|
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
|
|
|
// Span Not
|
2014-09-23 06:39:57 -04:00
|
|
|
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
|
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
|
|
|
// Span Or
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = spanOrQuery()
|
|
|
|
.clause(spanTermQuery("field","value1")) <1>
|
|
|
|
.clause(spanTermQuery("field","value2")) <1>
|
|
|
|
.clause(spanTermQuery("field","value3")); <1>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> span term queries
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[term]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Term Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-term-query.html[Term Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = termQuery(
|
|
|
|
"name", <1>
|
|
|
|
"kimchy"); <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> value
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-10-01 12:15:49 -04:00
|
|
|
[[java-terms]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Terms Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-terms-query.html[Terms Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = termsQuery("tags", <1>
|
|
|
|
"blue", "pill") <2>
|
|
|
|
.minimumMatch(1); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> values
|
|
|
|
<3> how many terms must match at least
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[top-children]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Top Children Query
|
|
|
|
|
2014-02-07 14:59:30 -05:00
|
|
|
See {ref}/query-dsl-top-children-query.html[Top Children Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = topChildrenQuery(
|
|
|
|
"blog_tag", <1>
|
|
|
|
termQuery("tag", "something") <2>
|
2013-08-28 19:24:34 -04:00
|
|
|
)
|
2014-09-23 06:39:57 -04:00
|
|
|
.score("max") <3>
|
|
|
|
.factor(5) <4>
|
|
|
|
.incrementalFactor(2); <5>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<1> field
|
|
|
|
<2> query
|
|
|
|
<3> `max`, `sum` or `avg`
|
|
|
|
<4> how many hits are asked for in the first child query run (defaults to 5)
|
|
|
|
<5> if not enough parents are found, and there are still more child docs to query, then the child search hits are
|
|
|
|
expanded by multiplying by the incremental_factor (defaults to 2).
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[wildcard]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Wildcard Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-wildcard-query.html[Wildcard Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = wildcardQuery("user", "k?mc*");
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[nested]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Nested Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-nested-query.html[Nested Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = nestedQuery(
|
|
|
|
"obj1", <1>
|
|
|
|
boolQuery() <2>
|
|
|
|
.must(matchQuery("obj1.name", "blue"))
|
|
|
|
.must(rangeQuery("obj1.count").gt(5))
|
|
|
|
)
|
|
|
|
.scoreMode("avg"); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<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`
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[indices]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Indices Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-indices-query.html[Indices Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// Using another query when no match for the main one
|
2014-09-23 06:39:57 -04:00
|
|
|
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
|
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 all (match all) or none (match no documents)
|
2014-09-23 06:39:57 -04:00
|
|
|
QueryBuilder qb = indicesQuery(
|
|
|
|
termQuery("tag", "wow"), <1>
|
|
|
|
"index1", "index2" <2>
|
|
|
|
)
|
|
|
|
.noMatchQuery("all"); <3>
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
<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`.
|
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-shape]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== GeoShape Query
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/query-dsl-geo-shape-query.html[GeoShape Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2014-09-23 06:39:57 -04:00
|
|
|
[source,xml]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
<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
|
|
|
QueryBuilder qb = geoShapeQuery(
|
|
|
|
"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
|
|
|
QueryBuilder qb = geoShapeQuery(
|
|
|
|
"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
|
|
|
--------------------------------------------------
|
2014-09-23 06:39:57 -04:00
|
|
|
// 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
|