How to join two indexes in ElasticSearch

I have two indexes that need to be divided:

// 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 when searching so that it returns something row by row:

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"
}
+4
source share
1 answer

As pointed out in another question , nothing prevents you from storing the name Customerinside each document order_itemduring indexing, but it also has a dedicated index orders, which also contains data Customer. Remember that all this is connected with the mental denormalization of your data, so that each of your documents is "self-sufficient" 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"
}

, , / OrderID, .

, @JohnAment , order/order_item

order...

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

order_item "children", , :

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

Source: https://habr.com/ru/post/1613128/


All Articles