2

有没有办法将术语附加到值数组中?

例如,如果我的文档如下所示:

{
   "items": ["item1", "item2", "item3"]
}

我想将“item4”和“item5”附加到它。

我必须在 2 个查询中完成吗?一个加载当前值列表,然后更新该列表?还是有更优雅的方式可以让我在一个查询中附加这些项目?

我正在尝试使用这样的 elastic4s 来做到这一点:

client.execute(ElasticDsl.update id id in indexName / documentType script {
  script(s"ctx._source.items += tag").params(Map("tag"->"item4"))
})

为了使用上面的代码片段,我需要启用 groovy 脚本,我不知道如何使用多个项目来做到这一点。

任何的想法?

4

2 回答 2

4

这是一个完整的示例,说明如何实现这一目标。

将新值合并到数组并在以下之后使其唯一:

DELETE test/test/1

POST test/test/1
{
  "terms":["item1", "item2", "item3"]
}

GET test/test/1

POST test/test/1/_update
{
     "script" : " ctx._source.terms << newItems; ctx._source.terms = ctx._source.terms.flatten().unique()",
     "params" : {
         "newItems" : ["a","b"]
     }
}

确保您在服务器配置中启用了脚本

user:/etc/elasticsearch# head elasticsearch.yml 
script.inline: true
script.indexed: true
...
于 2016-03-30T15:12:49.190 回答
-1

尝试在您的代码中使用“条款”过滤器。如果您使用的是 NEST,那么以下链接将很有用https://nest.azurewebsites.net/nest/writing-queries.html

于 2016-03-30T15:41:27.890 回答