【问题标题】:Elastic search finding multiple exact values query弹性搜索查找多个精确值查询
【发布时间】:2016-01-06 00:10:37
【问题描述】:

我有数据存储在这样的弹性索引中

{'name': 'Arnie Metz PhD', 'user_id': 'CL_000960', 'email_id': 'streich.anjelica@gmail.com', 'customer_id': 'CL_2135514566_1427476813'}
{'name': 'Ms. Princess Bernhard', 'user_id': 'CL_000972', 'email_id': 'obatz@yahoo.com', 'customer_id': 'CL_2135514566_1427476810'}
{'name': "Lori O'Kon", 'user_id': 'CL_000980', 'email_id': 'murl86@schmidt.com', 'customer_id': 'CL_2135514566_1427476811'}
{'name': "Ahmad O'Reilly", 'user_id': 'CL_000981', 'email_id': 'kassie95@yahoo.com', 'customer_id': 'CL_2135514566_1427476815'}
{'name': 'Lovell Connelly', 'user_id': 'CL_000982', 'email_id': 'wweimann@mclaughlincorwin.com', 'customer_id': 'CL_2135514566_1427476815'}
{'name': 'Errol Feest', 'user_id': 'CL_000989', 'email_id': 'cordella30@yahoo.com', 'customer_id': 'CL_2135514566_1427476810'}
{'name': "May O'Conner", 'user_id': 'CL_000990', 'email_id': 'iverson51@gmail.com', 'customer_id': 'CL_2135514566_1427476815'}
{'name': 'Virgie Wyman', 'user_id': 'CL_000999', 'email_id': 'florine.jenkins@yahoo.com', 'customer_id': 'CL_2135514566_1427476812'}
{'name': 'Ofelia McClure', 'user_id': 'CL_0001001', 'email_id': 'fidelia.hilll@mayert.com', 'customer_id': 'CL_2135514566_1427476814'}
{'name': 'Mr. Edson Rosenbaum Jr.', 'user_id': 'CL_0001003', 'email_id': 'mkerluke@hotmail.com', 'customer_id': 'CL_2135514566_1427476810'}

我想从查询中得到的是使用以下查询的 user_ids 列表中的电子邮件 ID 列表

查询 1

根据Elastic Doc

{
  "query" : {
    "filtered" : {
        "filter" : {
            "terms" : {
                "user_id" : ["CL_0004430", "CL_0004496"]
            }
        }
     }
   }
 }

这没有给出结果。它给出了空结果

查询 2

{
 "query": {
   "bool": {
     "must": [
      {
        "match": {
          "user_id": {
          "query": "['CL_00078','CL_00028']",
          "operator": "or"
          }
        }
      }
    ]
  }
 },
 "aggs": {}
}

这按预期工作,但问题在于条件参数的限制。我不能在列表中提供超过 1000 封电子邮件。

有没有更好的查询方法来获得超过 10000 条记录?

【问题讨论】:

  • 也许我错了。问题是制作超过 10000 封电子邮件的过滤器或将它们放入结果中?
  • 也许您的查询 1 可以使用小写字母 (CL_0004430 => cl_0004430)。 terms 是严格的比较,你的映射可能是这个字段的小写(它是默认解析器完成的)。 match 不区分大小写

标签: elasticsearch


【解决方案1】:

这是一个非常好的问题。在存储用户 ID 之类的内容时,通常最好将它们设置为“未分析”。这样,当您对它们进行精确搜索时,您会得到预期的结果。使用以下映射时,您的术语查询按预期工作:

POST test_users
{
  "mappings" :{
    "test_user":{
      "properties": {
        "name": { "type": "string" },
        "user_id": {"type": "string", "index": "not_analyzed"},
        "email_id": {"type": "string", "fields": { "raw": { "type": "string", "index": "not_analyzed" }}},
        "customer_id": { "type": "string", "index": "not_analyzed"}
      }
    }
  }
}

POST _bulk
{"create": {"_index": "test_users", "_type": "test_user" }}
{"name": "Arnie Metz PhD", "user_id": "CL_000960", "email_id": "streich.anjelica@gmail.com", "customer_id": "CL_2135514566_1427476813"}
{"create": {"_index": "test_users", "_type": "test_user" }}
{"name": "Ms. Princess Bernhard", "user_id": "CL_000972", "email_id": "obatz@yahoo.com", "customer_id": "CL_2135514566_1427476810"}

# returns two results.
GET test_users/test_user/_search
{
 "query": {
    "filtered" : {
      "filter" : {
        "terms": {
          "user_id": ["CL_000960","CL_000972"]
        }
      }
    }
  }
}

您需要做的另一件事是在您的 elasticsearch.yml 配置文件中设置 index.query.bool.max_clause_count: 12000(或其他一些大数字)并重新启动您的实例。否则你会得到TooManyClauses[maxClauseCount is set to 1024];

在使用我自己的 ElasticSearch 实例进行试验后,将 10,000 个项目传递到一个 terms 数组中大约需要 1.5 秒才能返回每组 25 个结果。这是在具有 4 核、3.40 GHz 处理器和 8 GB RAM 的桌面工作站上运行的单个节点。因此,您可能需要考虑扫描和滚动类型的查询。

【讨论】:

    【解决方案2】:

    你试过filter or 吗? (我不知道是否有限制,但是在某些低速连接上发送长查询可能会很慢)

    {
        "query" : {
            "filtered" : {
                "filter" : {
                    "or" : [{
                            "term" : {
                                "user_id" : "CL_0004430",
                                "_cache" : false
                            }
                        }, {
                            "term" : {
                                "user_id" : "CL_0004496",
                                "_cache" : false
                            }
                        }
                    ]
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多