# Span Queries Near a Known Token Index

**URL:** <https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410>\
**Category:** Elasticsearch\
**Created:** [March 29, 2017, 5:59am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410 "2017-03-29T05:59:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [March 29, 2017, 5:59am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410/1 "2017-03-29T05:59:17Z")

</div>

Is there a way to do a span query near a particular token index?

For example, is it possible to find all documents that mention "foo" within ten tokens of the start of the document? Or more generally, is it possible to find all documents that contain "foo" within X tokens of the Yth token in the document?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 29, 2017, 7:03am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410/2 "2017-03-29T07:03:23Z")

</div>

There is no span query that does it directly as it is a more common use-case to reason on relative positions of tokens. However, the information that is needed to find tokens near a certain absolute position is available in the Lucene index so it would be possible to implement such span queries.

---

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [March 29, 2017, 7:07am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410/3 "2017-03-29T07:07:14Z")

</div>

Thanks for the response, that's what I thought, but wanted to double check. This would be a really nice feature.

---

<div class="post-metadata">

**Author:** ![Michael\_Sander](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_sander/32/838_2.png) [@Michael\_Sander](https://discuss.elastic.co/u/Michael_Sander)\
**Post date:** [March 30, 2017, 7:11am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410/4 "2017-03-30T07:11:59Z")

</div>

FYI, I opened a feature request which has a richer explanation of the motivation and use-cases: [https://github.com/elastic/elasticsearch/issues/23812](https://github.com/elastic/elasticsearch/issues/23812)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 27, 2017, 7:12am UTC](https://discuss.elastic.co/t/span-queries-near-a-known-token-index/80410/5 "2017-04-27T07:12:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
