我是Elasticsearch的新手,并且对路径的分层标记器有疑问。这是我的代码示例:
我的映射代码:
PUT /my_index { "settings": { "analysis": { "analyzer": { "path-analyzer": { "type": "custom", "tokenizer": "path-tokenizer" } }, "tokenizer": { "path-tokenizer": { "type": "path_hierarchy", "delimiter": "." } } } }, "mappings": { "my_type": { "dynamic": "strict", "properties": { "group_path": { "type": "string", "index_analyzer": "path-analyzer", "search_analyzer": "keyword" } } } } }
这是我的PUT:
PUT /my_index/my_type/1 { "group_path": ["Book.Thriller.Adult","DVD.Comedy.Kids"] }
这是我的查询:
GET /my_index/my_type/_search?search_type=count { "aggs": { "category": { "terms": { "field": "group_path", "size": 0 } } } }
结果:
{ ... "aggregations": { "category": { "buckets": [ { "key": "Book", "doc_count": 1 }, { "key": "Book.Thriller", "doc_count": 1 }, { "key": "Book.Thriller.Adult", "doc_count": 1 }, { "key": "DVD", "doc_count": 1 }, { "key": "DVD.Comedy", "doc_count": 1 }, { "key": "DVD.Comedy.Kids", "doc_count": 1 } ] } } }
到目前为止,一切都很好。我正在寻找的是如何仅针对第一类创建存储桶。我如何获得这样的结果:
{ ... "aggregations": { "category": { "buckets": [ { "key": "Book", "doc_count": 1 }, { "key": "DVD", "doc_count": 1 } ] } } }
感谢您的任何帮助。
我发现这样做的唯一方法是使用exclude语法排除不需要的级别。
{ "aggs": { "category": { "terms": { "field": "group_path", "size": 0, "exclude" : ".*\\..*" } } } }
然后将返回
aggregations: { category: { buckets: [ { key: Book doc_count: 1 } { key: DVD doc_count: 1 } ] } }
如果您选择书籍,则可以像这样搜索
{ "query" : { "filtered": { "filter": { "prefix": { "group_path": "Book" } } } }, "aggs" : { "category": { "terms": { "field": "group_path", "size": 0, "include" : "Book\\..*", "exclude": ".*\\..*\\..*" } } } }
aggregations: { category: { buckets: [ { key: Book.Thriller doc_count: 1 } ] } }