具有分层类别,子类别的Elasticsearch聚合;限制水平 [英] Elasticsearch aggregation with hierarchical category, subcategory; limit the levels
问题描述
我有带有类别字段的产品。使用聚合,我可以获得所有子类别的完整类别。我想限制构面中的级别。
I have products with categories field. Using the aggregation I can get the full categories with all subcategories. I want to limit the levels in the facet.
例如我有以下方面:
auto, tools & travel (115)
auto, tools & travel > luggage tags (90)
auto, tools & travel > luggage tags > luggage spotters (40)
auto, tools & travel > luggage tags > something else (50)
auto, tools & travel > car organizers (25)
使用
"aggs": {
"cat_groups": {
"terms": {
"field": "categories.keyword",
"size": 10,
"include": "auto, tools & travel > .*"
}
}
}
我正在像
"buckets": [
{
"auto, tools & travel > luggage tags",
"doc_count": 90
},
{
"key": "auto, tools & travel > luggage tags > luggage spotters",
"doc_count": 40
},
{
"key": "auto, tools & travel > luggage tags > something else",
"doc_count": 50
},
{
"key": "auto, tools & travel > car organizers",
"doc_count": 25
}
]
但是我想限制水平。例如我只想获取 auto,tools&的结果。旅行>行李标签
。如何限制水平?
顺便说一句, exclude:。*>。*>。*
对我不起作用。
But I want to limit the level. e.g. I want to get only the results for auto, tools & travel > luggage tags
. How can I limit the levels?
By the way, "exclude": ".* > .* > .*"
does not work for me.
我需要根据搜索获得不同级别的存储桶。有时是第一级,有时是第二或第三级。当我想要第一级时,我不希望第二级出现在存储桶中;
Elasticsearch版本6.4
Elasticsearch version 6.4
推荐答案
最后,我已经能够理解以下技术。
Finally I've been able to figure the below technique.
我已经使用类别$的多字段c $ c>,以便您可以使用
categories.facets
进行汇总/构面,并使用 categories
进行常规文本搜索。
I have implemented a custom analyzer
using Path Hierarchy Tokenizer and I have created multi-field called categories
so that you can use categories.facets
for aggregations/facets and do normal text search using categories
.
自定义分析器仅适用于 categories.facets
The custom analyzer would only apply for categories.facets
请注意我的字段 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"
}
}
}
}
}
}
}
样本文档
Sample Documents
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"
}
查询
您可以尝试以下查询,同样,我已经实现了过滤器聚合,因为您只需要特定的单词以及术语汇总。
Query
You can try the below query which you are looking for. Again I've implemented Filter Aggregation because you need only specific words along with Terms Aggregation.
{
"size": 0,
"aggs":{
"facets": {
"filter": {
"bool": {
"must": [
{ "match": { "categories": "luggage"} }
]
}
},
"aggs": {
"categories": {
"terms": {
"field": "categories.facet"
}
}
}
}
}
}
响应
Response
{
"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
}
]
}
}
}
}
最终答案在聊天室讨论
Final Answer Post Discussion On Chat
POST myindex/_search
{
"size": 0,
"aggs":{
"facets": {
"filter": {
"bool": {
"must": [
{ "match": { "categories": "luggage"} }
]
}
},
"aggs": {
"categories": {
"terms": {
"field": "categories.facet",
"exclude": ".*>{1}.*>{1}.*"
}
}
}
}
}
}
请注意,我在 exclude
中添加了正则表达式
,这样就不会考虑出现多个>
的任何方面
Note that I've added exclude
with a regular expression
in such a way that it would not consider any facets which is having more than one occurrence of >
让我知道是否有帮助。
这篇关于具有分层类别,子类别的Elasticsearch聚合;限制水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!