Elasticsearch官方说明文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-inner-hits.html
针对 nested 类型字段参数,使用 Inner hits 关键字查询返回其关联子元素,如下面例子所示:
创建名称为 demo,类型为 test_data 的索引
put demo
{
"settings": {
"number_of_shards": 4,
"number_of_replicas": 1
},
"mappings": {
"test_data": {
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "text"
},
"university": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"foundingYear": {
"type": "date"
},
"address": {
"type": "text"
},
"college": {
"type": "nested",
"properties": {
"name": {
"type": "text"
}
}
}
}
}
}
}
}
}
put demo/test_data
{
"id": 1,
"title": "北京市学校信息列表",
"university": [
{
"name": "清华大学",
"address": "北京市海淀区双清路30号",
"foundingYear": "1911-04-09",
"college": [
{
"name": "土木水利学院"
},
{
"name": "信息科学技术学院"
},
{
"name": "材料学院"
}
]
},
{
"name": "北京大学",
"address": "北京市海淀区颐和园路5号",
"foundingYear": "1898-06-01",
"college": [
{
"name": "经济学院"
},
{
"name": "外国语学院"
},
{
"name": "信息科学技术学院"
}
]
}
]
}
put demo/test_data
{
"id": 2,
"title": "上海市学校信息列表",
"university": [
{
"name": "复旦大学",
"address": "上海市杨浦区邯郸路220号",
"foundingYear": "1905-06-29",
"college": [
{
"name": "哲学学院"
},
{
"name": "信息科学与工程学院"
},
{
"name": "计算机科学技术学院"
}
]
},
{
"name": "上海交通大学",
"address": "上海市闵行区东川路800号",
"foundingYear": "1896-12-01",
"college": [
{
"name": "船舶海洋与建筑工程学院"
},
{
"name": "环境科学与工程学院"
},
{
"name": "外国语学院"
}
]
}
]
}
OK,上面两步已经将demo索引创建好了,并且写入了测试数据。
post demo/test_data/_search
{
"_source": {
"includes": [
"*"
],
"excludes": [
"university"
]
},
"query": {
"nested": {
"path": "university",
"query": {
"bool": {
"must": [
{
"match": {
"university.name": "清华"
}
}
]
}
},
"inner_hits": {}
}
}
}
结果为下图
2. 查询学校下的学院(college)name 中包含 “技术” 的信息,并且只返回命中的该学院的信息
post demo/test_data/_search
{
"_source": {
"includes": [
"*"
],
"excludes": [
"university"
]
},
"query": {
"nested": {
"path": "university.college",
"query": {
"bool": {
"must": [
{
"match": {
"university.college.name": "技术"
}
}
]
}
},
"inner_hits": {}
}
}
}
结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 4,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.7368987,
"hits": [
{
"_index": "demo",
"_type": "test_data",
"_id": "AXMxTYI9vSpKwawRb20E",
"_score": 2.7368987,
"_source": {
"id": 2,
"title": "上海市学校信息列表"
},
"inner_hits": {
"university.college": {
"hits": {
"total": 1,
"max_score": 2.7368987,
"hits": [
{
"_nested": {
"field": "university",
"offset": 0,
"_nested": {
"field": "college",
"offset": 2
}
},
"_score": 2.7368987,
"_source": {
"name": "计算机科学技术学院"
}
}
]
}
}
}
},
{
"_index": "demo",
"_type": "test_data",
"_id": "AXMxTENevSpKwawRb20D",
"_score": 1.5730922,
"_source": {
"id": 1,
"title": "北京市学校信息列表"
},
"inner_hits": {
"university.college": {
"hits": {
"total": 2,
"max_score": 1.5730922,
"hits": [
{
"_nested": {
"field": "university",
"offset": 1,
"_nested": {
"field": "college",
"offset": 2
}
},
"_score": 1.5730922,
"_source": {
"name": "信息科学技术学院"
}
},
{
"_nested": {
"field": "university",
"offset": 0,
"_nested": {
"field": "college",
"offset": 1
}
},
"_score": 1.5730922,
"_source": {
"name": "信息科学技术学院"
}
}
]
}
}
}
}
]
}
}
post demo/test_data/_search
{
"_source": {
"includes": [
"*"
],
"excludes": [
"university"
]
},
"query": {
"nested": {
"path": "university",
"query": {
"nested": {
"path": "university.college",
"query": {
"match": {
"university.college.name": "技术"
}
},
"inner_hits": {}
}
},
"inner_hits": {
"_source": {
"excludes": [
"university.college"
]
}
}
}
}
}
结果如下:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 4,
"successful": 4,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.7368987,
"hits": [
{
"_index": "demo",
"_type": "test_data",
"_id": "AXMxTYI9vSpKwawRb20E",
"_score": 2.7368987,
"_source": {
"id": 2,
"title": "上海市学校信息列表"
},
"inner_hits": {
"university": {
"hits": {
"total": 1,
"max_score": 2.7368987,
"hits": [
{
"_nested": {
"field": "university",
"offset": 0
},
"_score": 2.7368987,
"_source": {
"university": {
"address": "上海市杨浦区邯郸路220号",
"foundingYear": "1905-06-29",
"name": "复旦大学"
}
},
"inner_hits": {
"university.college": {
"hits": {
"total": 1,
"max_score": 2.7368987,
"hits": [
{
"_nested": {
"field": "university",
"offset": 0,
"_nested": {
"field": "college",
"offset": 2
}
},
"_score": 2.7368987,
"_source": {
"name": "计算机科学技术学院"
}
}
]
}
}
}
}
]
}
}
}
},
{
"_index": "demo",
"_type": "test_data",
"_id": "AXMxTENevSpKwawRb20D",
"_score": 1.5730922,
"_source": {
"id": 1,
"title": "北京市学校信息列表"
},
"inner_hits": {
"university": {
"hits": {
"total": 2,
"max_score": 1.5730922,
"hits": [
{
"_nested": {
"field": "university",
"offset": 1
},
"_score": 1.5730922,
"_source": {
"university": {
"address": "北京市海淀区颐和园路5号",
"foundingYear": "1898-06-01",
"name": "北京大学"
}
},
"inner_hits": {
"university.college": {
"hits": {
"total": 1,
"max_score": 1.5730922,
"hits": [
{
"_nested": {
"field": "university",
"offset": 1,
"_nested": {
"field": "college",
"offset": 2
}
},
"_score": 1.5730922,
"_source": {
"name": "信息科学技术学院"
}
}
]
}
}
}
},
{
"_nested": {
"field": "university",
"offset": 0
},
"_score": 1.5730922,
"_source": {
"university": {
"address": "北京市海淀区双清路30号",
"foundingYear": "1911-04-09",
"name": "清华大学"
}
},
"inner_hits": {
"university.college": {
"hits": {
"total": 1,
"max_score": 1.5730922,
"hits": [
{
"_nested": {
"field": "university",
"offset": 0,
"_nested": {
"field": "college",
"offset": 1
}
},
"_score": 1.5730922,
"_source": {
"name": "信息科学技术学院"
}
}
]
}
}
}
}
]
}
}
}
}
]
}
}