问题描述
我有两个索引必须分开:
// index =`order_item`
{
ID:1,
名称:鞋,
价格:9.99,
OrderID:82
},{
ID:1,
名称:帽子,
价格:19.99,
OrderID:82
}
// index =`order`
{
ID:82,
客户:John Smith
}
我如何在搜索上加入这两个表格,以便返回以下一行:
results = {
ID:1,
姓名:鞋,
价格:9.99 ,
Order.ID:82,
客户:John Smith
},{
ID:1,
Name 帽子,
价格:19.99,
Order.ID:82,
客户:约翰·史密斯
}
正如,没有任何东西阻止您在每个<$中存储客户
名称c $ c> order_item 文档在索引时,仍然有一个专用索引订单
还包含客户
数据。请记住,这是关于巧妙地对数据进行非规范化,以使您的每个文档都符合您的需求。
curl -XPUT localhost:9200 / order_items / order_item / 1 -d'{
ID:1,
名称:鞋子,
价格:9.99,
OrderID:82,
客户:约翰·史密斯
}
curl -XPUT localhost:9200 / order_items / order_item / 2 -d'{
ID:2,
名称:帽子,
Price:19.99,
OrderID:82,
Customer:John Smith
}
此解决方案的优点在于,每个订单项目都是完全独立的,您可以在 OrderID
为了获得给定订单的所有项目。
另外,作为他的评论中提到的@JohnAment, order / order_item
用例也是使用
- 或。
在第一种情况下,您将有一个订单
父文档...
curl -XPUT localhost:9200 / orders / order / 82 -d'{
ID:82,
客户:约翰·史密斯
}'
还有几个 order_item
您使用其父代码索引的儿童文档:
curl -XPUT localhost:9200 / order_items / order_item / 1?parent = 82 -d'{
ID:1,
Name:Shoes,
Price:9.99
}
curl -XPUT localhost:9200 / order_items / order_item / 2?parent = 82 -d'{
ID:2,
名称:帽子,
价格:19.99
}'
在第二种情况下,您的订单
文档将包含嵌套的 OrderItems
属性和woul中的所有订单项d看起来像这样:
curl -XPUT localhost:9200 / orders / order / 82 -d'{
ID $
ID:1,
名称:$鞋子
价格:9.99
},{
ID:2,
名称:帽子,
价格:19.99
}
]
}'
I have two indexes that must be separated:
// 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"
}
How would I 'join' these two tables on a search, such that it would return something along the lines of:
results = {
"ID": 1,
"Name": "Shoes",
"Price": 9.99,
"Order.ID": 82,
"Customer": "John Smith"
},{
"ID": 1,
"Name": "Hat",
"Price": 19.99,
"Order.ID": 82,
"Customer": "John Smith"
}
As answered in your other question, nothing prevents you from storing the Customer
name inside each order_item
document at indexing time, while still having a dedicated index orders
also containing the Customer
data. Remember that it's all about cleverly denormalizing your data so that each of your documents be as "self-contained" as you need.
curl -XPUT localhost:9200/order_items/order_item/1 -d '{
"ID": 1,
"Name": "Shoes",
"Price": 9.99,
"OrderID": 82,
"Customer": "John Smith"
}'
curl -XPUT localhost:9200/order_items/order_item/2 -d '{
"ID": 2,
"Name": "Hat",
"Price": 19.99,
"OrderID": 82,
"Customer": "John Smith"
}
The advantages of this solution is that each order item is completely self-contained, and you can group/aggregate them on OrderID
in order to get all items of a given order.
Also, as @JohnAment mentioned in his comment, the order/order_item
use case is also a good candidate for using either
In the first case, you'd have one order
"parent" document...
curl -XPUT localhost:9200/orders/order/82 -d '{
"ID": 82,
"Customer": "John Smith"
}'
And several order_item
"children" documents that you index using their parent ID:
curl -XPUT localhost:9200/order_items/order_item/1?parent=82 -d '{
"ID": 1,
"Name": "Shoes",
"Price": 9.99
}'
curl -XPUT localhost:9200/order_items/order_item/2?parent=82 -d '{
"ID": 2,
"Name": "Hat",
"Price": 19.99
}'
In the second case, your order
document would contain all order items in a nested OrderItems
property and would look like this:
curl -XPUT localhost:9200/orders/order/82 -d '{
"ID": 82,
"Customer": "John Smith"
"OrderItems": [
{
"ID": 1,
"Name": "Shoes",
"Price": 9.99
},{
"ID": 2,
"Name": "Hat",
"Price": 19.99
}
]
}'
这篇关于如何在ElasticSearch中加入两个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!