Elasticsearch 支持区分大小写和不区分大小写

Elasticsearch support both case sensitive & insensitive

设置: Elasticsearch 6.3

我有一个代表产品目录的索引。

每个文档包含一个产品的数据。

其中一个名为 categories 的字段是一个字符串数组 - 相关类别列表。

99.9% 的查询是:给我匹配类别 A、B 和 C 的产品。查询是大小写 不敏感,因此类别映射看起来像:

"categories": {
    "type": "keyword",
    "normalizer": "lowercase_normalizer"
}

为了报告(所有查询的 0.1%),我需要 return 所有可能类别案例的列表 sensitive!

考虑以下文档:

"_id": "product1",
"_source": {
    "categories": [
        "WOMEN",
        "Footwear"
     ]
}

"_id": "product2",
"_source": {
    "categories": [
        "Men",
        "Footwear"
     ]
}

运行 以下查询:

{
  "size": 0,
  "aggs": {
    "categories": {
      "terms": {
        "field": "categories",
        "size": 100
      }
    }
  }
}

return:

{
  "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
        },
       .........
      ]
    }
  }
}

有没有办法 return 区分大小写的类别(存储在文档中)?我对此查询结果中的 ["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
      }
    }
  }
}