Using span queries

ElasticSearch leverages the Lucene span queries, which basically allow us to create queries that match when some tokens or phrases are placed near other tokens or phrases. When using the standard non-span queries, we are not able to make queries that are position aware—to some extent phrase queries allow that, but only to some extent.

There are five span queries exposed in ElasticSearch:

  • Span term query
  • Span first query
  • Span near query
  • Span or query
  • Span not query

Before we continue with the description, let us index a new document that we will be using in order to show how span queries work. To do that, we send the following command to ElasticSearch:

curl -XPOST 'localhost:9200/library/book/5' -d '{
  "title" : "Test book",
 "author" ...

Get ElasticSearch Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.