json 如何在 ElasticSearch 中“加入”两个索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33335303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:06:52  来源:igfitidea点击:

How to 'join' two indexes in ElasticSearch

jsonelasticsearch

提问by David542

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"
}

回答by Val

As answered in your other question, nothing prevents you from storing the Customername inside each order_itemdocument at indexing time, while still having a dedicated index ordersalso containing the Customerdata. Remember that it's all about cleverly denormalizing your data so that each of your documents be as "self-contained" as you need.

正如您在另一个问题中所回答的那样,没有什么可以阻止您在索引时将Customer名称存储在每个order_item文档中,同时仍然有一个orders包含Customer数据的专用索引。请记住,这一切都是关于巧妙地对数据进行非规范化,以便您的每个文档都可以根据需要“自包含”。

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 OrderIDin order to get all items of a given order.

此解决方案的优点是每个订单项目都是完全独立的,您可以对它们进行分组/聚合OrderID以获取给定订单的所有项目。

Also, as @JohnAment mentioned in his comment, the order/order_itemuse case is also a good candidate for using either

此外,正如@JohnAment 在他的评论中提到的,order/order_item用例也是使用任一

  1. the parent/child relationship
  2. or nested objects.
  1. 父/子关系
  2. 嵌套对象

In the first case, you'd have one order"parent" document...

在第一种情况下,您将拥有一个order“父”文档......

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:

以及order_item使用父 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 orderdocument would contain all order items in a nested OrderItemsproperty and would look like this:

在第二种情况下,您的order文档将包含嵌套OrderItems属性中的所有订单项,如下所示:

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
      }
    ]
}'