『备忘录』elasticsearch 去重分页查询
一开始数据结构设计的很复杂,又是父子关系又是嵌套关系,结果发现不能通过简单的查询得到想要的结果:比如一个商店只出现一件符合条件的商品,弄得查询语句就变成这样了
curl -XPOST http://localhost:9200/bnb_shop_room/_search -H 'Content-Type:application/json' -d' { "query":{ "bool":{ "must":[ {"match":{"shop_name":"民宿"}}, {"has_child":{ "type":"roomBase", "query": { "bool":{ "must":[ {"has_child": { "type":"roomPrice", "query":{ "bool":{ "must":[ { "nested":{ "path":"room_prices", "query": { "bool":{ "must":[ { "range":{ "room_prices.date":{ "gt":"2018-03-24", "lt":"2019-09-26" } } }, { "term":{ "room_prices.status":1 } }, { "range":{ "room_prices.price":{ "gt":0 } } } ] } } } } ] } } } } ] } } } } ] } }, "size":5, "from":0, "sort": [ { "_geo_distance": { "shop_location" : { "lat" : "22.5971370000", "lon" : "114.5201730000" }, "order": "asc", "unit": "m" } } ], "aggs": { "shop": { "terms": { "field" : "shop_id" }, "aggs": { "room": { "children": { "type": "roomBase" }, "aggs": { "price": { "children": { "type": "roomPrice" }, "aggs": { "nestedPrice": { "nested": { "path": "room_prices" }, "aggs": { "statsPrice": { "stats": { "field": "room_prices.price" } } } } } } } } } } } }'
最后改了索引信息的结构,不做关联关系,全部平铺,冗余数据,发现查询语句简单多了,查询语句变成如下:
curl -XPOST http://localhost:9200/shop_room_day/_doc/_search -H 'Content-Type:application/json' -d' { "query":{ "bool":{ "must":[ {"match":{"shop_name":"主题 深圳"}}, { "range":{ "day_date":{ "gt":"2018-03-27", "lt":"2018-03-29" } } }, { "term":{ "day_status":1 } }, { "range":{ "day_price":{ "gt":0 } } } ] } }, "sort": [ {"day_price":{"order":"asc"}}, { "_geo_distance": { "shop_location" : { "lat" : "22.5971370000", "lon" : "114.5201730000" }, "order": "asc", "unit": "m" } } ], "collapse": { "field": "shop_id" }, "size":2, "from":0 }'
用 『collapse』功能能从搜索结果中去重,翻页靠『size』和『from』组合,当然也可以用『scroll』的方式实现翻页效果
折叠『collapse』功能推荐这篇案例文章:「Elasticsearch 5.x 字段折叠的使用」 https://elasticsearch.cn/article/132
分页『scroll』功能推荐这篇案例文章:「elasticsearch 深分页问题以及解决方法」https://blog.csdn.net/wild46cat/article/details/64123353