Link Search Menu Expand Document Documentation Menu

Span term query

The span_term query is the most basic span query that matches spans containing a single term. It serves as a building block for more complex span queries.

For example, you can use the span_term query to:

  • Find exact term matches that can be used in other span queries.
  • Match specific words while maintaining position information.
  • Create basic spans that can be combined with other span queries.

Example

To try the examples in this section, complete the setup steps.

The following query searches for the exact term “formal”:

GET /clothing/_search
{
  "query": {
    "span_term": {
      "description": "formal"
    }
  }
}

Alternatively, you can specify the search term in the value parameter:

GET /clothing/_search
{
  "query": {
    "span_term": {
      "description": {
        "value": "formal"
      }
    }
  }
}

You can also specify a boost value in order to boost the document score:

GET /clothing/_search
{
  "query": {
    "span_term": {
      "description": {
        "value": "formal",
        "boost": 2
      }
    }
  }
}

The query matches documents 1 and 2 because they contain the exact term “formal”. Position information is preserved for use in other span queries.

Response
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.498922,
    "hits": [
      {
        "_index": "clothing",
        "_id": "2",
        "_score": 1.498922,
        "_source": {
          "description": "Beautiful long dress in red silk, perfect for formal events."
        }
      },
      {
        "_index": "clothing",
        "_id": "1",
        "_score": 1.4466847,
        "_source": {
          "description": "Long-sleeved dress shirt with a formal collar and button cuffs. "
        }
      }
    ]
  }
}

Parameters

The following table lists all top-level parameters supported by span_term queries.

Parameter Data type Description
<field> String or object The name of the field in which to search.
350 characters left

Have a question? .

Want to contribute? or .