2018-12-14 10:14:00 -05:00
|
|
|
[[query-dsl-intervals-query]]
|
|
|
|
=== Intervals query
|
2019-07-18 10:18:11 -04:00
|
|
|
++++
|
|
|
|
<titleabbrev>Intervals</titleabbrev>
|
|
|
|
++++
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
Returns documents based on the order and proximity of matching terms.
|
|
|
|
|
|
|
|
The `intervals` query uses *matching rules*, constructed from a small set of
|
|
|
|
definitions. Theses rules are then applied to terms from a specified `field`.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
The definitions produce sequences of minimal intervals that span terms in a
|
2019-08-09 08:53:25 -04:00
|
|
|
body of text. These intervals can be further combined and filtered by
|
2018-12-14 10:14:00 -05:00
|
|
|
parent sources.
|
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
|
|
|
|
[[intervals-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
The following `intervals` search returns documents containing `my
|
|
|
|
favorite food` immediately followed by `hot water` or `cold porridge` in the
|
|
|
|
`my_text` field.
|
|
|
|
|
|
|
|
This search would match a `my_text` value of `my favorite food is cold
|
|
|
|
porridge` but not `when it's cold my favorite food is porridge`.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-14 10:14:00 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"all_of" : {
|
|
|
|
"ordered" : true,
|
|
|
|
"intervals" : [
|
|
|
|
{
|
|
|
|
"match" : {
|
2019-08-09 08:53:25 -04:00
|
|
|
"query" : "my favorite food",
|
2018-12-14 10:14:00 -05:00
|
|
|
"max_gaps" : 0,
|
|
|
|
"ordered" : true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"any_of" : {
|
|
|
|
"intervals" : [
|
|
|
|
{ "match" : { "query" : "hot water" } },
|
|
|
|
{ "match" : { "query" : "cold porridge" } }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2019-08-09 08:53:25 -04:00
|
|
|
}
|
2018-12-14 10:14:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
[[intervals-top-level-params]]
|
|
|
|
==== Top-level parameters for `intervals`
|
|
|
|
[[intervals-rules]]
|
|
|
|
`<field>`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Required, rule object) Field you wish to search.
|
|
|
|
|
|
|
|
The value of this parameter is a rule object used to match documents
|
|
|
|
based on matching terms, order, and proximity.
|
|
|
|
|
|
|
|
Valid rules include:
|
|
|
|
|
|
|
|
* <<intervals-match,`match`>>
|
|
|
|
* <<intervals-prefix,`prefix`>>
|
|
|
|
* <<intervals-wildcard,`wildcard`>>
|
|
|
|
* <<intervals-all_of,`all_of`>>
|
|
|
|
* <<intervals-any_of,`any_of`>>
|
|
|
|
* <<interval_filter,`filter`>>
|
|
|
|
--
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
[[intervals-match]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `match` rule parameters
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
The `match` rule matches analyzed text.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
`query`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Required, string) Text you wish to find in the provided `<field>`.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`max_gaps`::
|
2019-08-09 08:53:25 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, integer) Maximum number of positions between the matching terms.
|
|
|
|
Terms further apart than this are not considered matches. Defaults to
|
|
|
|
`-1`.
|
|
|
|
|
|
|
|
If unspecified or set to `-1`, there is no width restriction on the match. If
|
|
|
|
set to `0`, the terms must appear next to each other.
|
|
|
|
--
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`ordered`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, boolean)
|
|
|
|
If `true`, matching terms must appear in their specified order. Defaults to
|
|
|
|
`false`.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`analyzer`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, string) <<analysis, analyzer>> used to analyze terms in the `query`.
|
|
|
|
Defaults to the top-level `<field>`'s analyzer.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`filter`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, <<interval_filter,interval filter>> rule object) An optional interval
|
|
|
|
filter.
|
|
|
|
|
2019-03-20 12:25:15 -04:00
|
|
|
`use_field`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, string) If specified, then match intervals from this
|
|
|
|
field rather than the top-level `<field>`. Terms are analyzed using the
|
|
|
|
search analyzer from this field. This allows you to search across multiple
|
|
|
|
fields as if they were all the same field; for example, you could index the same
|
|
|
|
text into stemmed and unstemmed fields, and search for stemmed tokens near
|
|
|
|
unstemmed ones.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-06-26 10:36:47 -04:00
|
|
|
[[intervals-prefix]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `prefix` rule parameters
|
2019-06-26 10:36:47 -04:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
The `prefix` rule matches terms that start with a specified set of characters.
|
|
|
|
This prefix can expand to match at most 128 terms. If the prefix matches more
|
|
|
|
than 128 terms, {es} returns an error. You can use the
|
|
|
|
<<index-prefixes,`index-prefixes`>> option in the field mapping to avoid this
|
|
|
|
limit.
|
2019-06-26 10:36:47 -04:00
|
|
|
|
|
|
|
`prefix`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Required, string) Beginning characters of terms you wish to find in the
|
|
|
|
top-level `<field>`.
|
|
|
|
|
2019-06-26 10:36:47 -04:00
|
|
|
`analyzer`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, string) <<analysis, analyzer>> used to normalize the `prefix`.
|
|
|
|
Defaults to the top-level `<field>`'s analyzer.
|
|
|
|
|
2019-06-26 10:36:47 -04:00
|
|
|
`use_field`::
|
2019-08-09 08:53:25 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string) If specified, then match intervals from this field rather
|
|
|
|
than the top-level `<field>`.
|
|
|
|
|
|
|
|
The `prefix` is normalized using the search analyzer from this field, unless a
|
|
|
|
separate `analyzer` is specified.
|
|
|
|
--
|
2019-06-26 10:36:47 -04:00
|
|
|
|
2019-06-28 08:58:06 -04:00
|
|
|
[[intervals-wildcard]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `wildcard` rule parameters
|
2019-06-28 08:58:06 -04:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
The `wildcard` rule matches terms using a wildcard pattern. This pattern can
|
|
|
|
expand to match at most 128 terms. If the pattern matches more than 128 terms,
|
|
|
|
{es} returns an error.
|
2019-06-28 08:58:06 -04:00
|
|
|
|
|
|
|
`pattern`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Required, string) Wildcard pattern used to find matching terms.
|
2019-06-28 08:58:06 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
This parameter supports two wildcard operators:
|
|
|
|
|
|
|
|
* `?`, which matches any single character
|
|
|
|
* `*`, which can match zero or more characters, including an empty one
|
|
|
|
|
|
|
|
WARNING: Avoid beginning patterns with `*` or `?`. This can increase
|
|
|
|
the iterations needed to find matching terms and slow search performance.
|
|
|
|
--
|
|
|
|
`analyzer`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, string) <<analysis, analyzer>> used to normalize the `pattern`.
|
|
|
|
Defaults to the top-level `<field>`'s analyzer.
|
|
|
|
|
2019-06-28 08:58:06 -04:00
|
|
|
`use_field`::
|
2019-08-09 08:53:25 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string) If specified, match intervals from this field rather than the
|
|
|
|
top-level `<field>`.
|
|
|
|
|
|
|
|
The `pattern` is normalized using the search analyzer from this field, unless
|
2019-06-28 08:58:06 -04:00
|
|
|
`analyzer` is specified separately.
|
2019-08-09 08:53:25 -04:00
|
|
|
--
|
2019-06-28 08:58:06 -04:00
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
[[intervals-all_of]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `all_of` rule parameters
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
The `all_of` rule returns matches that span a combination of other rules.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
`intervals`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Required, array of rule objects) An array of rules to combine. All rules must
|
|
|
|
produce a match in a document for the overall source to match.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`max_gaps`::
|
2019-08-09 08:53:25 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, integer) Maximum number of positions between the matching terms.
|
|
|
|
Intervals produced by the rules further apart than this are not considered
|
|
|
|
matches. Defaults to `-1`.
|
|
|
|
|
|
|
|
If unspecified or set to `-1`, there is no width restriction on the match. If
|
|
|
|
set to `0`, the terms must appear next to each other.
|
|
|
|
--
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`ordered`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, boolean) If `true`, intervals produced by the rules should appear in
|
|
|
|
the order in which they are specified. Defaults to `false`.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`filter`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, <<interval_filter,interval filter>> rule object) Rule used to filter
|
|
|
|
returned intervals.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
[[intervals-any_of]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `any_of` rule parameters
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
The `any_of` rule returns intervals produced by any of its sub-rules.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
`intervals`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Required, array of rule objects) An array of rules to match.
|
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
`filter`::
|
2019-08-09 08:53:25 -04:00
|
|
|
(Optional, <<interval_filter,interval filter>> rule object) Rule used to filter
|
|
|
|
returned intervals.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
[[interval_filter]]
|
2019-08-09 08:53:25 -04:00
|
|
|
==== `filter` rule parameters
|
|
|
|
|
|
|
|
The `filter` rule returns intervals based on a query. See
|
|
|
|
<<interval-filter-rule-ex>> for an example.
|
|
|
|
|
|
|
|
`after`::
|
|
|
|
(Optional, query object) Query used to return intervals that follow an interval
|
|
|
|
from the `filter` rule.
|
|
|
|
|
|
|
|
`before`::
|
|
|
|
(Optional, query object) Query used to return intervals that occur before an
|
|
|
|
interval from the `filter` rule.
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
`contained_by`::
|
|
|
|
(Optional, query object) Query used to return intervals contained by an interval
|
|
|
|
from the `filter` rule.
|
|
|
|
|
|
|
|
`containing`::
|
|
|
|
(Optional, query object) Query used to return intervals that contain an interval
|
|
|
|
from the `filter` rule.
|
|
|
|
|
|
|
|
`not_contained_by`::
|
|
|
|
(Optional, query object) Query used to return intervals that are *not*
|
|
|
|
contained by an interval from the `filter` rule.
|
|
|
|
|
|
|
|
`not_containing`::
|
|
|
|
(Optional, query object) Query used to return intervals that do *not* contain
|
|
|
|
an interval from the `filter` rule.
|
|
|
|
|
|
|
|
`not_overlapping`::
|
|
|
|
(Optional, query object) Query used to return intervals that do *not* overlap
|
|
|
|
with an interval from the `filter` rule.
|
|
|
|
|
|
|
|
`overlapping`::
|
|
|
|
(Optional, query object) Query used to return intervals that overlap with an
|
|
|
|
interval from the `filter` rule.
|
|
|
|
|
|
|
|
`script`::
|
|
|
|
(Optional, <<modules-scripting-using, script object>>) Script used to return
|
|
|
|
matching documents. This script must return a boolean value, `true` or `false`.
|
|
|
|
See <<interval-script-filter>> for an example.
|
|
|
|
|
|
|
|
|
|
|
|
[[intervals-query-note]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[interval-filter-rule-ex]]
|
|
|
|
===== Filter example
|
|
|
|
|
|
|
|
The following search includes a `filter` rule. It returns documents that have
|
|
|
|
the words `hot` and `porridge` within 10 positions of each other, without the
|
|
|
|
word `salty` in between:
|
2018-12-14 10:14:00 -05:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-14 10:14:00 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"match" : {
|
|
|
|
"query" : "hot porridge",
|
|
|
|
"max_gaps" : 10,
|
|
|
|
"filter" : {
|
|
|
|
"not_containing" : {
|
|
|
|
"match" : {
|
|
|
|
"query" : "salty"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2018-12-19 06:12:18 -05:00
|
|
|
[[interval-script-filter]]
|
2019-08-09 08:53:25 -04:00
|
|
|
===== Script filters
|
2018-12-19 06:12:18 -05:00
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
You can use a script to filter intervals based on their start position, end
|
|
|
|
position, and internal gap count. The following `filter` script uses the
|
|
|
|
`interval` variable with the `start`, `end`, and `gaps` methods:
|
2018-12-19 06:12:18 -05:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-19 06:12:18 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"match" : {
|
|
|
|
"query" : "hot porridge",
|
|
|
|
"filter" : {
|
|
|
|
"script" : {
|
|
|
|
"source" : "interval.start > 10 && interval.end < 20 && interval.gaps == 0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
|
2018-12-14 10:14:00 -05:00
|
|
|
[[interval-minimization]]
|
2019-08-09 08:53:25 -04:00
|
|
|
===== Minimization
|
2018-12-14 10:14:00 -05:00
|
|
|
|
|
|
|
The intervals query always minimizes intervals, to ensure that queries can
|
2019-08-09 08:53:25 -04:00
|
|
|
run in linear time. This can sometimes cause surprising results, particularly
|
|
|
|
when using `max_gaps` restrictions or filters. For example, take the
|
2018-12-14 10:14:00 -05:00
|
|
|
following query, searching for `salty` contained within the phrase `hot
|
|
|
|
porridge`:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-14 10:14:00 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"match" : {
|
|
|
|
"query" : "salty",
|
|
|
|
"filter" : {
|
|
|
|
"contained_by" : {
|
|
|
|
"match" : {
|
|
|
|
"query" : "hot porridge"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
This query does *not* match a document containing the phrase `hot porridge is
|
2018-12-14 10:14:00 -05:00
|
|
|
salty porridge`, because the intervals returned by the match query for `hot
|
|
|
|
porridge` only cover the initial two terms in this document, and these do not
|
|
|
|
overlap the intervals covering `salty`.
|
|
|
|
|
|
|
|
Another restriction to be aware of is the case of `any_of` rules that contain
|
2019-08-09 08:53:25 -04:00
|
|
|
sub-rules which overlap. In particular, if one of the rules is a strict
|
|
|
|
prefix of the other, then the longer rule can never match, which can
|
|
|
|
cause surprises when used in combination with `max_gaps`. Consider the
|
2018-12-14 10:14:00 -05:00
|
|
|
following query, searching for `the` immediately followed by `big` or `big bad`,
|
|
|
|
immediately followed by `wolf`:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-14 10:14:00 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"all_of" : {
|
|
|
|
"intervals" : [
|
|
|
|
{ "match" : { "query" : "the" } },
|
|
|
|
{ "any_of" : {
|
|
|
|
"intervals" : [
|
|
|
|
{ "match" : { "query" : "big" } },
|
|
|
|
{ "match" : { "query" : "big bad" } }
|
|
|
|
] } },
|
|
|
|
{ "match" : { "query" : "wolf" } }
|
|
|
|
],
|
|
|
|
"max_gaps" : 0,
|
|
|
|
"ordered" : true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-08-09 08:53:25 -04:00
|
|
|
Counter-intuitively, this query does *not* match the document `the big bad
|
|
|
|
wolf`, because the `any_of` rule in the middle only produces intervals
|
2018-12-14 10:14:00 -05:00
|
|
|
for `big` - intervals for `big bad` being longer than those for `big`, while
|
2019-08-09 08:53:25 -04:00
|
|
|
starting at the same position, and so being minimized away. In these cases,
|
2018-12-14 10:14:00 -05:00
|
|
|
it's better to rewrite the query so that all of the options are explicitly
|
|
|
|
laid out at the top level:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2018-12-14 10:14:00 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
POST _search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"intervals" : {
|
|
|
|
"my_text" : {
|
|
|
|
"any_of" : {
|
|
|
|
"intervals" : [
|
|
|
|
{ "match" : {
|
|
|
|
"query" : "the big bad wolf",
|
|
|
|
"ordered" : true,
|
|
|
|
"max_gaps" : 0 } },
|
|
|
|
{ "match" : {
|
|
|
|
"query" : "the big wolf",
|
|
|
|
"ordered" : true,
|
|
|
|
"max_gaps" : 0 } }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|