2015-12-30 122 views
3

我們擁有一個包含100萬條記錄的數據庫,我們希望使用UserID查詢電子郵件列表。彈性搜索中的批量查詢

什麼是在彈性搜索中做到這一點的最佳方式。我們不想循環個人UID並獲得相應的電子郵件。如果我們可以通過一次批量搜索獲得所有電子郵件,那將是非常棒的。

歡迎任何想法。

+0

也許你應該顯示您當前的數據庫架構的樣子,你會如何查詢到實現你所需要的。也許爲了其他有類似需求的人的利益,請解釋你爲什麼要採用這種方法(性能等) – Val

+0

你的ES文檔的模式是什麼?每個文件是否包含電子郵件和用戶ID字段?通過'數據庫'你是指elasticsearch? –

+0

根據您希望發送電子郵件的UID數量,它可能作爲單個查詢。理想情況下,你會想做一個過濾搜索。如果您使用的是elasticsearch的pre 2.0版本。根據您的架構,可能有多種方法可以加快速度,但這些類型的查詢通常是有問題的。 – sbochins

回答

1

你可以嘗試這樣。

POST localhost:9200/users/user/_search?pretty=true 
{ 
    "_source": "email", 
    "query" : { 
     "match" : { "userId" : "abc123" } 
    } 
} 

POST localhost:9200/users/user/_search?pretty=true 
{ 
    "query" : { 
      "match" : { "userId":"abc123" } 
     }, 
     "fields": ["email"] 
} 

我建議第一個。

+0

請注意,您需要將'_search'端點添加到您的網址 – Val

0

您可以使用Multi Search API用於此目的:

curl -s -XGET localhost:9200/_msearch/template -d ' 
{"index" : "logstash-2017.03.20"} 
{"inline": {"query": {"match": {"uid" : "E434C35-B080-403C-ADA9-2FD164CF70" }}}} 
{"index" : "logstash-2017.03.20"} 
{"inline": {"query": {"match": {"uid" : "E1D65ED3-F3BE-42E8-AF2F-A4D4F843F7" }}}} 
' 

注意:每個搜索命令(對索引和查詢線)必須通過新的線分開一個也最後一次查詢後一條新線路必須是當下。將查詢寫入文件可能更安全,例如requests然後用--data-binary標誌:

curl -s -XGET localhost:9200/_msearch/template --data-binary "@requests" 

您將獲得responses的數組,每個查詢:

{ 
    "responses": [ 
    { 
     "took": 86, 
     "timed_out": false, 
     "_shards": { 
     "total": 3, 
     "successful": 3, 
     "failed": 0 
     }, 
     "hits": { 
     "total": 1, 
     "max_score": 13.081283, 
     "hits": [ 
      { ... } 
     ] 
     } 
    }, 
    { 
     "took": 82, 
     "timed_out": false, 
     "_shards": { 
     "total": 3, 
     "successful": 3, 
     "failed": 0 
     }, 
     "hits": { 
     "total": 1, 
     "max_score": 13.081283, 
     "hits": [ 
      { ... } 
     ] 
     } 
    } 
    ] 
}