在ELK中的特定位置匹配子字符串电子邮件地址

时间:2020-07-07 13:26:24

标签: elasticsearch lucene kibana amazon-elasticsearch

我正在尝试从ELK Kibana发现部分的消息字段中查找与电子邮件匹配的数据,我使用以下方法获取结果:

@message:"abc@email.com"

但是,生成的结果包含一些其他消息,其中电子邮件不匹配,我无法为此解决方案。

结果是(出于安全原因对数据进行了清除 ):

@message:[INF] [2020-07-07 12:54:51.105] [PID-1]:[abcdefg] [JID-5c] [数据] LIST_LOOKUP:abc@email.com |个人资料中的用户列表|名称| user_name @id:355502086986714

@message:[INF] [2020-07-07 12:38:36.755] [PID-2]:[abcdefg] [JID-ed2] [数据] LIST_LOOKUP:abc@email.com |个人资料中的用户列表|名称| user_name @id:355501869671304

@message:[INF] [2020-07-07 12:19:48.141] [PID-3] [abc@email.com]: [c5] [数据]在11毫秒内完成200 OK,@ id:355501617979964834

@message:[INF] [2020-07-07 11:19:48.930] [PID-5] [abc@email.com]: [542] [数据]在9毫秒内完成200 OK,@ id:35550081535

我希望它是:

@message:[INF] [2020-07-07 12:19:48.141] [PID-3] [abc@email.com]: [c5] [数据]在11毫秒内完成200 OK,@ id:355501617979964834

@message:[INF] [2020-07-07 11:19:48.930] [PID-5] [abc@email.com]: [542] [数据]在9毫秒内完成200 OK,@ id:35550081535

我尝试使用@message: "[PID-*] [abc@email.com]"@message: "\[PID-*\] \[abc@email.com\] \:"@message: "[abc@email.com]"@message: *abc@email.com*和其他一些类似的搜索,但均未成功。

请让我知道我在这里缺少什么,以及如何使用Discover和KQL / Lucene在ELK kibana中进行有效的亚文本搜索。

这是我的索引的映射(我正在从cloudwatch日志中获取数据):

{
   "cwl-*":{
      "mappings":{
         "properties":{
            "@id":{
               "type":"string"
            },
            "@log_stream":{
               "type":"string"
            },
            "@log_group":{
               "type":"string"
            },
            "@message":{
               "type":"string"
            },
            "@owner":{
               "type":"string"
            },
            "@timestamp":{
               "type":"date"
            }
         }
      }
   }
}

2 个答案:

答案 0 :(得分:1)

您所有的结果都包含abc@gmail.com。因此是可以预期的。

[abc@gmail.com]被标记为

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

如果您有电子邮件字段,则可以使用它。或者您需要更改该字段的映射。

如果它不能回答您的问题,您可以使用http://host:port/indexName/_mapping

为该字段添加映射吗?

答案 1 :(得分:1)

@Gibbs已经提到了原因all your data contains字符串abc@email.com,现在通过查看您的映射,可以确认您正在使用string字段而没有显式分析器将使用{{3 }}

相反,您应该将获取邮件ID的字段映射到使用default standard analyzer(不会拆分文本)的自定义分析器。

有关如何使用示例创建此分析器的示例

使用自定义电子邮件分析器进行映射

{
    "settings": {
        "analysis": {
            "analyzer": {
                "email_analyzer": {
                    "tokenizer": "my_tokenizer"
                }
            },
            "tokenizer": {
                "my_tokenizer": {
                    "type": "uax_url_email"
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "email": {
                "type": "text",
                "analyzer": "email_analyzer"
            }
        }
    }
}

分析api响应

POST http:// {{hostname}}:{{port}} / {{index-name}} / _analyze

{
    "analyzer": "email_analyzer",
    "text": "abc@email.com"
}


{
    "tokens": [
        {
            "token": "abc@email.com",
            "start_offset": 0,
            "end_offset": 13,
            "type": "<EMAIL>",
            "position": 0
        }
    ]
}