# Get index of item in results set

**URL:** https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660
**Category:** Elasticsearch
**Created:** [October 9, 2018, 3:02pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660 "2018-10-09T15:02:57Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ambrt](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@ambrt](https://discuss.elastic.co/u/ambrt)
#### Post date: [October 9, 2018, 3:02pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/1 "2018-10-09T15:02:57Z")

</div>

Hi,

Is there a way to get index of item in results set?

Lets say i have 1000 results of query match\_all, sorted by date created. How to know where is (index of) one specific item in those 1000 results?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 9, 2018, 3:18pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/2 "2018-10-09T15:18:46Z")

</div>

I don't understand the question.  
Could you be more specific? May be with an example?

---

<div class="post-metadata">

### Author: ![ambrt](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@ambrt](https://discuss.elastic.co/u/ambrt)
#### Post date: [October 9, 2018, 3:32pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/3 "2018-10-09T15:32:49Z")

</div>

Thanks for reply @dadoonet

Example would a linking to one specific reply in paginated thread (on forum).

Thread would be paginated (20 replies per page). I'd need to know where the one specific reply is in whole thread of hundreds of replies (i.e on which page of pagination it was posted), that are sorted by dateCreated.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 9, 2018, 5:59pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/4 "2018-10-09T17:59:26Z")

</div>

Could you provide a script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you want to do. Please, try to keep the example as simple as possible.

I'm not sure what you're indexing and how...

---

<div class="post-metadata">

### Author: ![ambrt](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@ambrt](https://discuss.elastic.co/u/ambrt)
#### Post date: [October 22, 2018, 10:21am UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/5 "2018-10-22T10:21:17Z")

</div>

Ok.

So my example would this:  
I query es with (let's call it "Script #1")

```
GET .kibana/doc/_search
{
  "_source":["_id","updated_at"],
  "query": {
    "match_all":{}
  },
  "size":5, 
  "sort": {"updated_at":"desc"}
}

```

and the hits are:

```
"hits": [
  {
    "_index": ".kibana",
    "_type": "doc",
    "_id": "visualization:1ffc5e20-7827-11e7-8c47-65b845b5cfb3",
    "_score": null,
    "_source": {
      "updated_at": "2018-10-20T21:50:20.309Z"
    },
    "sort": [
      1540072220309
    ]
  },
  {
    "_index": ".kibana",
    "_type": "doc",
    "_id": "visualization:1bdca740-7828-11e7-8c47-65b845b5cfb3",
    "_score": null,
    "_source": {
      "updated_at": "2018-10-20T21:50:20.309Z"
    },
    "sort": [
      1540072220309
    ]
  },
  {
    "_index": ".kibana",
    "_type": "doc",
    "_id": "visualization:804ffc40-7828-11e7-8c47-65b845b5cfb3",
    "_score": null,
    "_source": {
      "updated_at": "2018-10-20T21:50:20.309Z"
    },
    "sort": [
      1540072220309
    ]
  },
  {
    "_index": ".kibana",
    "_type": "doc",
    "_id": "dashboard:8d3ed660-7828-11e7-8c47-65b845b5cfb3",
    "_score": null,
    "_source": {
      "updated_at": "2018-10-20T21:50:20.309Z"
    },
    "sort": [
      1540072220309
    ]
  },
  {
    "_index": ".kibana",
    "_type": "doc",
    "_id": "visualization:c618e4e0-7c69-11e7-aa55-3b0d52c71c60",
    "_score": null,
    "_source": {
      "updated_at": "2018-10-20T21:50:20.309Z"
    },
    "sort": [
      1540072220309
    ]
  }
]

```

The item with "\_id":"dashboard:8d3ed660-7828-11e7-8c47-65b845b5cfb3" is 4th in the array of 5 hits.

What i would like to do is ("Script #2") to tell which nth number in hits of "Script #1" is item with provided \_id.

In this example script should return "4" for \_id "dashboard:8d3ed660-7828-11e7-8c47-65b845b5cfb3"

On such small arrays ,as in example, i could just run script, get data and count it in my own program.

But the question is how could i do it for bigger (or any) sets of hits and ranking script.

@dadoonet

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 22, 2018, 10:49am UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/6 "2018-10-22T10:49:10Z")

</div>

I don't see any option to do that on elasticsearch side.  
I'm wondering though what is the use case for this. Could you tell?

---

<div class="post-metadata">

### Author: ![ambrt](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@ambrt](https://discuss.elastic.co/u/ambrt)
#### Post date: [October 25, 2018, 2:03pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/7 "2018-10-25T14:03:13Z")

</div>

Oh, i see. Thank you for reply.

The use case is basically a typical forum with feed for new replies. In order to make href link from feed to specific page of thread i'd need to know how many replies i got to "skip" (or paginate the thread).

I was searching for it because I create and store both feed and replies in ES.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [October 26, 2018, 10:31am UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/8 "2018-10-26T10:31:28Z")

</div>

Why not storing the link to each thread in the JSON document itself? Like:

```auto
{
  "user": "me",
  "date": "2018-10-20",
  "text": "foo bar baz",
  "url": "https://linkto/this/thread"
}

```

---

<div class="post-metadata">

### Author: ![ambrt](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@ambrt](https://discuss.elastic.co/u/ambrt)
#### Post date: [October 28, 2018, 12:22pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/9 "2018-10-28T12:22:12Z")

</div>

That's good idea.

To solve pagination, one could count number of already existing replies to thread and increment it by one on new reply stored.

Result would be

```
{
  "user": "me",
  "date": "2018-10-20",
  "text": "foo bar baz",
  "url": "https://linkto/this/thread",
  "reply_number": 32
}

```

or something like that.

Anyway thank you reply.

---

<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: [November 25, 2018, 12:22pm UTC](https://discuss.elastic.co/t/get-index-of-item-in-results-set/151660/10 "2018-11-25T12:22:13Z")

</div>

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