Rohit Patwa
Rohit Patwa

Reputation: 1160

What is the difference between span_containing and span_within query in elasticsearch?

The documentation says

span_containing:

The big and little clauses can be any span type query. Matching spans from big that contain matches from little are returned.

span_within:

The big and little clauses can be any span type query. Matching spans from little that are enclosed within big are returned.

Upvotes: 2

Views: 911

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

As far as which documents are matched by the query, there is no difference. The difference is which span is matched.

  • span_containing matches big.
  • span_within matches little.

The query will take it's boost from the matching span, so span_containing will get it's boost from big, and span_within will get it's boost from the matching little.

The difference also becomes relevant if your span_within or span_containing is part of another span query which considers the position of the matched span.


For example, lets say you have some text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

And your span_containing/within is matching the first five terms, with little matching "ipsum". If you then wrap it in a span_near, then the slop of that outer span_near is going to be taken from the boundaries of big for span_containing, and from little ("ipsum") for span_within.

The distance between "amet" (the end of big) and "consectetur" is ≤1, so this would match the text:

"span_near": {
  "clauses": [
    "span_containing" : {
      "little" : {
        "span_term" : { "field" : "ipsum" }
      },
      "big" : {
        "span_near" : {
          "clauses" : [
            { "span_term" : { "field" : "lorem" } },
            { "span_term" : { "field" : "amet" } }
          ],
          "slop" : 5,
          "in_order" : true
        }
      }
    },
    { "span_term" : { "field" : "consectetur" } }
  ],
  "slop": 1,
  "in_order": true
}

But the distance between "ipsum" and "consectetur" is >1, so this would not:

"span_near": {
  "clauses": [
    "span_within" : {
      "little" : {
        "span_term" : { "field" : "ipsum" }
      },
      "big" : {
        "span_near" : {
          "clauses" : [
            { "span_term" : { "field" : "lorem" } },
            { "span_term" : { "field" : "amet" } }
          ],
          "slop" : 5,
          "in_order" : true
        }
      }
    },
    { "span_term" : { "field" : "consectetur" } }
  ],
  "slop": 1,
  "in_order": true
}

Upvotes: 2

Related Questions