【问题标题】:Elasticsearch support both case sensitive & insensitiveElasticsearch 支持区分大小写和不区分大小写
【发布时间】:2020-01-20 16:30:05
【问题描述】:

设置: 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

谢谢, 伊泰

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您需要在属性中配置一个不使用任何规范化器的字段:

    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
          }
        }
      }
    }
    

    【讨论】:

    • 我会检查一下,谢谢!它会影响我的其他查询吗? (不是聚合、过滤/术语/必须查询)
    • 没有副作用 :) 因为您现有的查询将针对与以前相同的字段
    • 我现在正在尝试解决它。它应该花费我更多吗?在 RAM / 存储方面?
    猜你喜欢
    • 2013-03-06
    • 2020-02-18
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-07-16
    • 2011-03-08
    相关资源
    最近更新 更多