用于电子邮件的 ElasticSearch Analyzer 和 Tokenizer

ElasticSearch Analyzer and Tokenizer for Emails

对于以下情况,我在Google或ES中都找不到完美的解决方案,希望有人能在这里提供帮助。

假设字段 "email" 下存储了五个电子邮件地址:

1. {"email": "john.doe@gmail.com"}
2. {"email": "john.doe@gmail.com, john.doe@outlook.com"}
3. {"email": "hello-john.doe@outlook.com"}
4. {"email": "john.doe@outlook.com}
5. {"email": "john@yahoo.com"}

我要完成以下搜索场景:

[搜索 -> 接收]

"john.doe@gmail.com" -> 1,2

"john.doe@outlook.com" -> 2,4

"john@yahoo.com" -> 5

"john.doe" -> 1,2,3,4

"john" -> 1,2,3,4,5

"gmail.com" -> 1,2

"outlook.com" -> 2,3,4

前三个匹配是必须的,其余匹配越精确越好。已经尝试了 index/search 分析器、分词器和过滤器的不同组合。也尝试过匹配查询的条件,但没有找到理想的解决方案,欢迎任何想法,并且不限制映射、分析器或使用哪种查询,谢谢。

映射:

PUT /test
{
  "settings": {
    "analysis": {
      "filter": {
        "email": {
          "type": "pattern_capture",
          "preserve_original": 1,
          "patterns": [
            "([^@]+)",
            "(\p{L}+)",
            "(\d+)",
            "@(.+)",
            "([^-@]+)"
          ]
        }
      },
      "analyzer": {
        "email": {
          "tokenizer": "uax_url_email",
          "filter": [
            "email",
            "lowercase",
            "unique"
          ]
        }
      }
    }
  },
  "mappings": {
    "emails": {
      "properties": {
        "email": {
          "type": "string",
          "analyzer": "email"
        }
      }
    }
  }
}

测试数据:

POST /test/emails/_bulk
{"index":{"_id":"1"}}
{"email": "john.doe@gmail.com"}
{"index":{"_id":"2"}}
{"email": "john.doe@gmail.com, john.doe@outlook.com"}
{"index":{"_id":"3"}}
{"email": "hello-john.doe@outlook.com"}
{"index":{"_id":"4"}}
{"email": "john.doe@outlook.com"}
{"index":{"_id":"5"}}
{"email": "john@yahoo.com"}

要使用的查询:

GET /test/emails/_search
{
  "query": {
    "term": {
      "email": "john.doe@gmail.com"
    }
  }
}