如何知道Elasticsearch上是否存在电子邮件地址?

时间:2018-03-20 07:17:16

标签: api elasticsearch postman

所以我在elasticsearch上添加了一些数据(带有名字的电子邮件地址)。现在想要验证特定的电子邮件地址是否存在。

以下是我通过Postman执行的代码:

**URL :** localhost:9200/demo1/demo_emails/_bulk (Put request)

**Raw Data (json) :** 
{ "create" : { "_index" : "demo1", "_type" : "Supp_emails", "_id" : "2" } }
{ "name" : "x1", "email": "x1@r.com" }
{ "create" : { "_index" : "demo1", "_type" : "Supp_emails", "_id" : "3" } }
{ "name" : "x2", "email": "x2@r.com" }

你可以看到“demo1”是Index& “demo_emails”是字段类型。我在该索引上添加了两个电子邮件地址。 现在想验证'x1@r.com'是否存在?

我已尝试过以下查询,但它显示所有详细信息而不是一封电子邮件

**URL :** localhost:9200/demo1/Supp_emails/_search?q=email:x1@r.com (Get request)

**Output :**
{
"took": 2,
"timed_out": false,
"_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": 2,
    "max_score": 0.87546873,
    "hits": [
        {
            "_index": "demo1",
            "_type": "demo_emails",
            "_id": "1",
            "_score": 0.87546873,
            "_source": {
                "name": "x1",
                "email": "x1@r.com"
            }
        },
        {
            "_index": "demo1",
            "_type": "demo_emails",
            "_id": "2",
            "_score": 0.87546873,
            "_source": {
                "name": "x2",
                "email": "x2@r.com"
            }
        } 

1 个答案:

答案 0 :(得分:0)

当您搜索电子邮件:x1@r.com 时,标记生成器会将字符串分解为标记。这些令牌取决于您使用的分析器和标记器。

我假设你没有做任何花哨的分析,所以这些代币将是: GET index / _analyze

{
  "tokens": [
    {
      "token": "x1",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "r.com",
      "start_offset": 3,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

如果您查看query_string的文档,您会发现默认运算符是OR 因此,您的查询实际上会查找 x1 OR r.com ,这就是这两个文档都返回的原因。

您可以通过添加参数http://elastic...?q= ... &amp; explain = true 来查看此内容。

你如何解决这个问题?使用其他运算符。只需将默认运算符更改为AND,d http://elastic...?q=...& default_operator = true

有用的链接: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices-analyze.html