intervals-query.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. [[query-dsl-intervals-query]]
  2. === Intervals query
  3. ++++
  4. <titleabbrev>Intervals</titleabbrev>
  5. ++++
  6. Returns documents based on the order and proximity of matching terms.
  7. The `intervals` query uses *matching rules*, constructed from a small set of
  8. definitions. These rules are then applied to terms from a specified `field`.
  9. The definitions produce sequences of minimal intervals that span terms in a
  10. body of text. These intervals can be further combined and filtered by
  11. parent sources.
  12. [[intervals-query-ex-request]]
  13. ==== Example request
  14. The following `intervals` search returns documents containing `my
  15. favorite food` immediately followed by `hot water` or `cold porridge` in the
  16. `my_text` field.
  17. This search would match a `my_text` value of `my favorite food is cold
  18. porridge` but not `when it's cold my favorite food is porridge`.
  19. [source,console]
  20. --------------------------------------------------
  21. POST _search
  22. {
  23. "query": {
  24. "intervals" : {
  25. "my_text" : {
  26. "all_of" : {
  27. "ordered" : true,
  28. "intervals" : [
  29. {
  30. "match" : {
  31. "query" : "my favorite food",
  32. "max_gaps" : 0,
  33. "ordered" : true
  34. }
  35. },
  36. {
  37. "any_of" : {
  38. "intervals" : [
  39. { "match" : { "query" : "hot water" } },
  40. { "match" : { "query" : "cold porridge" } }
  41. ]
  42. }
  43. }
  44. ]
  45. }
  46. }
  47. }
  48. }
  49. }
  50. --------------------------------------------------
  51. [[intervals-top-level-params]]
  52. ==== Top-level parameters for `intervals`
  53. [[intervals-rules]]
  54. `<field>`::
  55. +
  56. --
  57. (Required, rule object) Field you wish to search.
  58. The value of this parameter is a rule object used to match documents
  59. based on matching terms, order, and proximity.
  60. Valid rules include:
  61. * <<intervals-match,`match`>>
  62. * <<intervals-prefix,`prefix`>>
  63. * <<intervals-wildcard,`wildcard`>>
  64. * <<intervals-all_of,`all_of`>>
  65. * <<intervals-any_of,`any_of`>>
  66. --
  67. [[intervals-match]]
  68. ==== `match` rule parameters
  69. The `match` rule matches analyzed text.
  70. `query`::
  71. (Required, string) Text you wish to find in the provided `<field>`.
  72. `max_gaps`::
  73. +
  74. --
  75. (Optional, integer) Maximum number of positions between the matching terms.
  76. Terms further apart than this are not considered matches. Defaults to
  77. `-1`.
  78. If unspecified or set to `-1`, there is no width restriction on the match. If
  79. set to `0`, the terms must appear next to each other.
  80. --
  81. `ordered`::
  82. (Optional, boolean)
  83. If `true`, matching terms must appear in their specified order. Defaults to
  84. `false`.
  85. `analyzer`::
  86. (Optional, string) <<analysis, analyzer>> used to analyze terms in the `query`.
  87. Defaults to the top-level `<field>`'s analyzer.
  88. `filter`::
  89. (Optional, <<interval_filter,interval filter>> rule object) An optional interval
  90. filter.
  91. `use_field`::
  92. (Optional, string) If specified, then match intervals from this
  93. field rather than the top-level `<field>`. Terms are analyzed using the
  94. search analyzer from this field. This allows you to search across multiple
  95. fields as if they were all the same field; for example, you could index the same
  96. text into stemmed and unstemmed fields, and search for stemmed tokens near
  97. unstemmed ones.
  98. [[intervals-prefix]]
  99. ==== `prefix` rule parameters
  100. The `prefix` rule matches terms that start with a specified set of characters.
  101. This prefix can expand to match at most 128 terms. If the prefix matches more
  102. than 128 terms, {es} returns an error. You can use the
  103. <<index-prefixes,`index-prefixes`>> option in the field mapping to avoid this
  104. limit.
  105. `prefix`::
  106. (Required, string) Beginning characters of terms you wish to find in the
  107. top-level `<field>`.
  108. `analyzer`::
  109. (Optional, string) <<analysis, analyzer>> used to normalize the `prefix`.
  110. Defaults to the top-level `<field>`'s analyzer.
  111. `use_field`::
  112. +
  113. --
  114. (Optional, string) If specified, then match intervals from this field rather
  115. than the top-level `<field>`.
  116. The `prefix` is normalized using the search analyzer from this field, unless a
  117. separate `analyzer` is specified.
  118. --
  119. [[intervals-wildcard]]
  120. ==== `wildcard` rule parameters
  121. The `wildcard` rule matches terms using a wildcard pattern. This pattern can
  122. expand to match at most 128 terms. If the pattern matches more than 128 terms,
  123. {es} returns an error.
  124. `pattern`::
  125. (Required, string) Wildcard pattern used to find matching terms.
  126. +
  127. --
  128. This parameter supports two wildcard operators:
  129. * `?`, which matches any single character
  130. * `*`, which can match zero or more characters, including an empty one
  131. WARNING: Avoid beginning patterns with `*` or `?`. This can increase
  132. the iterations needed to find matching terms and slow search performance.
  133. --
  134. `analyzer`::
  135. (Optional, string) <<analysis, analyzer>> used to normalize the `pattern`.
  136. Defaults to the top-level `<field>`'s analyzer.
  137. `use_field`::
  138. +
  139. --
  140. (Optional, string) If specified, match intervals from this field rather than the
  141. top-level `<field>`.
  142. The `pattern` is normalized using the search analyzer from this field, unless
  143. `analyzer` is specified separately.
  144. --
  145. [[intervals-all_of]]
  146. ==== `all_of` rule parameters
  147. The `all_of` rule returns matches that span a combination of other rules.
  148. `intervals`::
  149. (Required, array of rule objects) An array of rules to combine. All rules must
  150. produce a match in a document for the overall source to match.
  151. `max_gaps`::
  152. +
  153. --
  154. (Optional, integer) Maximum number of positions between the matching terms.
  155. Intervals produced by the rules further apart than this are not considered
  156. matches. Defaults to `-1`.
  157. If unspecified or set to `-1`, there is no width restriction on the match. If
  158. set to `0`, the terms must appear next to each other.
  159. --
  160. `ordered`::
  161. (Optional, boolean) If `true`, intervals produced by the rules should appear in
  162. the order in which they are specified. Defaults to `false`.
  163. `filter`::
  164. (Optional, <<interval_filter,interval filter>> rule object) Rule used to filter
  165. returned intervals.
  166. [[intervals-any_of]]
  167. ==== `any_of` rule parameters
  168. The `any_of` rule returns intervals produced by any of its sub-rules.
  169. `intervals`::
  170. (Required, array of rule objects) An array of rules to match.
  171. `filter`::
  172. (Optional, <<interval_filter,interval filter>> rule object) Rule used to filter
  173. returned intervals.
  174. [[interval_filter]]
  175. ==== `filter` rule parameters
  176. The `filter` rule returns intervals based on a query. See
  177. <<interval-filter-rule-ex>> for an example.
  178. `after`::
  179. (Optional, query object) Query used to return intervals that follow an interval
  180. from the `filter` rule.
  181. `before`::
  182. (Optional, query object) Query used to return intervals that occur before an
  183. interval from the `filter` rule.
  184. `contained_by`::
  185. (Optional, query object) Query used to return intervals contained by an interval
  186. from the `filter` rule.
  187. `containing`::
  188. (Optional, query object) Query used to return intervals that contain an interval
  189. from the `filter` rule.
  190. `not_contained_by`::
  191. (Optional, query object) Query used to return intervals that are *not*
  192. contained by an interval from the `filter` rule.
  193. `not_containing`::
  194. (Optional, query object) Query used to return intervals that do *not* contain
  195. an interval from the `filter` rule.
  196. `not_overlapping`::
  197. (Optional, query object) Query used to return intervals that do *not* overlap
  198. with an interval from the `filter` rule.
  199. `overlapping`::
  200. (Optional, query object) Query used to return intervals that overlap with an
  201. interval from the `filter` rule.
  202. `script`::
  203. (Optional, <<modules-scripting-using, script object>>) Script used to return
  204. matching documents. This script must return a boolean value, `true` or `false`.
  205. See <<interval-script-filter>> for an example.
  206. [[intervals-query-note]]
  207. ==== Notes
  208. [[interval-filter-rule-ex]]
  209. ===== Filter example
  210. The following search includes a `filter` rule. It returns documents that have
  211. the words `hot` and `porridge` within 10 positions of each other, without the
  212. word `salty` in between:
  213. [source,console]
  214. --------------------------------------------------
  215. POST _search
  216. {
  217. "query": {
  218. "intervals" : {
  219. "my_text" : {
  220. "match" : {
  221. "query" : "hot porridge",
  222. "max_gaps" : 10,
  223. "filter" : {
  224. "not_containing" : {
  225. "match" : {
  226. "query" : "salty"
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. [[interval-script-filter]]
  237. ===== Script filters
  238. You can use a script to filter intervals based on their start position, end
  239. position, and internal gap count. The following `filter` script uses the
  240. `interval` variable with the `start`, `end`, and `gaps` methods:
  241. [source,console]
  242. --------------------------------------------------
  243. POST _search
  244. {
  245. "query": {
  246. "intervals" : {
  247. "my_text" : {
  248. "match" : {
  249. "query" : "hot porridge",
  250. "filter" : {
  251. "script" : {
  252. "source" : "interval.start > 10 && interval.end < 20 && interval.gaps == 0"
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. --------------------------------------------------
  261. [[interval-minimization]]
  262. ===== Minimization
  263. The intervals query always minimizes intervals, to ensure that queries can
  264. run in linear time. This can sometimes cause surprising results, particularly
  265. when using `max_gaps` restrictions or filters. For example, take the
  266. following query, searching for `salty` contained within the phrase `hot
  267. porridge`:
  268. [source,console]
  269. --------------------------------------------------
  270. POST _search
  271. {
  272. "query": {
  273. "intervals" : {
  274. "my_text" : {
  275. "match" : {
  276. "query" : "salty",
  277. "filter" : {
  278. "contained_by" : {
  279. "match" : {
  280. "query" : "hot porridge"
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. --------------------------------------------------
  290. This query does *not* match a document containing the phrase `hot porridge is
  291. salty porridge`, because the intervals returned by the match query for `hot
  292. porridge` only cover the initial two terms in this document, and these do not
  293. overlap the intervals covering `salty`.
  294. Another restriction to be aware of is the case of `any_of` rules that contain
  295. sub-rules which overlap. In particular, if one of the rules is a strict
  296. prefix of the other, then the longer rule can never match, which can
  297. cause surprises when used in combination with `max_gaps`. Consider the
  298. following query, searching for `the` immediately followed by `big` or `big bad`,
  299. immediately followed by `wolf`:
  300. [source,console]
  301. --------------------------------------------------
  302. POST _search
  303. {
  304. "query": {
  305. "intervals" : {
  306. "my_text" : {
  307. "all_of" : {
  308. "intervals" : [
  309. { "match" : { "query" : "the" } },
  310. { "any_of" : {
  311. "intervals" : [
  312. { "match" : { "query" : "big" } },
  313. { "match" : { "query" : "big bad" } }
  314. ] } },
  315. { "match" : { "query" : "wolf" } }
  316. ],
  317. "max_gaps" : 0,
  318. "ordered" : true
  319. }
  320. }
  321. }
  322. }
  323. }
  324. --------------------------------------------------
  325. Counter-intuitively, this query does *not* match the document `the big bad
  326. wolf`, because the `any_of` rule in the middle only produces intervals
  327. for `big` - intervals for `big bad` being longer than those for `big`, while
  328. starting at the same position, and so being minimized away. In these cases,
  329. it's better to rewrite the query so that all of the options are explicitly
  330. laid out at the top level:
  331. [source,console]
  332. --------------------------------------------------
  333. POST _search
  334. {
  335. "query": {
  336. "intervals" : {
  337. "my_text" : {
  338. "any_of" : {
  339. "intervals" : [
  340. { "match" : {
  341. "query" : "the big bad wolf",
  342. "ordered" : true,
  343. "max_gaps" : 0 } },
  344. { "match" : {
  345. "query" : "the big wolf",
  346. "ordered" : true,
  347. "max_gaps" : 0 } }
  348. ]
  349. }
  350. }
  351. }
  352. }
  353. }
  354. --------------------------------------------------