# Set an index number to a document

**URL:** <https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395>\
**Category:** Elasticsearch\
**Created:** [February 28, 2020, 9:39am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395 "2020-02-28T09:39:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [February 28, 2020, 9:39am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/1 "2020-02-28T09:39:06Z")

</div>

Hello,  
How can the index number of a document be calculated in Elastic?  
For example, there is an index with gameplay sessions of players. Each document contains data about a player's game. So how can the information (e.g., number of kills in **only** the **4th** game of a player) be visualized?

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [February 28, 2020, 12:47pm UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/2 "2020-02-28T12:47:40Z")

</div>

Hi there,

can you please be a bit more specific on what you are trying to achieve? Can you post here ( **properly indented and formatted using the `Preformatted text` tool**![image](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a26d3d1d3616fa75c69561bb9c63c489c4b66ff3.png) ) a sample document, explaining what you would like to visualize?

---

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [February 28, 2020, 1:38pm UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/3 "2020-02-28T13:38:38Z")

</div>

Here is the json of a sample document:

```auto
{
  "_index": "gameplay_stat",
  "_type": "data",
  "_id": "ux0NaHABoXVsCiNPmxAC",
  "_version": 1,
  "_score": null,
  "_source": {
    "player_id": 5617,
    "player_login": "YYY",
    "player_created_at": "2020-02-19T15:41:20.000+00:00",
    "avg_lifetime": 23,
    "room_id": 76521,
    "efficiency": 261,
    "room_player_amount": 6,
    "room_build": "0.3.0.1136",
    "room_map_name": "map2",
    "room_game_mode": "game_mode_3",
    "room_created_at": "2020-02-20T13:29:03.000+00:00",
    "room_duration": 165,
    "kills": 6,
    "place": 1,
    "assist": 1,
    "death": 5,
    "character_key": 7,
    "character_name": "character1",
    "character_lvl": 3,
    "is_tester": false,
    "leaver": false,
    "room_length": 165,
    "is_win": false,
    "players_amount": 1,
    "team_score": 504,
    "enemy_team_score": 1000,
    "session_id": 62812,
    "session_count": 13,
    "device": "Xiaomi Redmi Note 8",
    "overall_gameplay_duration": 13947,
    "damage_dealt": 5100,
    "damage_received": 6494,
    "min_fps": 53,
    "max_fps": 59,
    "middle_fps": 59,
    "median_fps": 59,
    "Rang_level": 9,
    "BattlePass_level": 34
  },
  "fields": {
    "win_count": [
      0
    ],
    "duration_in_min": [
      2
    ],
    "kill_death_ratio": [
      1
    ],
    "player_created_at": [
      "2020-02-19T15:41:20.000Z"
    ],
    "room_created_at": [
      "2020-02-20T13:29:03.000Z"
    ],
    "win_not_count": [
      1
    ],
    "days_after_registration": [
      1
    ]
  },
  "sort": [
    1582291743000
  ]
}

```

So I need some specific information about , for example, the average number of kills in the 3rd game (of each player who's played it) and compare it to the 7th game. For that I need to number a player's each game and then visualize the information.

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [March 2, 2020, 11:01am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/4 "2020-03-02T11:01:35Z")

</div>

Hi,

well, I don't really get where you could retrieve info about which game the player is playing. Is there any info in that document you posted that might suggest anything about the game? This looks like something you should give in input to Logstash more than something Logstash can compute per se.

Unless you wanna do something like applying the [`elasticsearch` filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-elasticsearch.html) and query elasticsearch for the last ingested document containing a field distinctively identifying a given player and, based on the result of the query, add a field `game` with a given value.

This means, let's say you wanna know which game is playing the player with id 5617, in your logstash you implement an elasticsearch filter querying your elasticsearch index containing all the info, filtering by that `player_id` and sorting in desc order. You can have two scenarios:

- If your query returns something (that player has already played a game), you take the value of the `game` field, increment it by 1 and add a new field `game` to your current event with that incremented value
- Your query returns nothing (this is the first game that played\_id is playing), so you simply add a field `game` to your current event with value 1.

Obviously, you have to be careful using `elasticsearch` filter plugin, since you're basically running a query against the elasticsearch ingest per each document Logstash is processing. It means that if your Logstash is processing a lot of documents, you're bombarding your elasticsearch with many queries. And if the index you're storing your info into is a large index, you're making a huge amount of queries against a very large index.

That's why I said it would be better to pass this kind of info as input to Logstash.

Hope this helps, anyway.

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [March 2, 2020, 11:10am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/5 "2020-03-02T11:10:23Z")

</div>

Hi,

I'm sorry. I was considering it as posted under the Logstash section 😅

Anyway, also in this case, this is something I'd probably do at ingest time. I mean, I'd try to ingest documents directly with the `game` field assigned (computing it before ingesting or, if not possible, computing it via the `elasticsearch` filter in Logstash or the [`enrich` processor](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/enrich-processor.html) in Elasticsearch).

Also, do you have only one of those document per player per game? Or can you have more than one per game per player?

Finally, do you need that specific piece of info to be computed real-time? Or can you accept to add it with some delay? (like running something once a day at the end of the day to add the `game` field to all those documents that still don't have it)?

---

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [March 4, 2020, 10:03am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/6 "2020-03-04T10:03:35Z")

</div>

Well, your idea about setting an index before ingesting is the solution, thanks!  
I've a document per a player's game. So if a player has played multiple times, there are multiple documents about it.  
Sometimes I need to compare statistics about the players' 1st game who installed the app in different days or weeks. It remotely resembles A / B tests comparison

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [March 4, 2020, 10:18am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/7 "2020-03-04T10:18:24Z")

</div>

> setting an index before ingesting

Not sure about what you mean by that, but I'm glad you could find a solution out of what I wrote 😅

I don't really get whether you need further support or not.

---

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [March 4, 2020, 11:05am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/8 "2020-03-04T11:05:15Z")

</div>

> [@Fabio-sama](#):
>
> setting an index before ingesting

I mean "set (=calculate) an _index_ _number of a game_ before ingesting" 🙂  
Thank you for the support!

---

<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:** [April 1, 2020, 11:05am UTC](https://discuss.elastic.co/t/set-an-index-number-to-a-document/221395/9 "2020-04-01T11:05:20Z")

</div>

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