【问题标题】:Elasticsearch extend inner document arrayElasticsearch 扩展内部文档数组
【发布时间】:2016-11-17 03:33:06
【问题描述】:

好吧,也许我错过了 Elasticsearch 的一些核心概念,但我对此很陌生,并试图实现一些对我来说看起来很明智的事情。

假设我们有许多跑步者参加比赛,赛道周围有检查站。

基本文档可能如下所示:

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

我的问题是,能够扩展检查点列表是否有意义,如果是,那么执行此操作的示例 (POST) 请求是什么?

更新:

预期结果:

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

【问题讨论】:

  • to extend the list of checkpoints 是什么意思?向数组中添加另一个对象?
  • 添加了预期结果

标签: java json search elasticsearch put


【解决方案1】:

您不必做特定的事情。

当你运行 PUT 查询时:

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

在 GET 查询中你会得到完全相同的结果:

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

结果:

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

所以,当你运行时:

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"  : "..."
        }
    ]
}'

您将获得:

{"_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"  : "..."
            }
        ]
    }}

【讨论】:

  • 谢谢您,先生!我花了大约 2 个小时,尝试了各种特殊模式,使用 /_update 脚本等。它总是比你想象的要简单。
猜你喜欢
  • 1970-01-01
  • 2019-04-29
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
相关资源
最近更新 更多