1.内嵌对象nested object
1)初始化mapping
PUT /user_index
{
"mappings": {
"properties": {
"login_name": {
"type": "keyword"
},
"age": {
"type": "short"
},
"address": {
"type": "nested",
"properties": {
"province": {
"type": "keyword"
},
"city": {
"type": "keyword"
},
"street": {
"type": "keyword"
}
}
}
}
}
}
2)初始化数据
PUT /user_index/_doc/1
{
"login_name": "jack",
"age": 25,
"address": [
{
"province": "北京",
"city": "北京",
"street": "枫林三路"
},
{
"province": "天津",
"city": "天津",
"street": "华夏路"
}
]
}
PUT /user_index/_doc/2
{
"login_name": "rose",
"age": 21,
"address": [
{
"province": "河北",
"city": "廊坊",
"street": "燕郊经济开发区"
},
{
"province": "天津",
"city": "天津",
"street": "华夏路"
}
]
}
3)搜索 (需要使用nested对应的搜索语法来执行搜索了),语法如下:
GET /user_index/_search
{
"query": {
"nested": {
"path": "address",
"query": {
"bool": {
"must": [
{
"match": {
"address.province": "天津"
}
}
],
"should": [
{
"match": {
"address.city": "北京"
}
}
]
}
}
}
}
}