Elasticsearch - 启用字段的全文搜索
Elasticsearch - Enable fulltext search of field
考虑到在我记录的事件中搜索,我已经 运行 陷入困境。我正在使用 elasticsearch 解决方案、filebeat 将消息从日志加载到 elasticsearch 和 Kibana 前端。
我目前将消息记录到字段 message
中,并将异常堆栈跟踪(如果存在)记录到 error.message
中。因此,记录的事件的片段可能如下所示:
{
"message": "Thrown exception: CustomException (Exception for testing purposes)"
"error" : {
"message" : "com.press.controller.CustomException: Exception for testing purposes\n at
com.press.controller....<you get the idea at this point>"
}
}
当然还有timestamp之类的其他字段,不过这些都不重要。重要的是:
当我搜索 message : customException
时,我可以找到我记录的事件。当我搜索 error.message : customException
时,我没有得到事件。我需要能够全文搜索所有字段。
有没有办法告诉elasticsearch在字段中启用全文搜索?
为什么 "message" 字段默认启用它? None 我的同事知道任何索引命令在部署后在控制台的字段上 运行 并且我们的权限不允许我或其他团队成员 运行 索引或分析命令任何领域。所以它必须在某个地方的配置中。
到目前为止我找不到解决方案。请把我推向正确的方向。
编辑:
字段配置如下:
我们使用修改后的 ECS,并且两个消息都声明为
level: core
type: text
在文件 fields.yml
中。
在 filebeat 中,配置片段是这样的:
filebeat.inputs:
- type: log
enabled: true
paths: .....
...
...
processors:
- rename:
fields:
- from: "msg"
to: "message"
- from: "filepath"
to: "log.file.name"
- from: "ex"
to: "error.message"
ignore_missing: true
fail_on_error: true
logging.level: debug
logging.to_files: true
出于安全考虑,我不能透露全部文件。另外,我需要手写所有的片段,所以拼写错误可能是我的错。
谢谢
Elastic Search 默认索引所有字段,这里您没有定义映射,因此默认情况下应该索引所有字段。
同样对于你的情况,我怀疑数据是否在弹性搜索中正常进行,因为日志似乎不正确 json。
您是否在 Kibana 中看到正确的日志,如果是,请发送示例 log/screenshot
问题出在与您的字段关联的分析器上,默认情况下,对于 ES 中的文本字段,使用标准分析器,如果文本包含 .
,则不会创建单独的标记,例如:foo.bar
将只产生 1 个令牌作为 foo.bar
,而如果您希望 foo
和 bar
都应在 foo.bar
中匹配,那么您需要生成 2 个令牌作为 foo
和bar
.
您需要的是一个自定义分析器,它会像上面那样创建令牌,因为您的 error.message
文本包含 .
,我在我的示例中对此进行了解释:
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": ["replace_dots"]
}
},
"char_filter": {
"replace_dots": {
"type": "mapping",
"mappings": [
". => \u0020"
]
}
}
}
}
}
POST /my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "foo.bar"
}
上面的示例创建了 2 个标记,如 foo
和 bar
,当您使用这些 API.
创建和测试它时,您也会遇到同样的情况
如果您遇到任何问题,请告诉我。
考虑到在我记录的事件中搜索,我已经 运行 陷入困境。我正在使用 elasticsearch 解决方案、filebeat 将消息从日志加载到 elasticsearch 和 Kibana 前端。
我目前将消息记录到字段 message
中,并将异常堆栈跟踪(如果存在)记录到 error.message
中。因此,记录的事件的片段可能如下所示:
{
"message": "Thrown exception: CustomException (Exception for testing purposes)"
"error" : {
"message" : "com.press.controller.CustomException: Exception for testing purposes\n at
com.press.controller....<you get the idea at this point>"
}
}
当然还有timestamp之类的其他字段,不过这些都不重要。重要的是:
当我搜索 message : customException
时,我可以找到我记录的事件。当我搜索 error.message : customException
时,我没有得到事件。我需要能够全文搜索所有字段。
有没有办法告诉elasticsearch在字段中启用全文搜索? 为什么 "message" 字段默认启用它? None 我的同事知道任何索引命令在部署后在控制台的字段上 运行 并且我们的权限不允许我或其他团队成员 运行 索引或分析命令任何领域。所以它必须在某个地方的配置中。
到目前为止我找不到解决方案。请把我推向正确的方向。
编辑: 字段配置如下:
我们使用修改后的 ECS,并且两个消息都声明为
level: core
type: text
在文件 fields.yml
中。
在 filebeat 中,配置片段是这样的:
filebeat.inputs:
- type: log
enabled: true
paths: .....
...
...
processors:
- rename:
fields:
- from: "msg"
to: "message"
- from: "filepath"
to: "log.file.name"
- from: "ex"
to: "error.message"
ignore_missing: true
fail_on_error: true
logging.level: debug
logging.to_files: true
出于安全考虑,我不能透露全部文件。另外,我需要手写所有的片段,所以拼写错误可能是我的错。
谢谢
Elastic Search 默认索引所有字段,这里您没有定义映射,因此默认情况下应该索引所有字段。 同样对于你的情况,我怀疑数据是否在弹性搜索中正常进行,因为日志似乎不正确 json。 您是否在 Kibana 中看到正确的日志,如果是,请发送示例 log/screenshot
问题出在与您的字段关联的分析器上,默认情况下,对于 ES 中的文本字段,使用标准分析器,如果文本包含 .
,则不会创建单独的标记,例如:foo.bar
将只产生 1 个令牌作为 foo.bar
,而如果您希望 foo
和 bar
都应在 foo.bar
中匹配,那么您需要生成 2 个令牌作为 foo
和bar
.
您需要的是一个自定义分析器,它会像上面那样创建令牌,因为您的 error.message
文本包含 .
,我在我的示例中对此进行了解释:
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": ["replace_dots"]
}
},
"char_filter": {
"replace_dots": {
"type": "mapping",
"mappings": [
". => \u0020"
]
}
}
}
}
}
POST /my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "foo.bar"
}
上面的示例创建了 2 个标记,如 foo
和 bar
,当您使用这些 API.
如果您遇到任何问题,请告诉我。