# Get the keyword positions for all matched docs with just ONE search query

**URL:** https://discuss.elastic.co/t/get-the-keyword-positions-for-all-matched-docs-with-just-one-search-query/77063
**Category:** Elasticsearch
**Created:** [March 1, 2017, 10:50pm UTC](https://discuss.elastic.co/t/get-the-keyword-positions-for-all-matched-docs-with-just-one-search-query/77063 "2017-03-01T22:50:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Yuchen\_He](https://avatars.discourse-cdn.com/v4/letter/y/e79b87/32.png) [@Yuchen\_He](https://discuss.elastic.co/u/Yuchen_He)
#### Post date: [March 1, 2017, 10:50pm UTC](https://discuss.elastic.co/t/get-the-keyword-positions-for-all-matched-docs-with-just-one-search-query/77063/1 "2017-03-01T22:50:46Z")

</div>

I'm been wrestling with this problem for days.

For example, if I have

doc\_id1:"thor marvel",  
doc\_id2:"spiderman thor",  
doc\_id3:"the avengers captain america ironman thor"

three documents in elastic search, and do a search query for "thor", I want it to tell me **where** keyword "thor" is found in **each** document, like { doc\_id1: 1, doc\_id2: 2, doc\_id3: 6} as the desired result.

I have two possible solution on top of my head now:

1. figure out a way to put the **\_vectorterm** info (which includes all the positions/offsets for each token/word of the document) into **\_source** , so that I can directly access \_vectorterm info in my normal search result. I can then construct the (doc, position) list outside elasticsearch. Normally, you can only access \_vectorterm info for a single document at a time given the index/type/id, which is why it's tricky. That should be the ideal way to achieve the goal

2. figure out a way to **trigger an action** whenever a new document is added. This action will scan through all the tokens/words in the new document, create a new "token" index(if it doesn't exist) and append (doc\_id, position) pair to it like  
{ keyword:"thor" [doc\_id1:1,doc\_id2:2,doc\_id3:6] }.  
So that I just need to search for "thor" among keywords indexes and then get the (doc, position) lists. This seems to be even harder and less optimal.

Sadly, I don't know how to do either one. I'll appreciate it if someone can give me some help on this. Many thanks!

---

<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: [March 29, 2017, 10:50pm UTC](https://discuss.elastic.co/t/get-the-keyword-positions-for-all-matched-docs-with-just-one-search-query/77063/2 "2017-03-29T22:50:56Z")

</div>

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