繁体   English   中英

使用Elasticsearch和无痛,如何插入地图数组?

[英]Using Elasticsearch and painless, how do I insert a map array?

我有以下代码:

  "script": {
    "lang": "painless",
    "source": """
      ctx._source.maparray = [
    "first" : "Foo",
    "last" : "Bar"
]

结果

"maparray": {
  "last": "Bar",
  "first": "Foo"
},

但我希望maparray成为一个数组。 所以现在基于:

https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-array.html

我尝试:

 "script": {
    "lang": "painless",
    "source": """
      ctx._source.maparray = new map[]{[
    "first" : "Foo",
    "last" : "Bar"
]}
    """,
    "params": {
      "key": "numWords"
    }
  }

但我得到:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... x._source.maparray = new map[]{[\n    \"first\" : \"Fo ...",
          "                             ^---- HERE"
        ],
        "script": "      ctx._source.maparray = new map[]{[\n    \"first\" : \"Foo\",\n    \"last\" : \"Bar\"\n]}",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "... x._source.maparray = new map[]{[\n    \"first\" : \"Fo ...",
      "                             ^---- HERE"
    ],
    "script": "      ctx._source.maparray = new map[]{[\n    \"first\" : \"Foo\",\n    \"last\" : \"Bar\"\n]}",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "invalid sequence of tokens near ['map'].",
      "caused_by": {
        "type": "no_viable_alt_exception",
        "reason": null
      }
    }
  },
  "status": 500
}

我的语法有什么问题?

你在寻找的实际上是一组地图。 下面是我如何提出一个示例脚本,我使用了Ingest Pipeline

脚本所需的管道

PUT _ingest/pipeline/my-pipeline-id-01
{
  "description" : "describe pipeline",
  "processors" : [
    {
        "script" : {
          "lang" : "painless",
          "inline" : """
             ArrayList al = new ArrayList();
             Map map = new HashMap();
             map.put("first","Foo");
             map.put("last", "Bar");
             al.add(map);
             ctx.maparray = al;
            """
        }
      }
  ]
}

您可以使用Reindex API测试脚本的工作方式。

Reindex脚本

POST _reindex
{
  "source": {
    "index": "<source_index_name>"
  },
  "dest": {
    "index": "<dest_index_name>",
    "pipeline": "my-pipeline-id-01"
  }
}

请测试以上内容,验证结果并告诉我它是怎么回事。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM