小编典典

如何在ElasticSearch中``联接''两个索引

elasticsearch

我有两个必须分开的索引:

// 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,
    "Name": "Shoes",
    "Price": 9.99,
    "Order.ID": 82,
    "Customer": "John Smith"
},{
    "ID": 1,
    "Name": "Hat",
    "Price": 19.99,
    "Order.ID": 82,
    "Customer": "John Smith"
}

阅读 349

收藏
2020-06-22

共1个答案

小编典典

正如您在其他问题中回答的那样,没有什么可以阻止您在建立索引时将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"
}

该解决方案的优点是每个订单商品都是完全独立的,您可以对其进行分组/汇总OrderID以获取给定订单的所有商品。

另外,正如@JohnAment在他的评论中提到的,order/order_item用例也是使用以下任一方法的理想选择

  1. 父/子关系
  2. 嵌套的对象

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

curl -XPUT localhost:9200/orders/order/82 -d '{
    "ID": 82,
    "Customer": "John Smith"
}'

还有几个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
}'

在第二种情况下,您的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
      }
    ]
}'
2020-06-22