栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有分层类别,子类别的Elasticsearch聚合;限制水平

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有分层类别,子类别的Elasticsearch聚合;限制水平

最后,我已经能够弄清楚以下技术。

我已经实现了

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
,即它不会考虑出现多个
>

让我知道是否有帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号