通过regexp在ElasticSearch中搜索电子邮件地址

时间:2017-03-28 14:43:56

标签: elasticsearch

如何通过regexp搜索Elastic来查找电子邮件地址?

我试着这样:

{
  "query": {
    "regexp": {
      "data": {
        "value": "[\\w\\'\\.\\_\\+\\-]+@[\\w]+[\\w\\-\\.]*\\.[\\w]+",
        "flags": "ALL"
      }
    }
  }
}

但我一无所获。

https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-regexp-query.html写的simbol @表示任何符号,因此它应该被转义。

因此我尝试这样:

{
      "query": {
        "regexp": {
          "data": {
            "value": "\\@",
            "flags": "ALL"
          }
        }
      }
    }

和那样:

{
          "query": {
            "regexp": {
              "data": {
                "value": "\\@",
                "flags": "ALL"
              }
            }
          }
        }

但我什么都没得到。

有什么想法吗?

更新 我在Ubuntu Ubuntu 16.04.2 LTS上使用5.2版本的弹性搜索。

数据样本:

curl -XPOST 'localhost:9200/my_index/index_type/_bulk?pretty' -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"data":"some text some text some text some text admin@company.comsome text some text some text "}
{"index":{"_id":"2"}}
{"data":"some text some text hr@company.comsome text some text "}
{"index":{"_id":"3"}}
{"data":"some text some text webmaster@company.comsome text some text "}

并搜索查询:

curl -XGET 'localhost:9200/my_index/index_type/_search?pretty' -H 'Content-Type: application/json' -d'
{
   "query": {
       "regexp": {
          "data": {
            "value": "\\@",
            "flags": "ALL"
          }
       }
   }
}
'

1 个答案:

答案 0 :(得分:1)

使用"[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]+" regexp查找电子邮件地址。然后运行以下查询查找所有电子邮件地址

GET /company/employee/_search
{
   "query": {
       "regexp": {
          "data": {
            "value": "[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]+",
            "flags": "ALL"
          }
       }
   }
}

此处company为索引,employee为类型,data为字段名称,然后插入以下数据以检查您的查询是否有效:

POST /company/employee/_bulk
{"index":{"_id":"1"}}
{"data":"admin@company.com"}
{"index":{"_id":"2"}}
{"data":"hr@company.com"}
{"index":{"_id":"3"}}
{"data":"webmaster@company.com"}
{"index":{"_id":"4"}}
{"data":"abc@company.com"}
{"index":{"_id":"5"}}
{"data":"ahmed@company.com"}
{"index":{"_id":"6"}}
{"data":"md@company.com"}
{"index":{"_id":"7"}}
{"data":"boss@company.com"}
{"index":{"_id":"8"}}
{"data":"amd@company.com"}
{"index":{"_id":"9"}}
{"data":"ad@company.com"}
{"index":{"_id":"10"}}
{"data":"ed@company.com"}
{"index":{"_id":"11"}}
{"data":"etc@company.com"}
{"index":{"_id":"12"}} 
{"data":"f23f23f23f23f23 d32d23d32d d32d2 3d 23"} 
{"index":{"_id":"13"}} 
{"data":"d23d32 d32d23d32 etc@company.com d3d23d23"}