您的位置:首页 > 其它

elastcisearch 嵌套查询

2017-12-01 00:46 190 查看
查找官方文档

https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-sort.html#_nested_sorting_example

还可以参见

http://blog.csdn.net/u012332735/article/details/62222953

PUT /testnexted/my_type/1?refresh
{
"product": "xxx",
"offer": [{
"price": 45,
"color": "blue"
},
{
"price": 18,
"color": "red"
},
{
"price": 45,
"color": "red"
},
{
"price": 28,
"color": "red"
}
]
}

PUT /testnexted/my_type/2?refresh
{
"product": "xxx56",
"offer": [{
"price": 23,
"color": "blue"
},
{
"price": 10,
"color": "red"
},
{
"price": 58,
"color": "red"
},
{
"price": 32,
"color": "red"
}
]
}

//defined
PUT /testnexted/
{
"mappings": {
"my_type": {
"properties": {
"offer": { "type": "nested" },
"price": {
"type": "integer"
},
"color": {
"type": "keyword"
}
}
}
}
}

//search
POST /testnexted/_search?pretty

//sort
POST /testnexted/_search
{
"sort" : [
{
"offer.price" : {
"order" : "asc",
"nested_path" : "offer",
"nested_filter" : {
"term" : { "offer.color" : "blue" }
}
}
}
]

}

//数组求平均值在排序
POST /testnexted/_search
{
"sort" : [
{
"offer.price" : {
"mode" :  "avg",
"order" : "asc",
"nested_path" : "offer",
"nested_filter" : {
"term" : { "offer.color" : "blue" }
}
}
}
]

}


结果

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "testnexted",
"_type": "my_type",
"_id": "2",
"_score": null,
"_source": {
"product": "xxx56",
"offer": [
{
"price": 23,
"color": "blue"
},
{
"price": 10,
"color": "red"
},
{
"price": 58,
"color": "red"
},
{
"price": 32,
"color": "red"
}
]
},
"sort": [
23
]
},
{
"_index": "testnexted",
"_type": "my_type",
"_id": "1",
"_score": null,
"_source": {
"product": "xxx",
"offer": [
{
"price": 45,
"color": "blue"
},
{
"price": 18,
"color": "red"
},
{
"price": 45,
"color": "red"
},
{
"price": 28,
"color": "red"
}
]
},
"sort": [
45
]
}
]
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  文档