Set an index number to a document

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?

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 ) a sample document, explaining what you would like to visualize?

Here is the json of a sample document:

  "_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": "",
    "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": [
    "duration_in_min": [
    "kill_death_ratio": [
    "player_created_at": [
    "room_created_at": [
    "win_not_count": [
    "days_after_registration": [
  "sort": [

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.


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 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.


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

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 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)?

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

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 :sweat_smile:

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

I mean "set (=calculate) an index number of a game before ingesting" :slight_smile:
Thank you for the support!

1 Like

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