试试搜索一下吧

使用join查询,现查询子文档,怎么返回子文档相应的父文档呢?

Elasticsearch | 作者 hubiao | 发布于2020年01月16日 | 阅读数:1769

使用join查询,现查询子文档,结果都是子文档,然后数据需要从子文档相应的父文档取几个字段,
怎么返回子文档对应的父文档呢?
 
 
GET /my-index/_search
{
"query": {
"parent_id": {
"type": "my-child",
"id": "1"
}
}
}

 
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: hubiao


加个 inner_hits: https://www.elastic.co/guide/e ... .html
DELETE my_index
#1、create index
PUT my_index
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
}
}
}
}

# 2、new add parent document
PUT my_index/_doc/1?refresh
{
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}

PUT my_index/_doc/2?refresh
{
"text": "This is another question",
"my_join_field": {
"name": "question"
}
}

# 3、create child document
PUT my_index/_doc/3?routing=1&refresh
{
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}

PUT my_index/_doc/4?routing=1&refresh
{
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}


GET my_index/_mapping
#4、get child document by parent document
GET my_index/_search
{
"query": {
"has_parent" : {
"parent_type" : "question",
"query" : {
"match_phrase": {
"text": "This is a question"
}
},
"inner_hits": {}
}
}
}

hubiao

赞同来自:

顶一下哦,求关注

要回复问题请先登录注册