您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
elasticSearch的parent_id与has_child查询(通过父文档查看子文档、通过子文档查看父文档)
发布时间:2021-08-28 22:40:52编辑:雪饮阅读()
上篇了解了查看一个父子文档在elasticSearch中以该父子关系索引实例中的所有文档以及该实例中某个指定父文档的查询。
那么接下来看看通过parent_id来查看一个指定父文档下面的子文档
请求:
请求体:
{
"query": {
"parent_id": {
"type": "answer",
"id": "question1"
}
}
}
响应体:
{
"took": 184,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.13353139,
"hits": [
{
"_index": "my-index-000001",
"_type": "_doc",
"_id": "answer1",
"_score": 0.13353139,
"_routing": "question1",
"_source": {
"comment": "I am learning ELK",
"username": "Jack",
"my_join_field": {
"name": "answer",
"parent": "question1"
}
}
},
{
"_index": "my-index-000001",
"_type": "_doc",
"_id": "answer2",
"_score": 0.13353139,
"_routing": "question1",
"_source": {
"comment": "I am learning ELK",
"username": "Jack",
"my_join_field": {
"name": "answer",
"parent": "question1"
}
}
}
]
}
}
这里注意关注:
该父文档所在的索引名:
子文档在父子关系中类型名称以及父文档的id:
{
"query": {
"parent_id": {
"type": "answer",
"id": "question1"
}
}
}
接下来我们来看看使用has_child利用一个子文档的查询去查询该子文档的父文档。
请求如:
请求体:
{
"query": {
"has_child": {
"type": "answer",
"query" : {
"match": {
"username" : "Jack"
}
}
}
}
}
响应体:
{
"took": 208,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my-index-000001",
"_type": "_doc",
"_id": "question1",
"_score": 1.0,
"_source": {
"name": "Central School",
"description": "CBSE Affiliation",
"street": "Nagan",
"city": "paprola",
"state": "HP",
"zip": "176115",
"location": [
31.8955385,
76.8380405
],
"fees": 2200,
"tags": [
"Senior Secondary",
"beautiful campus"
],
"rating": "3.3",
"my_join_field": {
"name": "question"
}
}
}
]
}
}
这里需要注意的是:
首先请求url中还是和上面一样,还是需要指定这个父子关系所在的索引名:
然后是has_child中指定type为当前所利用匹配查询的子文档应该所属的在父子关系中的名称
{
"query": {
"has_child": {
"type": "answer",
"query" : {
"match": {
"username" : "Jack"
}
}
}
}
}
以及我们查询这个子文档的条件,这里使用的是query的match去匹配,这里匹配的username字段,其实这个匹配的结果是有多个的,但我这里这多个文档是属于同一个父文档的。
那么这里我们再次创建父文档question2,并创建子文档answer3挂载到这个question2中,那么你并且username也还是Jack,那么接下来上面的通过子文档查看父文档的请求再次发送下,就可以看到响应体变成如:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my-index-000001",
"_type": "_doc",
"_id": "question1",
"_score": 1.0,
"_source": {
"name": "Central School",
"description": "CBSE Affiliation",
"street": "Nagan",
"city": "paprola",
"state": "HP",
"zip": "176115",
"location": [
31.8955385,
76.8380405
],
"fees": 2200,
"tags": [
"Senior Secondary",
"beautiful campus"
],
"rating": "3.3",
"my_join_field": {
"name": "question"
}
}
},
{
"_index": "my-index-000001",
"_type": "_doc",
"_id": "question2",
"_score": 1.0,
"_source": {
"name": "Central School",
"description": "CBSE Affiliation",
"street": "Nagan",
"city": "paprola",
"state": "HP",
"zip": "176115",
"location": [
31.8955385,
76.8380405
],
"fees": 2200,
"tags": [
"Senior Secondary",
"beautiful campus"
],
"rating": "3.3",
"my_join_field": {
"name": "question"
}
}
}
]
}
}
是的,它变成了两个父亲了,其实has_child这里的作用就大致可以总结出来了,它就是通过一个子文档或多个子文档查到他们对应的父文档,若是多个子文档同属于一个父亲,则会将这些相同的结果合并为一个结果,这有点类似mysql中的group by查询了。
关键字词:elasticSearch,parent_id,has_child,通过父文档,通过子文档