intervals-query.asciidoc 12 KB

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