我有两个必须分开的索引:
- // index = `order_item`
- {
- "ID": 1,"Name": "Shoes","Price": 9.99,"OrderID": 82
- },{
- "ID": 1,"Name": "Hat","Price": 19.99,"OrderID": 82
- }
- // index = `order`
- {
- "ID": 82,"Customer": "John Smith"
- }
- results = {
- "ID": 1,"Order.ID": 82,"Customer": "John Smith"
- },"Customer": "John Smith"
- }
解决方法
正如在
your other question中所回答的,没有什么可以阻止您在索引时将每个order_item文档中的客户名称存储,同时仍然具有包含客户数据的专用索引订单.请记住,这一切都是为了巧妙地对数据进行非规范化,以便您的每个文档都可以根据需要“自包含”.
- curl -XPUT localhost:9200/order_items/order_item/1 -d '{
- "ID": 1,"OrderID": 82,"Customer": "John Smith"
- }'
- curl -XPUT localhost:9200/order_items/order_item/2 -d '{
- "ID": 2,"Customer": "John Smith"
- }
此解决方案的优点是每个订单项都是完全自包含的,您可以在OrderID上对它们进行分组/聚合,以获取给定订单的所有项目.
另外,正如@JohnAment在他的评论中提到的,order / order_item用例也是使用它们的一个很好的候选者
> parent/child relationship
>或nested objects.
在第一种情况下,你有一个订单“父”文件……
- curl -XPUT localhost:9200/orders/order/82 -d '{
- "ID": 82,"Customer": "John Smith"
- }'
以及使用其父ID编制索引的几个order_item“子”文档:
- curl -XPUT localhost:9200/order_items/order_item/1?parent=82 -d '{
- "ID": 1,"Price": 9.99
- }'
- curl -XPUT localhost:9200/order_items/order_item/2?parent=82 -d '{
- "ID": 2,"Price": 19.99
- }'
在第二种情况下,您的订单文档将包含嵌套的OrderItems属性中的所有订单商品,如下所示:
- curl -XPUT localhost:9200/orders/order/82 -d '{
- "ID": 82,"Customer": "John Smith"
- "OrderItems": [
- {
- "ID": 1,"Price": 9.99
- },{
- "ID": 2,"Price": 19.99
- }
- ]
- }'