嵌套代码如下:
PUT demo { "settings": { "number_of_shards": 4, "number_of_replicas": 1 }, "mappings": { "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" } } } } } } } } POST demo/_doc { "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/_doc { "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": "外国语学院" } ] } ] } GET demo/_doc/_search { "_source": { "includes": [ "*" ], "excludes": [ "university" ] }, "query": { "nested": { "path": "university", "query": { "bool": { "must": [ { "match": { "university.name": "清华" } } ] } }, "inner_hits": {} } } }
对象代码如下:
PUT demo2 { "settings": { "number_of_shards": 4, "number_of_replicas": 1 }, "mappings": { "properties": { "id": { "type": "long" }, "title": { "type": "text" }, "university": { "type": "object", "properties": { "name": { "type": "text" }, "foundingYear": { "type": "date" }, "address": { "type": "text" } } } } } } POST demo2/_doc { "id": 1, "title": "北京市学校信息列表", "university": { "name": "北京大学", "address": "北京市海淀区颐和园路5号", "foundingYear": "1898-06-01" } } GET demo2/_doc/_search { "query": { "bool": { "must": [ { "match": { "university.name": "北京" } } ] } } }