首页 > 解决方案 > Elasticsearch:在关键字中搜索忽略大小写和重音(通过聚合)

问题描述

我可以像这样在索引上搜索特定关键字:

GET */_search/?
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "TECH.keyword": {
      "terms": {
        "field": "TECH.keyword",
        "include": ".*mine.*",
        "order": {
          "_count": "desc"
        },
        "size": 20
      }
    }
  }
}

使用此查询,我可以获取TECH.keyword字段中包含“我的”的所有条目,按"_count": "desc". 所以,没关系。

实际的问题是索引可以包含,甚至mineMine字段中。我想全部归还。MINEminéTECH.keyword

有没有办法在关键字中搜索忽略大小写和重音?

当前映射为:

"TECH": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
},

标签: elasticsearchkibana

解决方案


您应该能够使用normalizer. 您不能使用analyzeronkeyword字段,但可以使用normalizer. 它允许您使用lowercaseasciifolding

https://www.elastic.co/guide/en/elasticsearch/reference/6.4/normalizer.html

PUT index
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "foo": {
          "type": "keyword",
          "normalizer": "my_normalizer"
        }
      }
    }
  }
}

推荐阅读