路径层次结构弹性搜索和文件夹深度



我正在为 Logstash/ElasticSearch 中的字段使用路径层次结构分词器。因此,如果路径字段类似于/a/b/c,则分词器会将其转换为

    /a
    /a/b
    /a/b/c

我想生成这样的统计数据

    a - 3 hits
    b - 2 hits
    c - 1 hit
最好的

方法是什么?另外,我想知道是否有办法在单独的字段中添加文件夹深度。

出于您的自定义目的,我认为您可以在文件上指定自定义模式分析器,并采用字段聚合术语。一个例子如下:

定义您的自定义分析器:

PUT /test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "nonword": {
          "type": "pattern",
          "pattern": "/"
        }
      }
    }
  }
}

创建映射:

 POST /test_index/_mapping/test_1
{
  "properties": {
    "dir": {
      "type": "string",
      "index": "analyzed",
      "analyzer": "nonword",
      "fields": {
        "un_touched": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

注意:"un_touched"字段用于保存数据的原始版本。

填充数据并执行聚合:

GET /test_index/test_1/_search
{
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "dir",
        "size": 0
      }
    }
  }
}

注意:这只是一个最小的例子,你应该真正关心模式;

最新更新