设置: Elasticsearch 6.3
我有一个代表产品目录的索引。
每个文档都包含一个产品的数据。
称为categories
的字段之一是字符串数组-相关类别列表。
99.9%的查询是:给我匹配类别A,B和C的产品。查询是区分大小写的不敏感,因此类别映射如下:
"categories": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
}
为了进行报告(占所有查询的0.1%),我需要返回所有可能类别的列表,区分大小写敏感!
考虑以下文件:
"_id": "product1",
"_source": {
"categories": [
"WOMEN",
"Footwear"
]
}
"_id": "product2",
"_source": {
"categories": [
"Men",
"Footwear"
]
}
运行以下查询:
{
"size": 0,
"aggs": {
"categories": {
"terms": {
"field": "categories",
"size": 100
}
}
}
}
返回:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 40453,
"max_score": 0,
"hits": [
]
},
"aggregations": {
"sterms#categories": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 12453,
"buckets": [
{
"key": "men",
"doc_count": 27049
},
{
"key": "women",
"doc_count": 21332
},
.........
]
}
}
}
有没有办法返回区分大小写的类别(存储在文档中)?我对查询结果中的
["WOMEN", "Men"]
感兴趣。The question in Elasticsearch discuss forum
谢谢,
伊泰
最佳答案
您需要在属性中配置一个不使用任何规范化器的字段:
Documentation on fields
就像是
"categories": {
"type": "keyword",
"normalizer": "lowercase_normalizer",
"fields": {
"case_sensitive": {
"type": "keyword"
}
}
}
然后在此字段上进行汇总:
{
"size": 0,
"aggs": {
"categories": {
"terms": {
"field": "categories.case_sensitive",
"size": 100
}
}
}
}
关于elasticsearch - Elasticsearch支持区分大小写和不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59819152/