2015-10-25 33 views
4

如何「加入」兩個指標,我有兩個指標必須分開:在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" 
} 
+0

不幸的是,它不能做大衛 – eliasah

+0

@eliasah - 我明白了,謝謝。那麼在這裏你會有什麼建議可以作爲我們如何能夠合併數據的。例如,像合併它然後創建第三個索引的處理作業。這裏有什麼可能嗎? – David542

+0

您是否考慮過文檔之間的父母/子女關係? https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html請注意,單個索引不像RDBMS中的表格。 –

回答

9

your other question回答,沒有什麼能阻止你從存儲在索引時間每個order_item文件內Customer名,同時還具有專用索引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. parent/child relationship
  2. nested objects

在第一種情況下,你有一個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 
     } 
    ] 
}'