2015-12-30 120 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": [ 
      { ... } 
     ] 
     } 
    } 
    ] 
}