# Getting position of document in search results

**URL:** https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227
**Category:** Elasticsearch
**Created:** [April 13, 2011, 7:58am UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227 "2011-04-13T07:58:53Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Wojciech\_Durczynski](https://avatars.discourse-cdn.com/v4/letter/w/fbc32d/32.png) [@Wojciech\_Durczynski](https://discuss.elastic.co/u/Wojciech_Durczynski)
#### Post date: [April 13, 2011, 7:58am UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/1 "2011-04-13T07:58:53Z")

</div>

Hello. I use elasticsearch for search (java API) and would like to  
achieve following functionality:  
Having document id and query criteria (query, filters and sort  
specification) I'd like to get position of document with this id in  
search results of query with provided criteria.  
Also I need ids of neighbour documents of this document in this search  
results.  
I know I can query for ids of all documents matching criteria and then  
find document with provided id in this sequence (together with its  
position and its neighbours). But it would be very slow if search  
results are very big.  
How to achieve this in an optimal way?

---

<div class="post-metadata">

### Author: ![Ridvan\_Gyundogan](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@Ridvan\_Gyundogan](https://discuss.elastic.co/u/Ridvan_Gyundogan)
#### Post date: [April 16, 2011, 5:25pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/2 "2011-04-16T17:25:55Z")

</div>

Hm,  
this would be interesting to me too.

I think about something like, I don know whether it is possible:

1. First query - add id=yourDocumentId with a filter to get the exact score  
of your document, scoreDoc.
2. Second query, add filter score\<scoreDoc and get the first result for the  
document after.
3. Third query, add filter score\>scoreDoc and get the last result for the  
document before.

I am newbie here, but I think this should be doable.

Best

2011/4/13 Wojciech Durczyński [wojciech.durczynski@comarch.com](mailto:wojciech.durczynski@comarch.com)

> Hello. I use elasticsearch for search (java API) and would like to  
> achieve following functionality:  
> Having document id and query criteria (query, filters and sort  
> specification) I'd like to get position of document with this id in  
> search results of query with provided criteria.  
> Also I need ids of neighbour documents of this document in this search  
> results.  
> I know I can query for ids of all documents matching criteria and then  
> find document with provided id in this sequence (together with its  
> position and its neighbours). But it would be very slow if search  
> results are very big.  
> How to achieve this in an optimal way?

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [April 16, 2011, 5:34pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/3 "2011-04-16T17:34:39Z")

</div>

> I think about something like, I don know whether it is possible:
> 
> 1. First query - add id=yourDocumentId with a filter to get the exact  
> score of your document, scoreDoc.
> 2. Second query, add filter score\<scoreDoc and get the first result  
> for the document after.
> 3. Third query, add filter score\>scoreDoc and get the last result for  
> the document before.

Can you explain your use case?

clint

---

<div class="post-metadata">

### Author: ![Ridvan\_Gyundogan](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@Ridvan\_Gyundogan](https://discuss.elastic.co/u/Ridvan_Gyundogan)
#### Post date: [April 16, 2011, 7:15pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/4 "2011-04-16T19:15:35Z")

</div>

I don't have use case at the moment but I could have the following  
situation:  
I sell products from several suppliers on my website.  
Direct clients search for some words on the site. For example "best product  
ever".

On the admin panel for the "supplier1" I want to show information: your  
"product1" appears on position 137 for the user's search "best product  
ever".

On Sat, Apr 16, 2011 at 8:34 PM, Clinton Gormley [clinton@iannounce.co.uk](mailto:clinton@iannounce.co.uk)wrote:

> > I think about something like, I don know whether it is possible:
> > 
> > 1. First query - add id=yourDocumentId with a filter to get the exact  
> > score of your document, scoreDoc.
> > 2. Second query, add filter score\<scoreDoc and get the first result  
> > for the document after.
> > 3. Third query, add filter score\>scoreDoc and get the last result for  
> > the document before.
> 
> Can you explain your use case?
> 
> clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [April 17, 2011, 12:02pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/5 "2011-04-17T12:02:41Z")

</div>

On Sat, 2011-04-16 at 22:15 +0300, Ridvan Gyundogan wrote:

> I don't have use case at the moment but I could have the following  
> situation:  
> I sell products from several suppliers on my website.  
> Direct clients search for some words on the site. For example "best  
> product ever".
> 
> On the admin panel for the "supplier1" I want to show information:  
> your "product1" appears on position 137 for the user's search "best  
> product ever".

Well, then I'd suggest pulling back enough results until you find the  
record you are after.

clint

---

<div class="post-metadata">

### Author: ![K\_B](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@K\_B](https://discuss.elastic.co/u/K_B)
#### Post date: [April 18, 2011, 8:45am UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/6 "2011-04-18T08:45:35Z")

</div>

@clint: sounds only good for pos 1 to 200 - but what would you suggest  
to do when the position is e.g.: 2734 or even 1.500.237 ?

I'm at a similar problem needing to find out the position of a  
document in a given query to get the document that are next to it (the  
one before and the one after) and couldn't find any good solution so  
far beside to save the whole query and use the from(int) onto it (java  
api); However, this only works in case the query was executed before  
so that the index-position could be saved; In case the query didn't  
execute before I have no idea what to do;

(Imagine: page with catalog -\> detail view that should allow to  
scroll; one could access the detail view without going to catalog  
beforehand);

Best

On 17 Apr., 14:02, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> On Sat, 2011-04-16 at 22:15 +0300, Ridvan Gyundogan wrote:
> 
> > I don't have use case at the moment but I could have the following  
> > situation:  
> > I sell products from several suppliers on my website.  
> > Direct clients search for some words on the site. For example "best  
> > product ever".
> 
> > On the admin panel for the "supplier1" I want to show information:  
> > your "product1" appears on position 137 for the user's search "best  
> > product ever".
> 
> Well, then I'd suggest pulling back enough results until you find the  
> record you are after.
> 
> clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [April 18, 2011, 9:09am UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/7 "2011-04-18T09:09:52Z")

</div>

On Mon, 2011-04-18 at 01:45 -0700, K.B. wrote:

> @clint: sounds only good for pos 1 to 200 - but what would you suggest  
> to do when the position is e.g.: 2734 or even 1.500.237 ?

OK - so lets say you have 5 shards in an ES index. And you want to find  
the doc at position 1,500,00

Each shard has to order 1,500,000 of their local docs an return to the  
node handling the request, which then has to combine 7,500,000 results  
into the right order, before discarding all but 10 of them.

This puts an enormous strain on your server.

Much better to keep track of these things in other ways.

clint

---

<div class="post-metadata">

### Author: ![David\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_b/32/3239_2.png) [@David\_B](https://discuss.elastic.co/u/David_B)
#### Post date: [April 18, 2011, 10:24pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/8 "2011-04-18T22:24:29Z")

</div>

On Apr 16, 6:25 pm, Ridvan Gyundogan [ridva...@gmail.com](mailto:ridva...@gmail.com) wrote:

> Hm,  
> this would be interesting to me too.
> 
> I think about something like, I don know whether it is possible:
> 
> 1. First query - add id=yourDocumentId with a filter to get the exact score  
> of your document, scoreDoc.

I originally thought something like this might be possible using the  
percolator but I see it is not - i.e., percolating documents against  
an index just returns the list of percolators that match, and doesn't  
give any indication as to the quality of the match.

A priori there doesn't seem to be any reason why this information  
shouldn't come out with the percolator (for my use case, I would only  
want rough score data - I wouldn't need an exact score that would be  
guaranteed to stay the same as other documents were added to the  
index). Is there any way this could be exposed or are percolator  
queries strict pass/fail?

