【问题标题】:How to use elasticsearch to query email from text with regex如何使用elasticsearch通过正则表达式从文本中查询电子邮件
【发布时间】:2021-09-05 21:55:56
【问题描述】:

我想从存储在es中的文本中查询所有电子邮件,现在我使用了这个查询词并得到了query result

{
"query": {
    "regexp": {
        "sys_content": {
            "value": "[-a-zA-Z0-9_]+(\\.[-a-zA-Z0-9_]+)*@[-a-zA-Z0-9_]+(\\.[-a-zA-Z0-9_]+)+",
            "flags_value": 65535,
            "max_determinized_states": 10000,
            "boost": 1.0
        }
    }
},
"highlight": {
    "pre_tags": [
        "<span style='color:red'>"
    ],
    "post_tags": [
        "</span>"
    ],
    "fragment_size": 100,
    "require_field_match": true,
    "fields": {
        "sys_content": {}
    }
}

}

然后,我尝试查询“\@”却一无所获

【问题讨论】:

    标签: elasticsearch lucene elasticsearch-query


    【解决方案1】:

    这是使用uax url email tokenizer 的解决方案。这将在索引时完成大部分工作,使您的搜索速度更快。

    使用自定义分析器创建索引以创建 令牌和过滤器以仅保留那些 令牌:

    PUT test-index
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "my_tokenizer",
              "filter": ["extract_email"]
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "uax_url_email",
              "max_token_length": 50
            }
          },
          "filter": {
            "extract_email": {
              "type": "keep_types",
              "types": [ "<EMAIL>" ]
            }
          }
        }
      },
      "mappings" : {
          "properties" : {
            "sys_content" : {
              "type" : "text",
              "fields": {
                "email": {
                  "type": "text",
                  "analyzer": "my_analyzer"
                }
              }
            }
          }
        }
    }
    

    然后添加一个文档:

    POST test-index/_doc
    {
      "sys_content": "test email@gmail.com not@ a@a email another@email.fr"
    }
    

    最后搜索并突出显示电子邮件。多亏了 uax url 电子邮件标记器,查找电子邮件已经在索引时完成,因此在搜索时,您只需匹配来自 sys_content.email 字段的任何标记:

    GET test-index/_search
    {
      "query": {
        "regexp": {
          "sys_content.email": {
            "value": ".*",
            "flags": "ALL",
            "case_insensitive": true,
            "max_determinized_states": 10000,
            "rewrite": "constant_score"
          }
        }
      },
      "highlight": {
        "pre_tags": [
            "<span style='color:red'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "fragment_size": 100,
        "require_field_match": true,
        "fields": {
            "sys_content.email": {}
        }
      }
    }
    

    这会产生以下结果:

    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test-index",
            "_type" : "_doc",
            "_id" : "GxSbM3oBJxdf7EzzH4jM",
            "_score" : 1.0,
            "_source" : {
              "sys_content" : "test email@gmail.com not@ a@a email another@email.fr"
            },
            "highlight" : {
              "sys_content.email" : [
                "test <span style='color:red'>email@gmail.com</span> not@ a@a email <span style='color:red'>another@email.fr</span>"
              ]
            }
          }
        ]
      }
    }
    

    注意:必须有更好的方法来匹配字段中的任何标记,而无需使用正则表达式搜索,但我找不到它。无论如何,这行得通,正则表达式非常简单,所以应该很快。

    【讨论】:

    • 感谢您的回复。我想我明白了。
    猜你喜欢
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多