intervals-query.asciidoc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. [[query-dsl-intervals-query]]
  2. === Intervals query
  3. An `intervals` query allows fine-grained control over the order and proximity of
  4. matching terms. Matching rules are constructed from a small set of definitions,
  5. and the rules are then applied to terms from a particular `field`.
  6. The definitions produce sequences of minimal intervals that span terms in a
  7. body of text. These intervals can be further combined and filtered by
  8. parent sources.
  9. The example below will search for the phrase `my favourite food` appearing
  10. before the terms `hot` and `water` or `cold` and `porridge` in any order, in
  11. the field `my_text`
  12. [source,js]
  13. --------------------------------------------------
  14. POST _search
  15. {
  16. "query": {
  17. "intervals" : {
  18. "my_text" : {
  19. "all_of" : {
  20. "ordered" : true,
  21. "intervals" : [
  22. {
  23. "match" : {
  24. "query" : "my favourite food",
  25. "max_gaps" : 0,
  26. "ordered" : true
  27. }
  28. },
  29. {
  30. "any_of" : {
  31. "intervals" : [
  32. { "match" : { "query" : "hot water" } },
  33. { "match" : { "query" : "cold porridge" } }
  34. ]
  35. }
  36. }
  37. ]
  38. },
  39. "_name" : "favourite_food"
  40. }
  41. }
  42. }
  43. }
  44. --------------------------------------------------
  45. // CONSOLE
  46. In the above example, the text `my favourite food is cold porridge` would
  47. match because the two intervals matching `my favourite food` and `cold
  48. porridge` appear in the correct order, but the text `when it's cold my
  49. favourite food is porridge` would not match, because the interval matching
  50. `cold porridge` starts before the interval matching `my favourite food`.
  51. [[intervals-match]]
  52. ==== `match`
  53. The `match` rule matches analyzed text, and takes the following parameters:
  54. [horizontal]
  55. `query`::
  56. The text to match.
  57. `max_gaps`::
  58. Specify a maximum number of gaps between the terms in the text. Terms that
  59. appear further apart than this will not match. If unspecified, or set to -1,
  60. then there is no width restriction on the match. If set to 0 then the terms
  61. must appear next to each other.
  62. `ordered`::
  63. Whether or not the terms must appear in their specified order. Defaults to
  64. `false`
  65. `analyzer`::
  66. Which analyzer should be used to analyze terms in the `query`. By
  67. default, the search analyzer of the top-level field will be used.
  68. `filter`::
  69. An optional <<interval_filter,interval filter>>
  70. `use_field`::
  71. If specified, then match intervals from this field rather than the top-level field.
  72. Terms will be analyzed using the search analyzer from this field. This allows you
  73. to search across multiple fields as if they were all the same field; for example,
  74. you could index the same text into stemmed and unstemmed fields, and search for
  75. stemmed tokens near unstemmed ones.
  76. [[intervals-prefix]]
  77. ==== `prefix`
  78. The `prefix` rule finds terms that start with a specified prefix. The prefix will
  79. expand to match at most 128 terms; if there are more matching terms in the index,
  80. then an error will be returned. To avoid this limit, enable the
  81. <<index-prefixes,`index-prefixes`>> option on the field being searched.
  82. [horizontal]
  83. `prefix`::
  84. Match terms starting with this prefix
  85. `analyzer`::
  86. Which analyzer should be used to normalize the `prefix`. By default, the
  87. search analyzer of the top-level field will be used.
  88. `use_field`::
  89. If specified, then match intervals from this field rather than the top-level field.
  90. The `prefix` will be normalized using the search analyzer from this field, unless
  91. `analyzer` is specified separately.
  92. [[intervals-all_of]]
  93. ==== `all_of`
  94. `all_of` returns returns matches that span a combination of other rules.
  95. [horizontal]
  96. `intervals`::
  97. An array of rules to combine. All rules must produce a match in a
  98. document for the overall source to match.
  99. `max_gaps`::
  100. Specify a maximum number of gaps between the rules. Combinations that match
  101. across a distance greater than this will not match. If set to -1 or
  102. unspecified, there is no restriction on this distance. If set to 0, then the
  103. matches produced by the rules must all appear immediately next to each other.
  104. `ordered`::
  105. Whether the intervals produced by the rules should appear in the order in
  106. which they are specified. Defaults to `false`
  107. `filter`::
  108. An optional <<interval_filter,interval filter>>
  109. [[intervals-any_of]]
  110. ==== `any_of`
  111. The `any_of` rule emits intervals produced by any of its sub-rules.
  112. [horizontal]
  113. `intervals`::
  114. An array of rules to match
  115. `filter`::
  116. An optional <<interval_filter,interval filter>>
  117. [[interval_filter]]
  118. ==== filters
  119. You can filter intervals produced by any rules by their relation to the
  120. intervals produced by another rule. The following example will return
  121. documents that have the words `hot` and `porridge` within 10 positions
  122. of each other, without the word `salty` in between:
  123. [source,js]
  124. --------------------------------------------------
  125. POST _search
  126. {
  127. "query": {
  128. "intervals" : {
  129. "my_text" : {
  130. "match" : {
  131. "query" : "hot porridge",
  132. "max_gaps" : 10,
  133. "filter" : {
  134. "not_containing" : {
  135. "match" : {
  136. "query" : "salty"
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. --------------------------------------------------
  146. // CONSOLE
  147. The following filters are available:
  148. [horizontal]
  149. `containing`::
  150. Produces intervals that contain an interval from the filter rule
  151. `contained_by`::
  152. Produces intervals that are contained by an interval from the filter rule
  153. `not_containing`::
  154. Produces intervals that do not contain an interval from the filter rule
  155. `not_contained_by`::
  156. Produces intervals that are not contained by an interval from the filter rule
  157. `overlapping`::
  158. Produces intervals that overlap with an interval from the filter rule
  159. `not_overlapping`::
  160. Produces intervals that do not overlap with an interval from the filter rule
  161. `before`::
  162. Produces intervals that appear before an interval from the filter role
  163. `after`::
  164. Produces intervals that appear after an interval from the filter role
  165. [[interval-script-filter]]
  166. ==== Script filters
  167. You can also filter intervals based on their start position, end position and
  168. internal gap count, using a script. The script has access to an `interval`
  169. variable, with `start`, `end` and `gaps` methods:
  170. [source,js]
  171. --------------------------------------------------
  172. POST _search
  173. {
  174. "query": {
  175. "intervals" : {
  176. "my_text" : {
  177. "match" : {
  178. "query" : "hot porridge",
  179. "filter" : {
  180. "script" : {
  181. "source" : "interval.start > 10 && interval.end < 20 && interval.gaps == 0"
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. --------------------------------------------------
  190. // CONSOLE
  191. [[interval-minimization]]
  192. ==== Minimization
  193. The intervals query always minimizes intervals, to ensure that queries can
  194. run in linear time. This can sometimes cause surprising results, particularly
  195. when using `max_gaps` restrictions or filters. For example, take the
  196. following query, searching for `salty` contained within the phrase `hot
  197. porridge`:
  198. [source,js]
  199. --------------------------------------------------
  200. POST _search
  201. {
  202. "query": {
  203. "intervals" : {
  204. "my_text" : {
  205. "match" : {
  206. "query" : "salty",
  207. "filter" : {
  208. "contained_by" : {
  209. "match" : {
  210. "query" : "hot porridge"
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. --------------------------------------------------
  220. // CONSOLE
  221. This query will *not* match a document containing the phrase `hot porridge is
  222. salty porridge`, because the intervals returned by the match query for `hot
  223. porridge` only cover the initial two terms in this document, and these do not
  224. overlap the intervals covering `salty`.
  225. Another restriction to be aware of is the case of `any_of` rules that contain
  226. sub-rules which overlap. In particular, if one of the rules is a strict
  227. prefix of the other, then the longer rule will never be matched, which can
  228. cause surprises when used in combination with `max_gaps`. Consider the
  229. following query, searching for `the` immediately followed by `big` or `big bad`,
  230. immediately followed by `wolf`:
  231. [source,js]
  232. --------------------------------------------------
  233. POST _search
  234. {
  235. "query": {
  236. "intervals" : {
  237. "my_text" : {
  238. "all_of" : {
  239. "intervals" : [
  240. { "match" : { "query" : "the" } },
  241. { "any_of" : {
  242. "intervals" : [
  243. { "match" : { "query" : "big" } },
  244. { "match" : { "query" : "big bad" } }
  245. ] } },
  246. { "match" : { "query" : "wolf" } }
  247. ],
  248. "max_gaps" : 0,
  249. "ordered" : true
  250. }
  251. }
  252. }
  253. }
  254. }
  255. --------------------------------------------------
  256. // CONSOLE
  257. Counter-intuitively, this query *will not* match the document `the big bad
  258. wolf`, because the `any_of` rule in the middle will only produce intervals
  259. for `big` - intervals for `big bad` being longer than those for `big`, while
  260. starting at the same position, and so being minimized away. In these cases,
  261. it's better to rewrite the query so that all of the options are explicitly
  262. laid out at the top level:
  263. [source,js]
  264. --------------------------------------------------
  265. POST _search
  266. {
  267. "query": {
  268. "intervals" : {
  269. "my_text" : {
  270. "any_of" : {
  271. "intervals" : [
  272. { "match" : {
  273. "query" : "the big bad wolf",
  274. "ordered" : true,
  275. "max_gaps" : 0 } },
  276. { "match" : {
  277. "query" : "the big wolf",
  278. "ordered" : true,
  279. "max_gaps" : 0 } }
  280. ]
  281. }
  282. }
  283. }
  284. }
  285. }
  286. --------------------------------------------------
  287. // CONSOLE