Intervals Usage

edit

An intervals query allows fine-grained control over the order and proximity of matching terms. Matching rules are constructed from a small set of definitions, and the rules are then applied to terms from a particular field.

The definitions produce sequences of minimal intervals that span terms in a body of text. These intervals can be further combined and filtered by parent sources.

Be sure to read the Elasticsearch documentation on Intervals query

Fluent DSL example

edit
q
.Intervals(c => c
    .Field(p => p.Description)
    .Name("named_query")
    .Boost(1.1)
    .AnyOf(any => any
        .Intervals(i => i
            .Match(m => m
                .Query("my favourite food")
                .MaxGaps(5)
                .Ordered()
                .Filter(f => f
                    .Containing(co => co
                        .Match(mm => mm
                            .Query("kimchy")
                        )
                    )
                )
            )
            .AllOf(all => all
                .Intervals(ii => ii
                    .Match(m => m
                        .Query("hot water")
                    )
                    .Match(m => m
                        .Query("cold porridge")
                    )
                )
                .Filter(f => f
                    .Script(s => s
                        .Source("interval.start > 0 && interval.end < 200")
                    )
                )
            )
        )
    )
)

Object Initializer syntax example

edit
new IntervalsQuery
{
    Field = Field<Project>(p => p.Description),
    Name = "named_query",
    Boost = 1.1,
    AnyOf = new IntervalsAnyOf
    {
        Intervals = new IntervalsContainer[]
        {
            new IntervalsMatch
            {
                Query = "my favourite food",
                MaxGaps = 5,
                Ordered = true,
                Filter = new IntervalsFilter
                {
                    Containing = new IntervalsMatch
                    {
                        Query = "kimchy"
                    }
                }
            },
            new IntervalsAllOf
            {
                Intervals = new IntervalsContainer[]
                {
                    new IntervalsMatch
                    {
                        Query = "hot water",
                    },
                    new IntervalsMatch
                    {
                        Query = "cold porridge",
                    },
                },
                Filter = new IntervalsFilter
                {
                    Script = new InlineScript("interval.start > 0 && interval.end < 200")
                }
            }
        }
    }
}

Example json output.

{
  "intervals": {
    "description": {
      "_name": "named_query",
      "boost": 1.1,
      "any_of": {
        "intervals": [
          {
            "match": {
              "query": "my favourite food",
              "max_gaps": 5,
              "ordered": true,
              "filter": {
                "containing": {
                  "match": {
                    "query": "kimchy"
                  }
                }
              }
            }
          },
          {
            "all_of": {
              "intervals": [
                {
                  "match": {
                    "query": "hot water"
                  }
                },
                {
                  "match": {
                    "query": "cold porridge"
                  }
                }
              ],
              "filter": {
                "script": {
                  "source": "interval.start > 0 && interval.end < 200"
                }
              }
            }
          }
        ]
      }
    }
  }
}

Prefix and Wildcard rules

edit

Prefix and Wildcard rules can be used to search for intervals that contain terms starting with a prefix, or match a pattern, respectively.

Only available in Elasticsearch 7.3.0+

Fluent DSL example

edit
q
.Intervals(c => c
    .Field(p => p.Description)
    .Name("named_query")
    .Boost(1.1)
    .AnyOf(any => any
        .Intervals(i => i
            .Wildcard(m => m
                .Pattern(IntervalsPrefix + "*")
            )
            .Prefix(m => m
                .Prefix(IntervalsPrefix)
            )
        )
    )
)

Object Initializer syntax example

edit
new IntervalsQuery
{
    Field = Field<Project>(p => p.Description),
    Name = "named_query",
    Boost = 1.1,
    AnyOf = new IntervalsAnyOf
    {
        Intervals = new IntervalsContainer[]
        {
            new IntervalsWildcard
            {
                Pattern = IntervalsPrefix + "*"
            },
            new IntervalsPrefix
            {
                Prefix = IntervalsPrefix
            }
        }
    }
}

Example json output.

{
  "intervals": {
    "description": {
      "_name": "named_query",
      "boost": 1.1,
      "any_of": {
        "intervals": [
          {
            "wildcard": {
              "pattern": "lorem*"
            }
          },
          {
            "prefix": {
              "prefix": "lorem"
            }
          }
        ]
      }
    }
  }
}

Fuzzy rules

edit

Fuzzy rules can be used to match terms that are similar to the provided term, within an edit distance defined by Fuzziness. If the fuzzy expansion matches more than 128 terms, Elasticsearch returns an error.

Only available in Elasticsearch 7.6.0+

Fluent DSL example

edit
q
.Intervals(c => c
    .Field(p => p.Description)
    .Name("named_query")
    .Boost(1.1)
    .Fuzzy(m => m
        .Term(IntervalsFuzzy)
        .Fuzziness(Fuzziness.Auto)
    )
)

Object Initializer syntax example

edit
new IntervalsQuery
{
    Field = Field<Project>(p => p.Description),
    Name = "named_query",
    Boost = 1.1,
    Fuzzy = new IntervalsFuzzy
    {
        Term = IntervalsFuzzy,
        Fuzziness = Fuzziness.Auto
    }
}

Example json output.

{
  "intervals": {
    "description": {
      "_name": "named_query",
      "boost": 1.1,
      "fuzzy": {
        "term": "lorem",
        "fuzziness": "AUTO"
      }
    }
  }
}