query-dsl-queries.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. [[query-dsl-queries]]
  2. == Query DSL - Queries
  3. elasticsearch provides a full Java query dsl in a similar manner to the
  4. REST {ref}/query-dsl.html[Query DSL]. The factory for query
  5. builders is `QueryBuilders`. Once your query is ready, you can use the
  6. <<search,Search API>>.
  7. See also how to build <<query-dsl-filters,Filters>>
  8. To use `QueryBuilders` or `FilterBuilders` just import them in your class:
  9. [source,java]
  10. --------------------------------------------------
  11. import static org.elasticsearch.index.query.QueryBuilders.*;
  12. import static org.elasticsearch.index.query.FilterBuilders.*;
  13. --------------------------------------------------
  14. Note that you can easily print (aka debug) JSON generated queries using
  15. `toString()` method on `QueryBuilder` object.
  16. The `QueryBuilder` can then be used with any API that accepts a query,
  17. such as `count` and `search`.
  18. [[match]]
  19. === Match Query
  20. See {ref}/query-dsl-match-query.html[Match Query]
  21. [source,java]
  22. --------------------------------------------------
  23. QueryBuilder qb = matchQuery(
  24. "name", <1>
  25. "kimchy elasticsearch" <2>
  26. );
  27. --------------------------------------------------
  28. <1> field
  29. <2> text
  30. [[multimatch]]
  31. === MultiMatch Query
  32. See {ref}/query-dsl-multi-match-query.html[MultiMatch
  33. Query]
  34. [source,java]
  35. --------------------------------------------------
  36. QueryBuilder qb = multiMatchQuery(
  37. "kimchy elasticsearch", <1>
  38. "user", "message" <2>
  39. );
  40. --------------------------------------------------
  41. <1> text
  42. <2> fields
  43. [[bool]]
  44. === Boolean Query
  45. See {ref}/query-dsl-bool-query.html[Boolean Query]
  46. [source,java]
  47. --------------------------------------------------
  48. QueryBuilder qb = boolQuery()
  49. .must(termQuery("content", "test1")) <1>
  50. .must(termQuery("content", "test4")) <1>
  51. .mustNot(termQuery("content", "test2")) <2>
  52. .should(termQuery("content", "test3")); <3>
  53. --------------------------------------------------
  54. <1> must query
  55. <2> must not query
  56. <3> should query
  57. [[boosting]]
  58. === Boosting Query
  59. See {ref}/query-dsl-boosting-query.html[Boosting Query]
  60. [source,java]
  61. --------------------------------------------------
  62. QueryBuilder qb = boostingQuery()
  63. .positive(termQuery("name","kimchy")) <1>
  64. .negative(termQuery("name","dadoonet")) <2>
  65. .negativeBoost(0.2f); <3>
  66. --------------------------------------------------
  67. <1> query that will promote documents
  68. <2> query that will demote documents
  69. <3> negative boost
  70. [[ids]]
  71. === IDs Query
  72. See {ref}/query-dsl-ids-query.html[IDs Query]
  73. [source,java]
  74. --------------------------------------------------
  75. QueryBuilder qb = idsQuery().ids("1", "2"); <1>
  76. --------------------------------------------------
  77. <1> document ids
  78. [[constant-score]]
  79. === Constant Score Query
  80. See {ref}/query-dsl-constant-score-query.html[Constant
  81. Score Query]
  82. [source,java]
  83. --------------------------------------------------
  84. QueryBuilder qb = constantScoreQuery(
  85. termFilter("name","kimchy") <1>
  86. )
  87. .boost(2.0f); <2>
  88. --------------------------------------------------
  89. <1> you can use a filter
  90. <2> filter score
  91. [source,java]
  92. --------------------------------------------------
  93. QueryBuilder qb = constantScoreQuery(
  94. termQuery("name","kimchy") <1>
  95. )
  96. .boost(2.0f); <2>
  97. --------------------------------------------------
  98. <1> you can use a query
  99. <2> query score
  100. [[dismax]]
  101. === Disjunction Max Query
  102. See {ref}/query-dsl-dis-max-query.html[Disjunction Max
  103. Query]
  104. [source,java]
  105. --------------------------------------------------
  106. QueryBuilder qb = disMaxQuery()
  107. .add(termQuery("name","kimchy")) <1>
  108. .add(termQuery("name","elasticsearch")) <2>
  109. .boost(1.2f) <3>
  110. .tieBreaker(0.7f); <4>
  111. --------------------------------------------------
  112. <1> add your queries
  113. <2> add your queries
  114. <3> boost factor
  115. <4> tie breaker
  116. [[flt]]
  117. === Fuzzy Like This (Field) Query (flt and flt_field)
  118. See:
  119. * {ref}/query-dsl-flt-query.html[Fuzzy Like This Query]
  120. * {ref}/query-dsl-flt-field-query.html[Fuzzy Like This Field Query]
  121. [source,java]
  122. --------------------------------------------------
  123. QueryBuilder qb = fuzzyLikeThisQuery("name.first", "name.last") <1>
  124. .likeText("text like this one") <2>
  125. .maxQueryTerms(12); <3>
  126. --------------------------------------------------
  127. <1> fields
  128. <2> text
  129. <3> max num of Terms in generated queries
  130. [source,java]
  131. --------------------------------------------------
  132. QueryBuilder qb = fuzzyLikeThisFieldQuery("name.first") <1>
  133. .likeText("text like this one") <2>
  134. .maxQueryTerms(12); <3>
  135. --------------------------------------------------
  136. <1> field
  137. <2> text
  138. <3> max num of Terms in generated queries
  139. [[fuzzy]]
  140. === FuzzyQuery
  141. See {ref}/query-dsl-fuzzy-query.html[Fuzzy Query]
  142. [source,java]
  143. --------------------------------------------------
  144. QueryBuilder qb = fuzzyQuery(
  145. "name", <1>
  146. "kimzhy" <2>
  147. );
  148. --------------------------------------------------
  149. <1> field
  150. <2> text
  151. [[has-child-parent]]
  152. === Has Child / Has Parent
  153. See:
  154. * {ref}/query-dsl-has-child-query.html[Has Child Query]
  155. * {ref}/query-dsl-has-parent-query.html[Has Parent]
  156. [source,java]
  157. --------------------------------------------------
  158. // Has Child
  159. QueryBuilder qb = hasChildQuery(
  160. "blog_tag", <1>
  161. termQuery("tag","something") <2>
  162. );
  163. --------------------------------------------------
  164. <1> child type to query against
  165. <2> query (could be also a filter)
  166. // Has Parent
  167. [source,java]
  168. --------------------------------------------------
  169. QueryBuilder qb = hasParentQuery(
  170. "blog", <1>
  171. termQuery("tag","something") <2>
  172. );
  173. --------------------------------------------------
  174. <1> parent type to query against
  175. <2> query (could be also a filter)
  176. [[match-all]]
  177. === MatchAll Query
  178. See {ref}/query-dsl-match-all-query.html[Match All
  179. Query]
  180. [source,java]
  181. --------------------------------------------------
  182. QueryBuilder qb = matchAllQuery();
  183. --------------------------------------------------
  184. [[mlt]]
  185. === More Like This Query (mlt)
  186. See:
  187. * {ref}/query-dsl-mlt-query.html[More Like This Query]
  188. [source,java]
  189. --------------------------------------------------
  190. // mlt Query
  191. QueryBuilder qb = moreLikeThisQuery("name.first", "name.last") <1>
  192. .likeText("text like this one") <2>
  193. .minTermFreq(1) <3>
  194. .maxQueryTerms(12); <4>
  195. --------------------------------------------------
  196. <1> fields
  197. <2> text
  198. <3> ignore threshold
  199. <4> max num of Terms in generated queries
  200. [[prefix]]
  201. === Prefix Query
  202. See {ref}/query-dsl-prefix-query.html[Prefix Query]
  203. [source,java]
  204. --------------------------------------------------
  205. QueryBuilder qb = prefixQuery(
  206. "brand", <1>
  207. "heine" <2>
  208. );
  209. --------------------------------------------------
  210. <1> field
  211. <2> prefix
  212. [[query-string]]
  213. === QueryString Query
  214. See {ref}/query-dsl-query-string-query.html[QueryString Query]
  215. [source,java]
  216. --------------------------------------------------
  217. QueryBuilder qb = queryStringQuery("+kimchy -elasticsearch"); <1>
  218. --------------------------------------------------
  219. <1> text
  220. [[java-range]]
  221. === Range Query
  222. See {ref}/query-dsl-range-query.html[Range Query]
  223. [source,java]
  224. --------------------------------------------------
  225. QueryBuilder qb = rangeQuery("price") <1>
  226. .from(5) <2>
  227. .to(10) <3>
  228. .includeLower(true) <4>
  229. .includeUpper(false); <5>
  230. --------------------------------------------------
  231. <1> field
  232. <2> from
  233. <3> to
  234. <4> include lower value means that `from` is `gt` when `false` or `gte` when `true`
  235. <5> include upper value means that `to` is `lt` when `false` or `lte` when `true`
  236. === Span Queries (first, near, not, or, term)
  237. See:
  238. * {ref}/query-dsl-span-term-query.html[Span Term Query]
  239. * {ref}/query-dsl-span-first-query.html[Span First Query]
  240. * {ref}/query-dsl-span-near-query.html[Span Near Query]
  241. * {ref}/query-dsl-span-not-query.html[Span Not Query]
  242. * {ref}/query-dsl-span-or-query.html[Span Or Query]
  243. [source,java]
  244. --------------------------------------------------
  245. // Span Term
  246. QueryBuilder qb = spanTermQuery(
  247. "user", <1>
  248. "kimchy" <2>
  249. );
  250. --------------------------------------------------
  251. <1> field
  252. <2> value
  253. [source,java]
  254. --------------------------------------------------
  255. // Span First
  256. QueryBuilder qb = spanFirstQuery(
  257. spanTermQuery("user", "kimchy"), <1>
  258. 3 <2>
  259. );
  260. --------------------------------------------------
  261. <1> query
  262. <2> max end position
  263. [source,java]
  264. --------------------------------------------------
  265. // Span Near
  266. QueryBuilder qb = spanNearQuery()
  267. .clause(spanTermQuery("field","value1")) <1>
  268. .clause(spanTermQuery("field","value2")) <1>
  269. .clause(spanTermQuery("field","value3")) <1>
  270. .slop(12) <2>
  271. .inOrder(false) <3>
  272. .collectPayloads(false); <4>
  273. --------------------------------------------------
  274. <1> span term queries
  275. <2> slop factor: the maximum number of intervening unmatched positions
  276. <3> whether matches are required to be in-order
  277. <4> collect payloads or not
  278. [source,java]
  279. --------------------------------------------------
  280. // Span Not
  281. QueryBuilder qb = spanNotQuery()
  282. .include(spanTermQuery("field","value1")) <1>
  283. .exclude(spanTermQuery("field","value2")); <2>
  284. --------------------------------------------------
  285. <1> span query whose matches are filtered
  286. <2> span query whose matches must not overlap those returned
  287. [source,java]
  288. --------------------------------------------------
  289. // Span Or
  290. QueryBuilder qb = spanOrQuery()
  291. .clause(spanTermQuery("field","value1")) <1>
  292. .clause(spanTermQuery("field","value2")) <1>
  293. .clause(spanTermQuery("field","value3")); <1>
  294. --------------------------------------------------
  295. <1> span term queries
  296. [[term]]
  297. === Term Query
  298. See {ref}/query-dsl-term-query.html[Term Query]
  299. [source,java]
  300. --------------------------------------------------
  301. QueryBuilder qb = termQuery(
  302. "name", <1>
  303. "kimchy"); <2>
  304. --------------------------------------------------
  305. <1> field
  306. <2> value
  307. [[java-terms]]
  308. === Terms Query
  309. See {ref}/query-dsl-terms-query.html[Terms Query]
  310. [source,java]
  311. --------------------------------------------------
  312. QueryBuilder qb = termsQuery("tags", <1>
  313. "blue", "pill") <2>
  314. .minimumMatch(1); <3>
  315. --------------------------------------------------
  316. <1> field
  317. <2> values
  318. <3> how many terms must match at least
  319. [[wildcard]]
  320. === Wildcard Query
  321. See {ref}/query-dsl-wildcard-query.html[Wildcard Query]
  322. [source,java]
  323. --------------------------------------------------
  324. QueryBuilder qb = wildcardQuery("user", "k?mc*");
  325. --------------------------------------------------
  326. [[nested]]
  327. === Nested Query
  328. See {ref}/query-dsl-nested-query.html[Nested Query]
  329. [source,java]
  330. --------------------------------------------------
  331. QueryBuilder qb = nestedQuery(
  332. "obj1", <1>
  333. boolQuery() <2>
  334. .must(matchQuery("obj1.name", "blue"))
  335. .must(rangeQuery("obj1.count").gt(5))
  336. )
  337. .scoreMode("avg"); <3>
  338. --------------------------------------------------
  339. <1> path to nested document
  340. <2> your query. Any fields referenced inside the query must use the complete path (fully qualified).
  341. <3> score mode could be `max`, `total`, `avg` (default) or `none`
  342. [[indices]]
  343. === Indices Query
  344. See {ref}/query-dsl-indices-query.html[Indices Query]
  345. [source,java]
  346. --------------------------------------------------
  347. // Using another query when no match for the main one
  348. QueryBuilder qb = indicesQuery(
  349. termQuery("tag", "wow"), <1>
  350. "index1", "index2" <2>
  351. )
  352. .noMatchQuery(termQuery("tag", "kow")); <3>
  353. --------------------------------------------------
  354. <1> query to be executed on selected indices
  355. <2> selected indices
  356. <3> query to be executed on non matching indices
  357. [source,java]
  358. --------------------------------------------------
  359. // Using all (match all) or none (match no documents)
  360. QueryBuilder qb = indicesQuery(
  361. termQuery("tag", "wow"), <1>
  362. "index1", "index2" <2>
  363. )
  364. .noMatchQuery("all"); <3>
  365. --------------------------------------------------
  366. <1> query to be executed on selected indices
  367. <2> selected indices
  368. <3> `none` (to match no documents), and `all` (to match all documents). Defaults to `all`.
  369. [[geo-shape]]
  370. === GeoShape Query
  371. See {ref}/query-dsl-geo-shape-query.html[GeoShape Query]
  372. Note: the `geo_shape` type uses `Spatial4J` and `JTS`, both of which are
  373. optional dependencies. Consequently you must add `Spatial4J` and `JTS`
  374. to your classpath in order to use this type:
  375. [source,xml]
  376. --------------------------------------------------
  377. <dependency>
  378. <groupId>com.spatial4j</groupId>
  379. <artifactId>spatial4j</artifactId>
  380. <version>0.4.1</version> <1>
  381. </dependency>
  382. <dependency>
  383. <groupId>com.vividsolutions</groupId>
  384. <artifactId>jts</artifactId>
  385. <version>1.13</version> <2>
  386. <exclusions>
  387. <exclusion>
  388. <groupId>xerces</groupId>
  389. <artifactId>xercesImpl</artifactId>
  390. </exclusion>
  391. </exclusions>
  392. </dependency>
  393. --------------------------------------------------
  394. <1> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.spatial4j%22%20AND%20a%3A%22spatial4j%22[Maven Central]
  395. <2> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22[Maven Central]
  396. [source,java]
  397. --------------------------------------------------
  398. // Import Spatial4J shapes
  399. import com.spatial4j.core.context.SpatialContext;
  400. import com.spatial4j.core.shape.Shape;
  401. import com.spatial4j.core.shape.impl.RectangleImpl;
  402. // Also import ShapeRelation
  403. import org.elasticsearch.common.geo.ShapeRelation;
  404. --------------------------------------------------
  405. [source,java]
  406. --------------------------------------------------
  407. // Shape within another
  408. QueryBuilder qb = geoShapeQuery(
  409. "location", <1>
  410. new RectangleImpl(0,10,0,10,SpatialContext.GEO) <2>
  411. )
  412. .relation(ShapeRelation.WITHIN); <3>
  413. --------------------------------------------------
  414. <1> field
  415. <2> shape
  416. <3> relation
  417. [source,java]
  418. --------------------------------------------------
  419. // Intersect shapes
  420. QueryBuilder qb = geoShapeQuery(
  421. "location", <1>
  422. new PointImpl(0, 0, SpatialContext.GEO) <2>
  423. )
  424. .relation(ShapeRelation.INTERSECTS); <3>
  425. --------------------------------------------------
  426. <1> field
  427. <2> shape
  428. <3> relation
  429. [source,java]
  430. --------------------------------------------------
  431. // Using pre-indexed shapes
  432. QueryBuilder qb = geoShapeQuery(
  433. "location", <1>
  434. "New Zealand", <2>
  435. "countries") <3>
  436. .relation(ShapeRelation.DISJOINT); <4>
  437. --------------------------------------------------
  438. <1> field
  439. <2> indexed shape id
  440. <3> index type of the indexed shapes
  441. <4> relation