# How to get start offset and end offset of a search string in search result in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879>\
**Category:** Elasticsearch\
**Created:** [November 27, 2020, 12:57pm UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879 "2020-11-27T12:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sreejith\_Pk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sreejith_pk/32/79809_2.png) [@Sreejith\_Pk](https://discuss.elastic.co/u/Sreejith_Pk)\
**Post date:** [November 27, 2020, 12:57pm UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879/1 "2020-11-27T12:57:37Z")

</div>

Hi team, I have a requirement to find the start and end position of a particular word in a text.

---

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [November 27, 2020, 9:20pm UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879/2 "2020-11-27T21:20:29Z")

</div>

Storing [term\_vectors](https://www.elastic.co/guide/en/elasticsearch/reference/current/term-vector.html) and using the [Term vectors API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html) might be the best bet?

---

<div class="post-metadata">

**Author:** ![Sreejith\_Pk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sreejith_pk/32/79809_2.png) [@Sreejith\_Pk](https://discuss.elastic.co/u/Sreejith_Pk)\
**Post date:** [November 30, 2020, 6:39am UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879/3 "2020-11-30T06:39:31Z")

</div>

Thanks for the reply!

Suppose my text is "The patient date of birth is 12/11/2019", and I am searching for "date of birth" in the text. Upon search I need to get the start and end offset of the search string in the search result.

I need a result something like,

```auto
"date of birth" : {
    "start_offset" : 12,
    "end_offset" : 25
}

```

I have explored the term vectors API, but every time when I search, the start offset always starts with 0. In my case my search string offset in whole text is 12.

Thanks

---

<div class="post-metadata">

**Author:** ![egalpin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egalpin/32/79772_2.png) [@egalpin](https://discuss.elastic.co/u/egalpin)\
**Post date:** [December 10, 2020, 12:08am UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879/4 "2020-12-10T00:08:41Z")

</div>

You might find success with [Highlighting](https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html#highlighting).

---

<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:** [January 7, 2021, 12:08am UTC](https://discuss.elastic.co/t/how-to-get-start-offset-and-end-offset-of-a-search-string-in-search-result-in-elasticsearch/256879/5 "2021-01-07T00:08:45Z")

</div>

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