https://elasticsearch.cn/article/132html
DELETE recipes
PUT recipes
POST recipes/type/_mapping
{
"properties": {
"name":{
"type": "text"
},
"rating":{
"type": "float"
},"type":{
"type": "keyword"
}
}
}
POST recipes/type/
{
"name":"清蒸魚頭","rating":1,"type":"湘菜"
}
POST recipes/type/
{
"name":"剁椒魚頭","rating":2,"type":"湘菜"
}
POST recipes/type/
{
"name":"紅燒鯽魚","rating":3,"type":"湘菜"
}
POST recipes/type/
{
"name":"鯽魚湯(辣)","rating":3,"type":"湘菜"
}
POST recipes/type/
{
"name":"鯽魚湯(微辣)","rating":4,"type":"湘菜"
}
POST recipes/type/
{
"name":"鯽魚湯(變態辣)","rating":5,"type":"湘菜"
}
POST recipes/type/
{
"name":"廣式鯽魚湯","rating":5,"type":"粵菜"
}
POST recipes/type/
{
"name":"魚香肉絲","rating":2,"type":"川菜"
}
POST recipes/type/
{
"name":"奶油鮑魚湯","rating":2,"type":"西菜"
}
POST recipes/type/_search
{
"query": {"match": {
"name": "魚"
}},"size": 3
}
全是湘菜,個人天,最近上火不想吃辣,這個第一頁的結果對我來講就是垃圾,以下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0.26742277,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYF_OA-dG63Txsd",
"_score": 0.26742277,
"_source": {
"name": "鯽魚湯(變態辣)",
"rating": 5,
"type": "湘菜"
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHXO_OA-dG63Txsa",
"_score": 0.19100356,
"_source": {
"name": "紅燒鯽魚",
"rating": 3,
"type": "湘菜"
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHWy_OA-dG63TxsZ",
"_score": 0.19100356,
"_source": {
"name": "剁椒魚頭",
"rating": 2,
"type": "湘菜"
}
}
]
}
}
咱們再看看,此次我想加個評分排序,你們都喜歡的是那些,看看有沒有喜歡吃的,執行查詢:
POST recipes/type/_search
{
"query": {"match": {
"name": "魚"
}},"sort": [
{
"rating": {
"order": "desc"
}
}
],"size": 3
}
結果稍微好點了,不過3個裏面2個是湘菜,仍是有點不合適,結果以下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYF_OA-dG63Txsd",
"_score": null,
"_source": {
"name": "鯽魚湯(變態辣)",
"rating": 5,
"type": "湘菜"
},
"sort": [
5
]
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYW_OA-dG63Txse",
"_score": null,
"_source": {
"name": "廣式鯽魚湯",
"rating": 5,
"type": "粵菜"
},
"sort": [
5
]
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHX7_OA-dG63Txsc",
"_score": null,
"_source": {
"name": "鯽魚湯(微辣)",
"rating": 4,
"type": "湘菜"
},
"sort": [
4
]
}
]
}
}
如今我知道了,我要看看其餘菜系,這家不是還有西餐、廣東菜等各類菜系的麼,來來,幫我每一個菜系來一個菜看看,換 terms agg 先獲得惟一的 term 的 bucket,再組合 top_hits agg,返回按評分排序的第一個 top hits,有點複雜,不要緊,看下面的查詢就知道了:
GET recipes/type/_search
{
"query": {
"match": {
"name": "魚"
}
},
"sort": [
{
"rating": {
"order": "desc"
}
}
],"aggs": {
"type": {
"terms": {
"field": "type",
"size": 10
},"aggs": {
"rated": {
"top_hits": {
"sort": [{
"rating": {"order": "desc"}
}],
"size": 1
}
}
}
}
},
"size": 0,
"from": 0
}
看下面的結果,雖然 json 結構有點複雜,不過總算是咱們想要的結果了,湘菜、粵菜、川菜、西菜都出來了,每樣一個,不重樣:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "湘菜",
"doc_count": 6,
"rated": {
"hits": {
"total": 6,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYF_OA-dG63Txsd",
"_score": null,
"_source": {
"name": "鯽魚湯(變態辣)",
"rating": 5,
"type": "湘菜"
},
"sort": [
5
]
}
]
}
}
},
{
"key": "川菜",
"doc_count": 1,
"rated": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYr_OA-dG63Txsf",
"_score": null,
"_source": {
"name": "魚香肉絲",
"rating": 2,
"type": "川菜"
},
"sort": [
2
]
}
]
}
}
},
{
"key": "粵菜",
"doc_count": 1,
"rated": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYW_OA-dG63Txse",
"_score": null,
"_source": {
"name": "廣式鯽魚湯",
"rating": 5,
"type": "粵菜"
},
"sort": [
5
]
}
]
}
}
},
{
"key": "西菜",
"doc_count": 1,
"rated": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHY3_OA-dG63Txsg",
"_score": null,
"_source": {
"name": "奶油鮑魚湯",
"rating": 2,
"type": "西菜"
},
"sort": [
2
]
}
]
}
}
}
]
}
}
}
上面的實現方法,前面已經說了,能夠作,有侷限性,那看看新的字段摺疊法如何作到呢,查詢以下,加一個 collapse 參數,指定對那個字段去重就好了,這裏固然對菜系「type」字段進行去重了:
GET recipes/type/_search
{
"query": {
"match": {
"name": "魚"
}
},
"collapse": {
"field": "type"
},
"size": 3,
"from": 0
}
結果很理想嘛,命中結果仍是熟悉的那個味道(和查詢結果長的同樣嘛),以下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRJ_OA-dG63TxpW",
"_score": 0.018980097,
"_source": {
"name": "鯽魚湯(微辣)",
"rating": 4,
"type": "湘菜"
},
"fields": {
"type": [
"湘菜"
]
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRk_OA-dG63TxpZ",
"_score": 0.013813315,
"_source": {
"name": "魚香肉絲",
"rating": 2,
"type": "川菜"
},
"fields": {
"type": [
"川菜"
]
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRb_OA-dG63TxpY",
"_score": 0.0125863515,
"_source": {
"name": "廣式鯽魚湯",
"rating": 5,
"type": "粵菜"
},
"fields": {
"type": [
"粵菜"
]
}
}
]
}
}
我再試試翻頁,把 from 改一下,如今返回了3條數據,from 改爲3,新的查詢以下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoDNlRw_OA-dG63Txpa",
"_score": 0.012546891,
"_source": {
"name": "奶油鮑魚湯",
"rating": 2,
"type": "西菜"
},
"fields": {
"type": [
"西菜"
]
}
}
]
}
}
上面的結果只有一條了,去重以後原本就只有4條數據,上面的工做正常,每一個菜系只有一個菜啊,那我不樂意了,幫我每一個菜系裏面多返回幾條,我好選菜啊,加上參數 inner_hits 來控制返回的條數,這裏返回2條,按 rating 也排個序,新的查詢構造以下:
GET recipes/type/_search
{
"query": {
"match": {
"name": "魚"
}
},
"collapse": {
"field": "type",
"inner_hits": {
"name": "top_rated",
"size": 2,
"sort": [
{
"rating": "desc"
}
]
}
},
"sort": [
{
"rating": {
"order": "desc"
}
}
],
"size": 2,
"from": 0
}
查詢結果以下,完美:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 9,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYF_OA-dG63Txsd",
"_score": null,
"_source": {
"name": "鯽魚湯(變態辣)",
"rating": 5,
"type": "湘菜"
},
"fields": {
"type": [
"湘菜"
]
},
"sort": [
5
],
"inner_hits": {
"top_rated": {
"hits": {
"total": 6,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYF_OA-dG63Txsd",
"_score": null,
"_source": {
"name": "鯽魚湯(變態辣)",
"rating": 5,
"type": "湘菜"
},
"sort": [
5
]
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHX7_OA-dG63Txsc",
"_score": null,
"_source": {
"name": "鯽魚湯(微辣)",
"rating": 4,
"type": "湘菜"
},
"sort": [
4
]
}
]
}
}
}
},
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYW_OA-dG63Txse",
"_score": null,
"_source": {
"name": "廣式鯽魚湯",
"rating": 5,
"type": "粵菜"
},
"fields": {
"type": [
"粵菜"
]
},
"sort": [
5
],
"inner_hits": {
"top_rated": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "recipes",
"_type": "type",
"_id": "AVoESHYW_OA-dG63Txse",
"_score": null,
"_source": {
"name": "廣式鯽魚湯",
"rating": 5,
"type": "粵菜"
},
"sort": [
5
]
}
]
}
}
}
}
]
}
}
好了,字段摺疊介紹就到這裏。