【问题标题】:Emails not being searched properly in elasticsearch在 elasticsearch 中未正确搜索电子邮件
【发布时间】:2016-02-02 07:19:56
【问题描述】:

我已经在 elasticsearch 中索引了一些以电子邮件 ID 作为字段的文档。但是当我查询特定的电子邮件 ID 时,搜索结果显示所有文档而没有过滤。

这是我使用的查询

{
 "query": {
   "match": {
     "mail-id": "abc@gmail.com"
   }
 }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    默认情况下,标准分析器会分析您的mail-id 字段,该分析器会将电子邮件abc@gmail.com 标记为以下两个标记:

    {
      "tokens" : [ {
        "token" : "abc",
        "start_offset" : 0,
        "end_offset" : 3,
        "type" : "<ALPHANUM>",
        "position" : 1
      }, {
        "token" : "gmail.com",
        "start_offset" : 4,
        "end_offset" : 13,
        "type" : "<ALPHANUM>",
        "position" : 2
      } ]
    }
    

    您需要使用UAX email URL tokenizer 创建一个自定义分析器,它将电子邮件地址标记为一个标记。

    所以你需要如下定义你的索引:

    curl -XPUT localhost:9200/people -d '{
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "type": "custom",
              "tokenizer": "uax_url_email"
            }
          }
        }
      },
      "mappings": {
        "person": {
          "properties": {
            "mail-id": {
              "type": "string",
              "analyzer": "my_analyzer"
            }
          }
        }
      }
    }'
    

    创建该索引后,您可以看到电子邮件 abc@gmail.com 将被标记为单个标记,您的搜索将按预期进行。

     curl -XGET 'localhost:9200/people/_analyze?analyzer=my_analyzer&pretty' -d 'abc@gmail.com'
    {
      "tokens" : [ {
        "token" : "abc@gmail.com",
        "start_offset" : 0,
        "end_offset" : 13,
        "type" : "<EMAIL>",
        "position" : 1
      } ]
    }
    

    【讨论】:

      【解决方案2】:

      当您使用默认映射时会发生这种情况。 Elasticsearch 具有 uax_url_email 标记器,可将 url 和电子邮件识别为单个实体/标记。 你可以阅读更多关于这个herehere

      【讨论】:

      • 嗨 Vineet.. 你能看看这个Question
      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2014-07-08
      • 2011-04-10
      • 2018-04-18
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多