Search code examples
javajsonsearchelasticsearchput

Elasticsearch extend inner document array


Ok maybe I am missing some core concept of Elasticsearch but I am new to this and trying to achieve something that looked sensible to me.

Lets imagine we have a number of runners in a race, with checkpoints around the track.

Out base documents may look like:

{
    "name"       : "John Smith",
    "age"        : "31",
    "checkpoints": [
        {
            "checkpoint" : "Race Start"
            "timestamp"  : "..."
        }
    ]
 }

My question is, does it make sense to be able to extend the list of checkpoints and if so, what would be an example (POST) request to do that?

Update:

Expected result:

{
    "name"       : "John Smith",
    "age"        : "31",
    "checkpoints": [
        {
            "checkpoint" : "Race Start"
            "timestamp"  : "..."
        },
        {
            "checkpoint" : "Checkpoint1"
            "timestamp"  : "..."
        },
        {
            "checkpoint" : "Checkpoint2"
            "timestamp"  : "..."
        }
    ]
 }

Solution

  • You don't have to do something specific.

    When you run PUT query:

    curl -XPUT localhost:9200/your_index/your_type/1 -d '{
        "name"       : "John Smith",
        "age"        : "31",
        "checkpoints": [
            {
                "checkpoint" : "Race Start",
                "timestamp"  : "..."
            }
        ]
    }'
    

    You will obtain exactly the same in GET query:

    curl -XGET localhost:9200/your_index/your_type/1
    

    Result:

    {"_index":"your_index","_type":"your_type","_id":"1","_version":2,"found":true,"_source":{
            "name"       : "John Smith",
            "age"        : "31",
            "checkpoints": [
                {
                    "checkpoint" : "Race Start",
                    "timestamp"  : "..."
                }
            ]
        }}
    

    So, when you run:

    curl -XPUT localhost:9200/your_index/your_type/1 -d '{
        "name"       : "John Smith",
        "age"        : "31",
        "checkpoints": [
            {
                "checkpoint" : "Race Start",
                "timestamp"  : "..."
            },
            {
                "checkpoint" : "Checkpoint1",
                "timestamp"  : "..."
            },
            {
                "checkpoint" : "Checkpoint2",
                "timestamp"  : "..."
            }
        ]
    }'
    

    You will obtain:

    {"_index":"your_index","_type":"your_type","_id":"1","_version":3,"found":true,"_source":{
            "name"       : "John Smith",
            "age"        : "31",
            "checkpoints": [
                {
                    "checkpoint" : "Race Start",
                    "timestamp"  : "..."
                },
                {
                    "checkpoint" : "Checkpoint1",
                    "timestamp"  : "..."
                },
                {
                    "checkpoint" : "Checkpoint2",
                    "timestamp"  : "..."
                }
            ]
        }}