In Elasticsearch, can multiple top-level documents share a single nested document?

自古美人都是妖i 提交于 2019-12-08 08:43:49

问题


Elasticsearch has nested documents (great). I'd like to use this to store Messages (top-level document) and their Authors (nested document).

Since one author can have many many messages -- can one version of the Author be referenced as the child of multiple Messages?

That way, if you update the Author data in one place, it would update everywhere they're referenced.

NB: This is related to: How to do a join in Elasticsearch -- or at the Lucene level -- an answer here might solve that too.


回答1:


You may want to take a look using a _parent mapping: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-parent-field.html

This allows you to create a type for an Author and a separate type for a Message (with _parent type of Author), then index documents separately and add to the messages over time. You only need to update the single version of an Author for it to affect queries for all messages with that Author.

To achieve queries of messages with a specific author you'll need to use the has_parent query or filter. Or the reverse, use has_child to find authors with certain messages.

  • has_child results in parent documents that have child documents that match the query
  • has_parent results in child documents that have the parent documents that match the query

I have been using this more dynamic form rather than nested documents and it has worked well for me (for both queries and facets), but you must take care defining your mappings before loading any documents of that type, since adding the _parent mapping after the fact doesn't seem to work for me. And reindexing has been something I have failed to manage so far.



来源:https://stackoverflow.com/questions/19524163/in-elasticsearch-can-multiple-top-level-documents-share-a-single-nested-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!