> 1. Second query, add filter score\<scoreDoc and get the first result for the  
> document after.
> 2. Third query, add filter score\>scoreDoc and get the last result for the  
> document before.
> 
> I am newbie here, but I think this should be doable.
> 
> Best
> 
> 2011/4/13 Wojciech Durczyński [wojciech.durczyn...@comarch.com](mailto:wojciech.durczyn...@comarch.com)
> 
> > Hello. I use elasticsearch for search (java API) and would like to  
> > achieve following functionality:  
> > Having document id and query criteria (query, filters and sort  
> > specification) I'd like to get position of document with this id in  
> > search results of query with provided criteria.  
> > Also I need ids of neighbour documents of this document in this search  
> > results.  
> > I know I can query for ids of all documents matching criteria and then  
> > find document with provided id in this sequence (together with its  
> > position and its neighbours). But it would be very slow if search  
> > results are very big.  
> > How to achieve this in an optimal way?

---

<div class="post-metadata">

### Author: ![dmitry\_polushkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitry_polushkin/32/1326_2.png) [@dmitry\_polushkin](https://discuss.elastic.co/u/dmitry_polushkin)
#### Post date: [August 21, 2014, 8:20pm UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/9 "2014-08-21T20:20:16Z")

</div>

Hi David,

Have you found some solution?

Regards,  
Dmitry

---

<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: [July 6, 2017, 1:07am UTC](https://discuss.elastic.co/t/getting-position-of-document-in-search-results/4227/10 "2017-07-06T01:07:07Z")

</div>


