Elasticsearch基于子数组子元素的最小值的嵌套排序

编程入门 行业动态 更新时间:2024-10-15 22:29:37
本文介绍了Elasticsearch基于子数组子元素的最小值的嵌套排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个订单,这些订单有多个发货,并且发货有多个产品.如何根据货件中的最小产品数量对订单排序?

I've two orders and these orders have multiple shipments and shipments have multiple products. How can I sort the orders based on the minimum product.quantity in a shipment?

例如.升序订购时,orderNo = 2应该首先列出,因为它的装运中包含product.quantity = 1.(这是所有product.quantity值中的最小值.(productName无关紧要)

For example. When ordering ascending, orderNo = 2 should be listed first because it has a shipment that contains a product.quantity=1. (This is the minimum value among all product.quantity values. (productName doesn't matter)

{ "orders": [ { "orderNo": "1", "shipments": [ { "products": [ { "productName": "AAA", "quantity": "2" }, { "productName": "AAA", "quantity": "2" } ] }, { "products": [ { "productName": "AAA", "quantity": "3" }, { "productName": "AAA", "quantity": "6" } ] } ] }, { "orderNo": "2", "shipments": [ { "products": [ { "productName": "AAA", "quantity": "1" }, { "productName": "AAA", "quantity": "6" } ] }, { "products": [ { "productName": "AAA", "quantity": "4" }, { "productName": "AAA", "quantity": "5" } ] } ] } ] }

推荐答案

假设每个订单都是一个单独的文档,则可以创建一个以订单为中心的索引,其中出货和 products 是嵌套字段以防止数组变平.

Assuming that each order is a separate document, you could create an order-focused index where both shipments and products are nested fields to prevent array flattening.

最小索引映射如下所示:

The minimal index mapping could then look like:

PUT orders { "mappings": { "properties": { "shipments": { "type": "nested", "properties": { "products": { "type": "nested" } } } } } }

下一步是确保数量为始终为数字-而非字符串.完成后,插入上述文档:

The next step is to ensure the quantity is always numeric -- not a string. When that's done, insert said docs:

POST orders/_doc {"orderNo":"1","shipments":[{"products":[{"productName":"AAA","quantity":2},{"productName":"AAA","quantity":2}]},{"products":[{"productName":"AAA","quantity":3},{"productName":"AAA","quantity":6}]}]} POST orders/_doc {"orderNo":"2","shipments":[{"products":[{"productName":"AAA","quantity":1},{"productName":"AAA","quantity":6}]},{"products":[{"productName":"AAA","quantity":4},{"productName":"AAA","quantity":5}]}]}

最后,您可以使用 嵌套排序:

Finally, you can use nested sorting:

POST orders/_search { "sort": [ { "shipments.products.quantity": { "nested": { "path": "shipments.products" }, "order": "asc" } } ] }

提示:为使查询更加有用,您可以引入对 inner_hits 进行排序,不仅可以对顶级订单进行排序,还可以对包含在给定订单中的单个产品进行排序.这些内部匹配需要嵌套查询,因此您可以在 shipments.products.quantity 上简单地添加非负条件.


Tip: To make the query even more useful, you could introduce sorted inner_hits to not only sort the top-level orders but also the individual products enclosed in a given order. These inner hits need a nested query so you could simply add a non-negative condition on shipments.products.quantity.

当您将此查询与上面的排序结合使用时,仅将响应限制为仅相关的属性和 filter_path :

When you combine this query with the above sort and restrict the response to only relevant attributes with filter_path:

POST orders/_search?filter_path=hits.hits._id,hits.hits._source.orderNo,hits.hits.inner_hits.*.hits.hits._source { "_source": ["orderNo", "non_negative_quantities"], "query": { "nested": { "path": "shipments.products", "inner_hits": { "name": "non_negative_quantities", "sort": { "shipments.products.quantity": "asc" } }, "query": { "range": { "shipments.products.quantity": { "gte": 0 } } } } }, "sort": [ { "shipments.products.quantity": { "nested": { "path": "shipments.products" }, "order": "asc" } } ] }

您最终将得到排序订单和排序产品:

{ "hits" : { "hits" : [ { "_id" : "gVc0BHgBly0XYOUcZ4vd", "_source" : { "orderNo" : "2" <--- }, "inner_hits" : { "non_negative_quantities" : { "hits" : { "hits" : [ { "_source" : { "quantity" : 1, <--- "productName" : "AAA" } }, { "_source" : { "quantity" : 4, <--- "productName" : "AAA" } }, { "_source" : { "quantity" : 5, <--- "productName" : "AAA" } } ] } } } }, { "_id" : "gFc0BHgBly0XYOUcYosz", "_source" : { "orderNo" : "1" }, "inner_hits" : { "non_negative_quantities" : { "hits" : { "hits" : [ { "_source" : { "quantity" : 2, "productName" : "AAA" } }, { "_source" : { "quantity" : 2, "productName" : "AAA" } }, { "_source" : { "quantity" : 3, "productName" : "AAA" } } ] } } } } ] } }

更多推荐

Elasticsearch基于子数组子元素的最小值的嵌套排序

本文发布于:2023-11-29 11:56:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   最小值   元素   Elasticsearch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!