1
0

query-dsl-queries.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. * {ref}/query-dsl-mlt-field-query.html[More Like This Field Query]
  189. [source,java]
  190. --------------------------------------------------
  191. // mlt Query
  192. QueryBuilder qb = moreLikeThisQuery("name.first", "name.last") <1>
  193. .likeText("text like this one") <2>
  194. .minTermFreq(1) <3>
  195. .maxQueryTerms(12); <4>
  196. --------------------------------------------------
  197. <1> fields
  198. <2> text
  199. <3> ignore threshold
  200. <4> max num of Terms in generated queries
  201. [source,java]
  202. --------------------------------------------------
  203. // mlt_field Query
  204. QueryBuilder qb = moreLikeThisFieldQuery("name.first") <1>
  205. .likeText("text like this one") <2>
  206. .minTermFreq(1) <3>
  207. .maxQueryTerms(12); <4>
  208. --------------------------------------------------
  209. <1> field
  210. <2> text
  211. <3> ignore threshold
  212. <4> max num of Terms in generated queries
  213. [[prefix]]
  214. === Prefix Query
  215. See {ref}/query-dsl-prefix-query.html[Prefix Query]
  216. [source,java]
  217. --------------------------------------------------
  218. QueryBuilder qb = prefixQuery(
  219. "brand", <1>
  220. "heine" <2>
  221. );
  222. --------------------------------------------------
  223. <1> field
  224. <2> prefix
  225. [[query-string]]
  226. === QueryString Query
  227. See {ref}/query-dsl-query-string-query.html[QueryString Query]
  228. [source,java]
  229. --------------------------------------------------
  230. QueryBuilder qb = queryString("+kimchy -elasticsearch"); <1>
  231. --------------------------------------------------
  232. <1> text
  233. [[java-range]]
  234. === Range Query
  235. See {ref}/query-dsl-range-query.html[Range Query]
  236. [source,java]
  237. --------------------------------------------------
  238. QueryBuilder qb = rangeQuery("price") <1>
  239. .from(5) <2>
  240. .to(10) <3>
  241. .includeLower(true) <4>
  242. .includeUpper(false); <5>
  243. --------------------------------------------------
  244. <1> field
  245. <2> from
  246. <3> to
  247. <4> include lower value means that `from` is `gt` when `false` or `gte` when `true`
  248. <5> include upper value means that `to` is `lt` when `false` or `lte` when `true`
  249. === Span Queries (first, near, not, or, term)
  250. See:
  251. * {ref}/query-dsl-span-term-query.html[Span Term Query]
  252. * {ref}/query-dsl-span-first-query.html[Span First Query]
  253. * {ref}/query-dsl-span-near-query.html[Span Near Query]
  254. * {ref}/query-dsl-span-not-query.html[Span Not Query]
  255. * {ref}/query-dsl-span-or-query.html[Span Or Query]
  256. [source,java]
  257. --------------------------------------------------
  258. // Span Term
  259. QueryBuilder qb = spanTermQuery(
  260. "user", <1>
  261. "kimchy" <2>
  262. );
  263. --------------------------------------------------
  264. <1> field
  265. <2> value
  266. [source,java]
  267. --------------------------------------------------
  268. // Span First
  269. QueryBuilder qb = spanFirstQuery(
  270. spanTermQuery("user", "kimchy"), <1>
  271. 3 <2>
  272. );
  273. --------------------------------------------------
  274. <1> query
  275. <2> max end position
  276. [source,java]
  277. --------------------------------------------------
  278. // Span Near
  279. QueryBuilder qb = spanNearQuery()
  280. .clause(spanTermQuery("field","value1")) <1>
  281. .clause(spanTermQuery("field","value2")) <1>
  282. .clause(spanTermQuery("field","value3")) <1>
  283. .slop(12) <2>
  284. .inOrder(false) <3>
  285. .collectPayloads(false); <4>
  286. --------------------------------------------------
  287. <1> span term queries
  288. <2> slop factor: the maximum number of intervening unmatched positions
  289. <3> whether matches are required to be in-order
  290. <4> collect payloads or not
  291. [source,java]
  292. --------------------------------------------------
  293. // Span Not
  294. QueryBuilder qb = spanNotQuery()
  295. .include(spanTermQuery("field","value1")) <1>
  296. .exclude(spanTermQuery("field","value2")); <2>
  297. --------------------------------------------------
  298. <1> span query whose matches are filtered
  299. <2> span query whose matches must not overlap those returned
  300. [source,java]
  301. --------------------------------------------------
  302. // Span Or
  303. QueryBuilder qb = spanOrQuery()
  304. .clause(spanTermQuery("field","value1")) <1>
  305. .clause(spanTermQuery("field","value2")) <1>
  306. .clause(spanTermQuery("field","value3")); <1>
  307. --------------------------------------------------
  308. <1> span term queries
  309. [[term]]
  310. === Term Query
  311. See {ref}/query-dsl-term-query.html[Term Query]
  312. [source,java]
  313. --------------------------------------------------
  314. QueryBuilder qb = termQuery(
  315. "name", <1>
  316. "kimchy"); <2>
  317. --------------------------------------------------
  318. <1> field
  319. <2> value
  320. [[java-terms]]
  321. === Terms Query
  322. See {ref}/query-dsl-terms-query.html[Terms Query]
  323. [source,java]
  324. --------------------------------------------------
  325. QueryBuilder qb = termsQuery("tags", <1>
  326. "blue", "pill") <2>
  327. .minimumMatch(1); <3>
  328. --------------------------------------------------
  329. <1> field
  330. <2> values
  331. <3> how many terms must match at least
  332. [[top-children]]
  333. === Top Children Query
  334. See {ref}/query-dsl-top-children-query.html[Top Children Query]
  335. [source,java]
  336. --------------------------------------------------
  337. QueryBuilder qb = topChildrenQuery(
  338. "blog_tag", <1>
  339. termQuery("tag", "something") <2>
  340. )
  341. .score("max") <3>
  342. .factor(5) <4>
  343. .incrementalFactor(2); <5>
  344. --------------------------------------------------
  345. <1> field
  346. <2> query
  347. <3> `max`, `sum` or `avg`
  348. <4> how many hits are asked for in the first child query run (defaults to 5)
  349. <5> if not enough parents are found, and there are still more child docs to query, then the child search hits are
  350. expanded by multiplying by the incremental_factor (defaults to 2).
  351. [[wildcard]]
  352. === Wildcard Query
  353. See {ref}/query-dsl-wildcard-query.html[Wildcard Query]
  354. [source,java]
  355. --------------------------------------------------
  356. QueryBuilder qb = wildcardQuery("user", "k?mc*");
  357. --------------------------------------------------
  358. [[nested]]
  359. === Nested Query
  360. See {ref}/query-dsl-nested-query.html[Nested Query]
  361. [source,java]
  362. --------------------------------------------------
  363. QueryBuilder qb = nestedQuery(
  364. "obj1", <1>
  365. boolQuery() <2>
  366. .must(matchQuery("obj1.name", "blue"))
  367. .must(rangeQuery("obj1.count").gt(5))
  368. )
  369. .scoreMode("avg"); <3>
  370. --------------------------------------------------
  371. <1> path to nested document
  372. <2> your query. Any fields referenced inside the query must use the complete path (fully qualified).
  373. <3> score mode could be `max`, `total`, `avg` (default) or `none`
  374. [[indices]]
  375. === Indices Query
  376. See {ref}/query-dsl-indices-query.html[Indices Query]
  377. [source,java]
  378. --------------------------------------------------
  379. // Using another query when no match for the main one
  380. QueryBuilder qb = indicesQuery(
  381. termQuery("tag", "wow"), <1>
  382. "index1", "index2" <2>
  383. )
  384. .noMatchQuery(termQuery("tag", "kow")); <3>
  385. --------------------------------------------------
  386. <1> query to be executed on selected indices
  387. <2> selected indices
  388. <3> query to be executed on non matching indices
  389. [source,java]
  390. --------------------------------------------------
  391. // Using all (match all) or none (match no documents)
  392. QueryBuilder qb = indicesQuery(
  393. termQuery("tag", "wow"), <1>
  394. "index1", "index2" <2>
  395. )
  396. .noMatchQuery("all"); <3>
  397. --------------------------------------------------
  398. <1> query to be executed on selected indices
  399. <2> selected indices
  400. <3> `none` (to match no documents), and `all` (to match all documents). Defaults to `all`.
  401. [[geo-shape]]
  402. === GeoShape Query
  403. See {ref}/query-dsl-geo-shape-query.html[GeoShape Query]
  404. Note: the `geo_shape` type uses `Spatial4J` and `JTS`, both of which are
  405. optional dependencies. Consequently you must add `Spatial4J` and `JTS`
  406. to your classpath in order to use this type:
  407. [source,xml]
  408. --------------------------------------------------
  409. <dependency>
  410. <groupId>com.spatial4j</groupId>
  411. <artifactId>spatial4j</artifactId>
  412. <version>0.4.1</version> <1>
  413. </dependency>
  414. <dependency>
  415. <groupId>com.vividsolutions</groupId>
  416. <artifactId>jts</artifactId>
  417. <version>1.13</version> <2>
  418. <exclusions>
  419. <exclusion>
  420. <groupId>xerces</groupId>
  421. <artifactId>xercesImpl</artifactId>
  422. </exclusion>
  423. </exclusions>
  424. </dependency>
  425. --------------------------------------------------
  426. <1> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.spatial4j%22%20AND%20a%3A%22spatial4j%22[Maven Central]
  427. <2> check for updates in http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.vividsolutions%22%20AND%20a%3A%22jts%22[Maven Central]
  428. [source,java]
  429. --------------------------------------------------
  430. // Import Spatial4J shapes
  431. import com.spatial4j.core.context.SpatialContext;
  432. import com.spatial4j.core.shape.Shape;
  433. import com.spatial4j.core.shape.impl.RectangleImpl;
  434. // Also import ShapeRelation
  435. import org.elasticsearch.common.geo.ShapeRelation;
  436. --------------------------------------------------
  437. [source,java]
  438. --------------------------------------------------
  439. // Shape within another
  440. QueryBuilder qb = geoShapeQuery(
  441. "location", <1>
  442. new RectangleImpl(0,10,0,10,SpatialContext.GEO) <2>
  443. )
  444. .relation(ShapeRelation.WITHIN); <3>
  445. --------------------------------------------------
  446. <1> field
  447. <2> shape
  448. <3> relation
  449. [source,java]
  450. --------------------------------------------------
  451. // Intersect shapes
  452. QueryBuilder qb = geoShapeQuery(
  453. "location", <1>
  454. new PointImpl(0, 0, SpatialContext.GEO) <2>
  455. )
  456. .relation(ShapeRelation.INTERSECTS); <3>
  457. --------------------------------------------------
  458. <1> field
  459. <2> shape
  460. <3> relation
  461. [source,java]
  462. --------------------------------------------------
  463. // Using pre-indexed shapes
  464. QueryBuilder qb = geoShapeQuery(
  465. "location", <1>
  466. "New Zealand", <2>
  467. "countries") <3>
  468. .relation(ShapeRelation.DISJOINT); <4>
  469. --------------------------------------------------
  470. <1> field
  471. <2> indexed shape id
  472. <3> index type of the indexed shapes
  473. <4> relation