query-dsl-queries.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 (Field) Query (mlt and mlt_field)
  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. [[top-children]]
  320. === Top Children Query
  321. See {ref}/query-dsl-top-children-query.html[Top Children Query]
  322. [source,java]
  323. --------------------------------------------------
  324. QueryBuilder qb = topChildrenQuery(
  325. "blog_tag", <1>
  326. termQuery("tag", "something") <2>
  327. )
  328. .score("max") <3>
  329. .factor(5) <4>
  330. .incrementalFactor(2); <5>
  331. --------------------------------------------------
  332. <1> field
  333. <2> query
  334. <3> `max`, `sum` or `avg`
  335. <4> how many hits are asked for in the first child query run (defaults to 5)
  336. <5> if not enough parents are found, and there are still more child docs to query, then the child search hits are
  337. expanded by multiplying by the incremental_factor (defaults to 2).
  338. [[wildcard]]
  339. === Wildcard Query
  340. See {ref}/query-dsl-wildcard-query.html[Wildcard Query]
  341. [source,java]
  342. --------------------------------------------------
  343. QueryBuilder qb = wildcardQuery("user", "k?mc*");
  344. --------------------------------------------------
  345. [[nested]]
  346. === Nested Query
  347. See {ref}/query-dsl-nested-query.html[Nested Query]
  348. [source,java]
  349. --------------------------------------------------
  350. QueryBuilder qb = nestedQuery(
  351. "obj1", <1>
  352. boolQuery() <2>
  353. .must(matchQuery("obj1.name", "blue"))
  354. .must(rangeQuery("obj1.count").gt(5))
  355. )
  356. .scoreMode("avg"); <3>
  357. --------------------------------------------------
  358. <1> path to nested document
  359. <2> your query. Any fields referenced inside the query must use the complete path (fully qualified).
  360. <3> score mode could be `max`, `total`, `avg` (default) or `none`
  361. [[indices]]
  362. === Indices Query
  363. See {ref}/query-dsl-indices-query.html[Indices Query]
  364. [source,java]
  365. --------------------------------------------------
  366. // Using another query when no match for the main one
  367. QueryBuilder qb = indicesQuery(
  368. termQuery("tag", "wow"), <1>
  369. "index1", "index2" <2>
  370. )
  371. .noMatchQuery(termQuery("tag", "kow")); <3>
  372. --------------------------------------------------
  373. <1> query to be executed on selected indices
  374. <2> selected indices
  375. <3> query to be executed on non matching indices
  376. [source,java]
  377. --------------------------------------------------
  378. // Using all (match all) or none (match no documents)
  379. QueryBuilder qb = indicesQuery(
  380. termQuery("tag", "wow"), <1>
  381. "index1", "index2" <2>
  382. )
  383. .noMatchQuery("all"); <3>
  384. --------------------------------------------------
  385. <1> query to be executed on selected indices
  386. <2> selected indices
  387. <3> `none` (to match no documents), and `all` (to match all documents). Defaults to `all`.
  388. [[geo-shape]]
  389. === GeoShape Query
  390. See {ref}/query-dsl-geo-shape-query.html[GeoShape Query]
  391. Note: the `geo_shape` type uses `Spatial4J` and `JTS`, both of which are
  392. optional dependencies. Consequently you must add `Spatial4J` and `JTS`
  393. to your classpath in order to use this type:
  394. [source,xml]
  395. --------------------------------------------------
  396. <dependency>
  397. <groupId>com.spatial4j</groupId>
  398. <artifactId>spatial4j</artifactId>
  399. <version>0.4.1</version> <1>
  400. </dependency>
  401. <dependency>
  402. <groupId>com.vividsolutions</groupId>
  403. <artifactId>jts</artifactId>
  404. <version>1.13</version> <2>
  405. <exclusions>
  406. <exclusion>
  407. <groupId>xerces</groupId>
  408. <artifactId>xercesImpl</artifactId>
  409. </exclusion>
  410. </exclusions>
  411. </dependency>
  412. --------------------------------------------------
  413. <1> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.spatial4j%22%20AND%20a%3A%22spatial4j%22[Maven Central]
  414. <2> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22[Maven Central]
  415. [source,java]
  416. --------------------------------------------------
  417. // Import Spatial4J shapes
  418. import com.spatial4j.core.context.SpatialContext;
  419. import com.spatial4j.core.shape.Shape;
  420. import com.spatial4j.core.shape.impl.RectangleImpl;
  421. // Also import ShapeRelation
  422. import org.elasticsearch.common.geo.ShapeRelation;
  423. --------------------------------------------------
  424. [source,java]
  425. --------------------------------------------------
  426. // Shape within another
  427. QueryBuilder qb = geoShapeQuery(
  428. "location", <1>
  429. new RectangleImpl(0,10,0,10,SpatialContext.GEO) <2>
  430. )
  431. .relation(ShapeRelation.WITHIN); <3>
  432. --------------------------------------------------
  433. <1> field
  434. <2> shape
  435. <3> relation
  436. [source,java]
  437. --------------------------------------------------
  438. // Intersect shapes
  439. QueryBuilder qb = geoShapeQuery(
  440. "location", <1>
  441. new PointImpl(0, 0, SpatialContext.GEO) <2>
  442. )
  443. .relation(ShapeRelation.INTERSECTS); <3>
  444. --------------------------------------------------
  445. <1> field
  446. <2> shape
  447. <3> relation
  448. [source,java]
  449. --------------------------------------------------
  450. // Using pre-indexed shapes
  451. QueryBuilder qb = geoShapeQuery(
  452. "location", <1>
  453. "New Zealand", <2>
  454. "countries") <3>
  455. .relation(ShapeRelation.DISJOINT); <4>
  456. --------------------------------------------------
  457. <1> field
  458. <2> indexed shape id
  459. <3> index type of the indexed shapes
  460. <4> relation