Elasticsearch leverages Lucene span queries, which allow us to make queries when some tokens or phrases are near other tokens or phrases. Basically, we can call them position aware queries. When using the standard non span queries, we are not able to make queries that are position aware; to some extent, the phrase
queries allow that, but only to some extent. So, for Elasticsearch and the underlying Lucene, it doesn't matter if the term is in the beginning of the sentence or at the end or near another term. When using span queries, it does matter.
The following span queries are exposed in Elasticsearch:
span term
queryspan first
queryspan near
queryspan or
queryspan not
queryspan within
queryspan containing
queryspan multi
query
Before we continue with the description, let's index a document to a completely new index that we will use to show how span queries work. To do this, we use the following command:
curl -XPUT 'localhost:9200/spans/book/1' -d '{ "title" : "Test...