弹性搜索,是否可以更新嵌套对象而不更新整个文档? [英] elastic search, is it possible to update nested objects without updating the entire document?

查看:267
本文介绍了弹性搜索,是否可以更新嵌套对象而不更新整个文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在索引一组文档(想像它们作为论坛帖子),其中嵌套的对象是与该帖子相关的用户。我的问题是可能会更新用户字段,但由于帖子不更改,因此不会重新建立索引,并且用户嵌套对象变得过时。有没有办法更新嵌套的对象,而不重新索引整个文档?或者唯一的解决方案是每次用户更改时重新索引用户的所有相关帖子?

I'm indexing a set of documents (imagine them as forum posts) with a nested object which is the user related to that post. My problem is that the user fields might be updated, but since the posts do not change they are not reindexed and the user nested objects become outdated. Is there a way to update the nested objects without reindexing the whole document again? Or the only solution would be to reindex all the related posts of a user everytime that the user changes?

推荐答案

您可以使用更新API。

You can use the Update API.

curl -XPOST localhost:9200/docs/posts/post/_update -d '{
    "script" : "ctx._source.nested_user = updated_nested_user",
    "params" : {
        "updated_nested_user" : {"field": "updated"}
    }
}'

看到这个 SO答案

请注意,更新脚本支持条件逻辑,如这里。所以您可以在用户更改时标记论坛帖子,然后迭代帖子以仅更改更改用户的帖子。

Note that update scripts support conditional logic, as shown here. So you could tag forum posts when the user changes, then iterate over the posts to update only posts with changed users.

curl -XPOST 'localhost:9200/docs/posts/post/_update' -d '{
    "script" : "ctx._source.tags.contains(tag) ? "ctx._source.nested_user = updated_nested_John" : ctx.op = "none"",
    "params" : {
        "tag": "updated_John_tag",
        "updated_nested_John" : {"field": "updated"}
    }
}'

更新

也许我的三元运算符示例是不完整的。这个问题没有提及,但假设用户在应用程序的另一部分更改了他们的信息,那么将这些更改应用到一个脚本中的论坛帖子将是一件好事。而不是使用标签,请尝试直接检查用户字段的更改:

Perhaps my ternary operator example was incomplete. This was not mentioned in the question, but assuming that users change their info in a separate part of the app, it would be nice to apply those changes to the forum posts in one script. Instead of using tags, try checking the user field directly for changes:

curl -XPOST 'localhost:9200/docs/posts/post/_update' -d '{
    "script" : "ctx._source.nested_user.contains(user) ? "ctx._source.nested_user = updated_nested_John" : ctx.op = "none"",
    "params" : {
        "user": "John",
        "updated_nested_John" : {"field": "updated"}
    }
}'

尽管如此,这可能比重新建立完整的帖子要慢一些。

As mentioned, though, this may be a slower operation than reindexing the full posts.

这篇关于弹性搜索,是否可以更新嵌套对象而不更新整个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