【问题标题】:case insensitive search for non-indexed fields不区分大小写的非索引字段搜索
【发布时间】:2016-06-27 15:21:25
【问题描述】:

如何在不分析数据的情况下使用不区分大小写的过滤器进行搜索? 例如在此示例中,由于大写,我将“delhi”和“Delhi”作为单独的条目。

new york 2
Delhi 1
delhi 1
new Jersey 1

预期结果:

new york 2
delhi 2
new jersey 1

我尝试了小写分析器,但为此我需要更改要分析的索引,这将返回“新”作为一个单独的城市,这是错误的。

DELETE /test_index
PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "cities": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"cities":["new york", "delhi"]}
{"index":{"_id":2}}
{"cities":["new york", "Delhi", "new Jersey"]}


POST /test_index/_search?search_type=count
{
    "aggs": {
        "city_terms": {
            "terms": {
                "field": "cities"
            }
}}}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    是的,但您仍然需要一个 keyword 分析器,它与 not_analyzed 完成完全相同的工作,但只会将您的输入小写:

    PUT /test_index
    {
       "settings": {
         "analysis": {
           "analyzer": {
             "keyword": {
               "type": "custom",
               "tokenizer": "keyword",
               "filter": ["lowercase"]
             }
           }
         }
       },
       "mappings": {
          "doc": {
             "properties": {
                "cities": {
                   "type": "string",
                   "analyzer": "keyword"
                }
             }
          }
       }
    }
    

    更新

    在 ES 5 之前,您可以这样做:

    POST /test_index/_search?search_type=count
    {
        "aggs": {
            "city_terms": {
                "terms": {
                    "script": "doc.cities.values.collect{it.toLowerCase()}"
                }
    }}}
    

    【讨论】:

    • 出现错误 #"reason": "analyzer [analyzer] not found for field [cities]"
    • 这次没有报错。但我仍然得到旧的错误结果。版本 #2.3.3
    • 好的,我看到了你的问题。 ES 5 将使它变得容易,使用ingest nodes and the lowercase processor 您将能够在输入被索引之前对其进行转换。在此之前,您要么需要客户端代码在索引文档之前将术语小写,要么使用带有脚本的 terms 聚合。我已经用后一种解决方案更新了我的答案。
    • 这正确显示了德里 2。但是为什么其他城市没有返回呢?
    • 没问题,我已经修好了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2010-10-28
    • 2010-09-15
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多