最后,我已经能够弄清楚以下技术。
我已经实现了
customanalyzer使用路径层次结构标记器,并且创建了称为的多字段,
categories以便您可以
categories.facets用于聚合/构面并使用进行普通文本搜索
categories。
定制分析器仅适用于
categories.facets
请注意
"fielddata": "true"我所在领域的财产
categories.facet
制图
PUT myindex{ "settings": { "analysis": { "analyzer": { "my_analyzer": { "tokenizer": "my_tokenizer" } }, "tokenizer": { "my_tokenizer": { "type": "path_hierarchy", "delimiter": ">" } } } }, "mappings": { "mydocs": { "properties": { "categories": { "type": "text", "fields": { "facet": { "type": "text", "analyzer": "my_analyzer", "fielddata": "true" } } } } } }}样本文件
POST myindex/mydocs/1{ "categories" : "auto, tools & travel > luggage tags > luggage spotters"}POST myindex/mydocs/2{ "categories" : "auto, tools & travel > luggage tags > luggage spotters"}POST myindex/mydocs/3{ "categories" : "auto, tools & travel > luggage tags > luggage spotters"}POST myindex/mydocs/4{ "categories" : "auto, tools & travel > luggage tags > something else"}询问
您可以尝试以下查询。再次,我实现了Filter
Aggregation,因为您只需要特定的单词以及Terms
Aggregation。
{ "size": 0, "aggs":{ "facets": { "filter": {"bool": { "must": [ { "match": { "categories": "luggage"} } ] } }, "aggs": { "categories": { "terms": { "field": "categories.facet" } } } } }}响应
{ "took": 43, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": 11, "max_score": 0, "hits": [] }, "aggregations": { "facets": { "doc_count": 4, "categories": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "auto, tools & travel ", "doc_count": 4 }, { "key": "auto, tools & travel > luggage tags ", "doc_count": 4 }, { "key": "auto, tools & travel > luggage tags > luggage spotters", "doc_count": 3 }, { "key": "auto, tools & travel > luggage tags > something else", "doc_count": 1 } ] } } }}最终答案发布讨论聊天
POST myindex/_search{ "size": 0, "aggs":{ "facets": { "filter": {"bool": { "must": [ { "match": { "categories": "luggage"} } ] } }, "aggs": { "categories": { "terms": { "field": "categories.facet", "exclude": ".*>{1}.*>{1}.*" } } } } }}请注意,我以这样的方式添加
exclude了一个
regular expression,即它不会考虑出现多个
>
让我知道是否有帮助。



