Parent Id Query

原文链接 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-parent-id-query.html

译文链接 http://www.apache.wiki/pages/createpage.action?spaceKey=Elasticsearch&fromPageId=4260708

贡献者 小蚊子

注意

V5.0.0中新添加

parent_id 查询可以用来查找那些属于一个特殊父类型的子文档。给出如下映射的定义:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -d'
  2. {
  3. "mappings": {
  4. "blog_post": {
  5. "properties": {
  6. "name": {
  7. "type": "keyword"
  8. }
  9. }
  10. },
  11. "blog_tag": {
  12. "_parent": {
  13. "type": "blog_post"
  14. },
  15. "_routing": {
  16. "required": true
  17. }
  18. }
  19. }
  20. }'
  1. curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
  2. {
  3. "query": {
  4. "parent_id" : {
  5. "type" : "blog_tag",
  6. "id" : "1"
  7. }
  8. }
  9. }'

上面的设置与使用下面的 has_parent查询在功能上相似,但是如果不需要做连接的话,下面的操作会更好。

  1. curl -XGET 'localhost:9200/my_index/_search?pretty' -d'
  2. {
  3. "query": {
  4. "has_parent": {
  5. "parent_type": "blog_post",
  6. "query": {
  7. "term": {
  8. "_id": "1"
  9. }
  10. }
  11. }
  12. }
  13. }'

Parameters(参数)

这个查询有两个必备的参数。

type 子类型。必须是一个_parent字段定义的类型
id 选择文档必须参照要求的parent文档id
ignore_unmapped 当设置为true时,这个参数将忽略一个未被映射的类型,这个查询将不匹配任何文档。这个在可能产生不同的映射的多索引查询中起作用。当设置为flase(默认值),如果类型未被映射,则查询将报异常。