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

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

问题描述

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

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 答案了解完整详情.

See this SO answer for full detail.

注意更新脚本支持条件逻辑,如图这里.因此,您可以在用户更改时标记论坛帖子,然后遍历帖子以仅更新用户更改的帖子.

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天全站免登陆