intervals-query.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-all_of]]
  77. ==== `all_of`
  78. `all_of` returns returns matches that span a combination of other rules.
  79. [horizontal]
  80. `intervals`::
  81. An array of rules to combine. All rules must produce a match in a
  82. document for the overall source to match.
  83. `max_gaps`::
  84. Specify a maximum number of gaps between the rules. Combinations that match
  85. across a distance greater than this will not match. If set to -1 or
  86. unspecified, there is no restriction on this distance. If set to 0, then the
  87. matches produced by the rules must all appear immediately next to each other.
  88. `ordered`::
  89. Whether the intervals produced by the rules should appear in the order in
  90. which they are specified. Defaults to `false`
  91. `filter`::
  92. An optional <<interval_filter,interval filter>>
  93. [[intervals-any_of]]
  94. ==== `any_of`
  95. The `any_of` rule emits intervals produced by any of its sub-rules.
  96. [horizontal]
  97. `intervals`::
  98. An array of rules to match
  99. `filter`::
  100. An optional <<interval_filter,interval filter>>
  101. [[interval_filter]]
  102. ==== filters
  103. You can filter intervals produced by any rules by their relation to the
  104. intervals produced by another rule. The following example will return
  105. documents that have the words `hot` and `porridge` within 10 positions
  106. of each other, without the word `salty` in between:
  107. [source,js]
  108. --------------------------------------------------
  109. POST _search
  110. {
  111. "query": {
  112. "intervals" : {
  113. "my_text" : {
  114. "match" : {
  115. "query" : "hot porridge",
  116. "max_gaps" : 10,
  117. "filter" : {
  118. "not_containing" : {
  119. "match" : {
  120. "query" : "salty"
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. // CONSOLE
  131. The following filters are available:
  132. [horizontal]
  133. `containing`::
  134. Produces intervals that contain an interval from the filter rule
  135. `contained_by`::
  136. Produces intervals that are contained by an interval from the filter rule
  137. `not_containing`::
  138. Produces intervals that do not contain an interval from the filter rule
  139. `not_contained_by`::
  140. Produces intervals that are not contained by an interval from the filter rule
  141. `overlapping`::
  142. Produces intervals that overlap with an interval from the filter rule
  143. `not_overlapping`::
  144. Produces intervals that do not overlap with an interval from the filter rule
  145. `before`::
  146. Produces intervals that appear before an interval from the filter role
  147. `after`::
  148. Produces intervals that appear after an interval from the filter role
  149. [[interval-script-filter]]
  150. ==== Script filters
  151. You can also filter intervals based on their start position, end position and
  152. internal gap count, using a script. The script has access to an `interval`
  153. variable, with `start`, `end` and `gaps` methods:
  154. [source,js]
  155. --------------------------------------------------
  156. POST _search
  157. {
  158. "query": {
  159. "intervals" : {
  160. "my_text" : {
  161. "match" : {
  162. "query" : "hot porridge",
  163. "filter" : {
  164. "script" : {
  165. "source" : "interval.start > 10 && interval.end < 20 && interval.gaps == 0"
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. --------------------------------------------------
  174. // CONSOLE
  175. [[interval-minimization]]
  176. ==== Minimization
  177. The intervals query always minimizes intervals, to ensure that queries can
  178. run in linear time. This can sometimes cause surprising results, particularly
  179. when using `max_gaps` restrictions or filters. For example, take the
  180. following query, searching for `salty` contained within the phrase `hot
  181. porridge`:
  182. [source,js]
  183. --------------------------------------------------
  184. POST _search
  185. {
  186. "query": {
  187. "intervals" : {
  188. "my_text" : {
  189. "match" : {
  190. "query" : "salty",
  191. "filter" : {
  192. "contained_by" : {
  193. "match" : {
  194. "query" : "hot porridge"
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. --------------------------------------------------
  204. // CONSOLE
  205. This query will *not* match a document containing the phrase `hot porridge is
  206. salty porridge`, because the intervals returned by the match query for `hot
  207. porridge` only cover the initial two terms in this document, and these do not
  208. overlap the intervals covering `salty`.
  209. Another restriction to be aware of is the case of `any_of` rules that contain
  210. sub-rules which overlap. In particular, if one of the rules is a strict
  211. prefix of the other, then the longer rule will never be matched, which can
  212. cause surprises when used in combination with `max_gaps`. Consider the
  213. following query, searching for `the` immediately followed by `big` or `big bad`,
  214. immediately followed by `wolf`:
  215. [source,js]
  216. --------------------------------------------------
  217. POST _search
  218. {
  219. "query": {
  220. "intervals" : {
  221. "my_text" : {
  222. "all_of" : {
  223. "intervals" : [
  224. { "match" : { "query" : "the" } },
  225. { "any_of" : {
  226. "intervals" : [
  227. { "match" : { "query" : "big" } },
  228. { "match" : { "query" : "big bad" } }
  229. ] } },
  230. { "match" : { "query" : "wolf" } }
  231. ],
  232. "max_gaps" : 0,
  233. "ordered" : true
  234. }
  235. }
  236. }
  237. }
  238. }
  239. --------------------------------------------------
  240. // CONSOLE
  241. Counter-intuitively, this query *will not* match the document `the big bad
  242. wolf`, because the `any_of` rule in the middle will only produce intervals
  243. for `big` - intervals for `big bad` being longer than those for `big`, while
  244. starting at the same position, and so being minimized away. In these cases,
  245. it's better to rewrite the query so that all of the options are explicitly
  246. laid out at the top level:
  247. [source,js]
  248. --------------------------------------------------
  249. POST _search
  250. {
  251. "query": {
  252. "intervals" : {
  253. "my_text" : {
  254. "any_of" : {
  255. "intervals" : [
  256. { "match" : {
  257. "query" : "the big bad wolf",
  258. "ordered" : true,
  259. "max_gaps" : 0 } },
  260. { "match" : {
  261. "query" : "the big wolf",
  262. "ordered" : true,
  263. "max_gaps" : 0 } }
  264. ]
  265. }
  266. }
  267. }
  268. }
  269. }
  270. --------------------------------------------------
  271. // CONSOLE