What the difference between span term query to term query?

2k Views Asked by At

I saw the section in Elasticsearch documentation (6.8) about span queries,
The request and result is similar to some of the Term level queries, but it mention the the span is "low-level positional" which smell like better preference (maybe I wrong).
The question is if I want to use pure term query ( for example match the user with the name), there is a big difference in the performance or in other thing that I prefer one option on another?


Examples:
GET /_search
{
    "query": {
        "span_term" : { "user" : "kimchy" }
    }
}

VS

POST _search
{
  "query": {
    "term" : { "user" : "kimchy" } 
  }
}
1

There are 1 best solutions below

0
On BEST ANSWER

If you're only interested in matching a field by an exact value, then the term query is the one you're looking for.

Span queries are much more involved (and low-level) and deal with positional matching of terms. span_term is one of the basic building blocks of more complex span queries, such as span_near, span_within, etc. You can find an interesting article demonstrating the power and complexity of span queries at: https://lucidworks.com/post/the-spanquery/

If you're ever interested to find matches based on relative positions of terms, then you should instead go for the intervals query, which provides a higher-level DSL for proximity matching.

But the bottom line is that if all you want to do is to match user: kimchy, then go for the simper term query.