# Fetching position of keyword in matched document

**URL:** <https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291>\
**Category:** Elasticsearch\
**Created:** [July 24, 2017, 9:27am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291 "2017-07-24T09:27:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugo53](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo53/32/24588_2.png) [@hugo53](https://discuss.elastic.co/u/hugo53)\
**Post date:** [July 24, 2017, 9:27am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/1 "2017-07-24T09:27:17Z")

</div>

Hello there,

I'm trying to highlight searched keyword in matched document with some custom works. Therefore, I need to know position (or offset) of that keyword in document. However, I found no documentation showing clearly how to do that. I know that when set "index\_options" to "offsets" or "term\_vector" to "with\_positions\_offsets", position for token will be generated and is stored together with token, but I don't know how to fetch those values.

Please give me some suggestions. Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 24, 2017, 10:34am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/2 "2017-07-24T10:34:38Z")

</div>

See term vectors API [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html)

---

<div class="post-metadata">

**Author:** ![hugo53](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo53/32/24588_2.png) [@hugo53](https://discuss.elastic.co/u/hugo53)\
**Post date:** [July 25, 2017, 1:31am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/3 "2017-07-25T01:31:01Z")

</div>

Thank you Mark for your support. It's useful but seems we need to do multi-steps to get those information, like:  
Step 1: Get term vector of document  
Step 2: Filter search keyword from result of step 1

So, in the case with query:

```
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "text": "brown fox"
    }
  },
  "highlight": {
    "fields": {
      "text": {} 
    }
  }
}

```

I would like to know if we can get offset position in result (for example in highlight, because elastic engine has ability to return highlighted text with pre & post tags, I assume that it knows text positions), so we don't need extra step.

```
{
  "took" : 113,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5063205,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "my_type",
        "_id" : "1",
        "_score" : 0.5063205,
        "_source" : {
          "text" : "Quick brown fox"
        },
        "highlight" : {
          "text" : [
            "Quick <em>brown</em> <em>fox</em>"
          ]
        }
      }
    ]
  }
}

```

Thank you again Mark!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 25, 2017, 9:17am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/4 "2017-07-25T09:17:21Z")

</div>

You can supply custom markup tags e.g.instead of `<em>` you could have `<somethingMyAppUnderstands>` but this won't return the offsets.  
I expect the most likely solution would be to implement a custom highlighter plugin (see [example](https://github.com/wikimedia/search-highlighter)) because these are given the resources you need to get hold of the query tokens and the document contents. With some custom code you could return the required output.

---

<div class="post-metadata">

**Author:** ![hugo53](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo53/32/24588_2.png) [@hugo53](https://discuss.elastic.co/u/hugo53)\
**Post date:** [July 25, 2017, 10:23am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/5 "2017-07-25T10:23:44Z")

</div>

Thank Mark for mentioning the plugin. I'm going to try it. Btw, I found this issue [https://github.com/elastic/elasticsearch/issues/5736](https://github.com/elastic/elasticsearch/issues/5736), still open for more than 3 years, hope next releases will implement this helpful feature.

---

<div class="post-metadata">

**Author:** ![hugo53](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugo53/32/24588_2.png) [@hugo53](https://discuss.elastic.co/u/hugo53)\
**Post date:** [July 29, 2017, 3:52am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/6 "2017-07-29T03:52:45Z")

</div>

Finally, by using [search-highlighter](https://github.com/wikimedia/search-highlighter) plugin, I can get highlight text offset, by query for example:

```
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "_source": ["content"],
    "query" : {
        "match_phrase" : { "content" : "test keyword" }
    },
    "highlight" : {
    	"pre_tags" : ["<b>"],
        "post_tags" : ["</b>"],
        "fields" : {
            "content" : {
            	"fragment_size" : 30, 
            	"number_of_fragments" : 10, 
            	"type": "experimental",
            	"options": {"return_offsets": true}
            }
        },
        "order" : "score"
    }
}
'

```

Thank you @Mark_Harwood for your help!

---

<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:** [August 26, 2017, 3:52am UTC](https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291/7 "2017-08-26T03:52:52Z")

</div>

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