Get index of item in results set


(Rik) #1

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?


(David Pilato) #2

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


(Rik) #3

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.


(David Pilato) #4

Could you provide a script as described in About the Elasticsearch category. 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...


(Rik) #5

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


(David Pilato) #6

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?


(Rik) #7

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.


(David Pilato) #8

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

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

(Rik) #9

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.


(system) #10

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