【问题标题】:Bulk query in Elastic search弹性搜索中的批量查询
【发布时间】:2017-03-29 17:21:45
【问题描述】:

我们有一个包含 100 万条记录的数据库,我们想使用 UserID 查询电子邮件列表。

在 Elastic 搜索中最好的方法是什么。我们不想循环单个 UID 并获取相应的电子邮件。如果我们可以通过一次批量搜索获得所有电子邮件,那就太好了。

欢迎提出任何想法。

【问题讨论】:

  • 也许你应该展示你当前的数据库模式是什么样的,以及你如何查询它来实现你所需要的。也可能是为了其他有类似需求的人的利益,请解释您为什么要采用这种方法(性能等)
  • 你的 ES 文档的架构是什么?每个文档是否包含电子邮件和用户 ID 字段? database 是指弹性搜索吗?
  • 根据您希望通过电子邮件发送的 UID 的数量,可以将其作为单个查询。理想情况下,您会想要进行过滤搜索。如果您使用的是 2.0 之前的 elasticsearch 版本。根据您的架构,可能有一些方法可以加快速度,但这些类型的查询通常会出现问题。

标签: elasticsearch elasticsearch-query


【解决方案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"]
}

我推荐第一个。

【讨论】:

  • 请注意,您需要将_search 端点添加到您的网址
【解决方案2】:

您可以为此使用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": [
          { ... }
        ]
      }
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2020-09-12
    相关资源
    最近更新 更多