我的弹性搜索文档中有一个字段path,其中包含这样的条目
/logs/hadoop-yarn/container/application_1451299305289_0120/container_e18_1451299305289_0120_01_011007/stderr
/logs/hadoop-yarn/container/application_1451299305289_0120/container_e18_1451299305289_0120_01_008874/stderr
#*Note -- I want to select all the documents having below line in the **path** field
/logs/hadoop-yarn/container/application_1451299305289_0120/container_e18_1451299305289_0120_01_009257/stderr我想对这个path字段进行一个类似的查询,给出了某些内容(基本上是所有3的一个和条件):-
1451299305289_0120009257stderr考虑到上述条件,应该选择将路径字段作为第3行的文档。
这就是我到目前为止所尝试的
http://localhost:9200/logstash-*/_search?q=application_1451299305289_0120 AND path:stderr&size=50此查询满足第三个条件,部分满足第一个条件,即如果我搜索1451299305289_0120而不是application_1451299305289_0120,则得到0的结果。(我真正需要的是在1451299305289_0120上搜索)
当我尝试这个
http://10.30.145.160:9200/logstash-*/_search?q=path:*_1451299305289_0120*008779 AND path:stderr&size=50我得到了结果,但一开始使用*是一项昂贵的操作。是他们有效实现这一目标的另一种方法(比如使用nGram和使用fuzzy-search of elastic-search)。
发布于 2015-12-30 15:47:26
这可以通过使用模式替换Char滤波器来实现。您只需使用regex提取重要的信息。这是我的装置
POST log_index
{
"settings": {
"analysis": {
"analyzer": {
"app_analyzer": {
"char_filter": [
"app_extractor"
],
"tokenizer": "keyword",
"filter": [
"lowercase",
"asciifolding"
]
},
"path_analyzer": {
"char_filter": [
"path_extractor"
],
"tokenizer": "keyword",
"filter": [
"lowercase",
"asciifolding"
]
},
"task_analyzer": {
"char_filter": [
"task_extractor"
],
"tokenizer": "keyword",
"filter": [
"lowercase",
"asciifolding"
]
}
},
"char_filter": {
"app_extractor": {
"type": "pattern_replace",
"pattern": ".*application_(.*)/container.*",
"replacement": "$1"
},
"path_extractor": {
"type": "pattern_replace",
"pattern": ".*/(.*)",
"replacement": "$1"
},
"task_extractor": {
"type": "pattern_replace",
"pattern": ".*container.{27}(.*)/.*",
"replacement": "$1"
}
}
}
},
"mappings": {
"your_type": {
"properties": {
"name": {
"type": "string",
"analyzer": "keyword",
"fields": {
"application_number": {
"type": "string",
"analyzer": "app_analyzer"
},
"path": {
"type": "string",
"analyzer": "path_analyzer"
},
"task": {
"type": "string",
"analyzer": "task_analyzer"
}
}
}
}
}
}
}我用正则表达式提取application number、task number和path。如果您有一些其他的日志模式,您可能希望对task regex进行一些优化,然后我们可以使用过滤器进行搜索,使用过滤器的一个很大的优点是它们是缓存的,并且能够更快地进行后续调用。
我像这样索引了样本日志
PUT log_index/your_type/1
{
"name" : "/logs/hadoop-yarn/container/application_1451299305289_0120/container_e18_1451299305289_0120_01_009257/stderr"
}此查询将给出所需的结果。
GET log_index/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"name.application_number": "1451299305289_0120"
}
},
{
"term": {
"name.task": "009257"
}
},
{
"term": {
"name.path": "stderr"
}
}
]
}
}
}
}
}另外,filtered query在ES 2.x中不推荐使用,只需使用filter directly.Also 路径层次结构就可以用于其他一些用途。
希望这会有所帮助:)
https://stackoverflow.com/questions/34528145
复制相似问题