elasticsearch检索支持不区分大小写搜索

语法问题:网上各种文章层出不穷,各种版本的十分多。小伙伴有时发现网上的语法自己一用就报错。这种情况下请按照官方文档最佳,选择好自己的ES版本对格式进行微调。文章仅只是提供一个学习方向。

随着搜索数据愈加庞大,越来越多的兄弟们开始使用ES,俺也不例外。
由于业务需要所以这里使用的 wildcard 检索,也就是相当于 MySQL 中的 like 。
在这里顺便提一句 wildcard 的小知识点:有人可能会在检索时字符串中间含有空格是查不到准确的结果的。因为 ES 会默认按字符串中的空格进行拆分。在这里就不多阐述 wildcard 的使用方法了。【8.1版本使用方法】

字段中有 keyword 属性时可以如下使用可避免自动拆分引起的查不到结果:
"wildcard":{"author.keyword":"*abc def*"}
写文章时本人使用的版本:

"version": {
        "number": "7.15.1",
        "build_flavor": "default",
        "build_type": "docker",
        "build_hash": "******",
        "build_date": "******",
        "build_snapshot": false,
        "lucene_version": "8.9.0",
        "minimum_wire_compatibility_version": "6.8.0",
        "minimum_index_compatibility_version": "6.0.0-beta1"
    },

如何查询时不区分大小写

方法一:从 index的mapping入手:
即加 mapping 中加入设置:

"settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  }

上面这段代码在创建索引是加入到 mapping 中。
并在相应的索引字段中加入对应的属性:
"normalizer": "my_normalizer"
以下使用 postman 举个例子:
地址:https://elasticsearch.net/test_index 使用 PUT 请求
请求的 json :

    {
    "settings": {
        "analysis": {
          "normalizer": {
            "my_normalizer": {
              "type": "custom",
              "char_filter": [],
              "filter": ["lowercase", "asciifolding"]
            }
          }
        }
      },
      "mappings": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "@version": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "author": {
                    "type": "keyword",
                    "normalizer": "my_normalizer",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "creater": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "email": {
                    "type": "keyword",
                    "normalizer": "my_normalizer",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }

提交创建后查看是否成功
地址:https://elasticsearch.com/test_index/_mappings?pretty 使用 GET 请求。返回结果与提交设置的一样就成功了。
注意点:这个方法对已经创建的索引并不适用。ES索引 mapping 创建完成后是不支持编辑某一索引字段属性的。需要重新创建生成索引才行。
其他发法还在学习中… 欢迎指教。
文章有问题的欢迎指出,会尽快修改学习。

本作品采用《CC 协议》,转载必须注明作者和本文链接
南南空空
讨论数量: 2
ma6plus
"wildcard":{"author.keyword":"*abc def*"}

这样的用法文档上说的很清楚了,性能昂贵。这个是字符串【居中】查询,自带的 query 基本就是为此准备的。
wildcard 对于 左,中,右三种查询方式,都可以。
左查询比如 kit* 匹配:

  • kitty
  • kitchen
    由查询比如 *land 匹配:
  • Finland
  • Island
  • Iceland
    唯独居中查询,性能最差。
    左、右匹配查询,英文叫 corner search,可以搜一下相关文章。
2年前 评论
NanKong (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!