2015-12-30 122 views
0

我在弹性搜索文档场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领域的类似查询条目

  1. 我已经给申请号1451299305289_0120
  2. 我也给任务数009257
  3. - :在所有3)条件
  4. 路径字段也应该包含stderr

鉴于上述标准具有路径字段作为第三行的文件应该被选择

这是我的尝试到目前为止

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和使用的elastic-search

+0

使用nGram将会非常昂贵,但是你可以做edgeNGram在分析时使用几个过滤器。 我建议你可以看看这篇文章.. http:// stackoverflow。com/questions/9421358 /文件名搜索与弹性搜索# 它可能没有什么帮助,因为在你可以得到一些方向.. –

回答

1

这可以通过使用Pattern Replace Char Filter来实现。您只需提取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 numbertask numberpath用正则表达式。如果您有其他日志格式,您可能需要稍微优化task regex,那么我们可以使用Filters进行搜索。使用过滤器的一大优势是它们是缓存并使后续调用更快。

我索引采样日志这样

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 queryES 2.x弃用,只是使用过滤器directly.Also path hierarchy可能有用的一些其他用途

希望这会有所帮助:)