note
- 干货 | Elasticsearch Nested类型深入详解
- elasticsearch(lucene)使用的库没有内部对象的概念,因此内部对象被扁平化为一个简单的字段名称和值列表。
- 为了解决这个问题,需要用到 nested,nested对象会被作为隐式对象处理。其内容不会被扁平化处理。
- 关联 doc 必须保存于同一个分片,所以在构建 index的时候,需要添加 routing=parent_id 参数
demo
nested
DELETE /netest_index
PUT /netest_index
{
"mappings": {
"properties": {
"title":{
"type":"text"
},
"comments": {
"type": "nested",
"properties": {
"name":{
"type":"text"
},
"count":{
"type":"integer"
}
}
}
}
}
}
POST /netest_index/_doc/1
{
"title":"es",
"comments":[
{
"name": "zhengcj",
"count": 6
},
{
"name":"dongyc",
"count":1
}
]
}
POST /netest_index/_doc/2
{
"title":"java",
"comments": [
{
"name": "zhengcj",
"count": 6
},
{
"name":"ljr",
"count":2
}
]
}
GET /netest_index/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {