ElasticSearch不区分字母大小写搜索

0、停止使用该索引的服务(避免新加了数据没备份)

1、备份filesearch索引(检查备份的索引和原索引数据条数是否一致)

1 POST    http://127.0.0.1:9200/_reindex
2 {
3     "source":{
4         "index":"filesearch"
5     },
6     "dest":{
7         "index":"filesearch_bak"
8     }
9 }

2、删除filesearch索引

3、新建索引(不包括不区分大小写的字段fileName)

 1 PUT    http://127.0.0.1:9200/filesearch
 2 {
 3     "mappings": {
 4         "properties": {
 5             "extendName": {
 6                 "type": "text",
 7                 "fields": {
 8                     "keyword": {
 9                         "ignore_above": 256,
10                         "type": "keyword"
11                     }
12                 }
13             },
14             "filePath": {
15                 "type": "text",
16                 "fields": {
17                     "keyword": {
18                         "ignore_above": 256,
19                         "type": "keyword"
20                     }
21                 }
22             },
23             "fileId": {
24                 "type": "text",
25                 "fields": {
26                     "keyword": {
27                         "ignore_above": 256,
28                         "type": "keyword"
29                     }
30                 }
31             },
32             "isDir": {
33                 "type": "long"
34             }
35         }
36     }
37 }

4、关闭索引

1 POST    http://127.0.0.1:9200/filesearch/_close

5、修改索引属性(添加不区分大小写的属性)

 1 PUT    http://127.0.0.1:9200/filesearch/_settings
 2 {
 3   "analysis": {
 4     "normalizer": {
 5       "lowercase_normalizer": {
 6         "type": "custom",
 7         "char_filter": [],
 8         "filter": [
 9           "lowercase"
10         ]
11       }
12     }
13   }
14 }

6、更新映射(添加不区分大小写的字段fileName)

 1 PUT    http://127.0.0.1:9200/filesearch/_mapping
 2 {
 3     "properties": {
 4         "fileName": {
 5             "type": "text",
 6             "fields": {
 7                 "keyword": {
 8                     "normalizer": "lowercase_normalizer",
 9                     "ignore_above": 256,
10                     "type": "keyword"
11                 }
12             }
13         }
14     }
15 }

7、开启索引

1 POST    http://127.0.0.1:9200/filesearch/_open

8、复制备份索引到新索引

1 POST    http://127.0.0.1:9200/_reindex
2 {
3     "source":{
4         "index":"filesearch_bak"
5     },
6     "dest":{
7         "index":"filesearch"
8     }
9 }

9、启动使用该索引的服务

           完成!          

测试:

1、写入数据

 1 PUT    http://127.0.0.1:9200/ws_index/_create/1/
 2 {
 3     "fileId": "83f311d71e5d4d799e5b254cf9305b04",
 4     "fileName": "0703WXb0839",
 5     "filePath": "/我的文件/20240703/demo",
 6     "extendName": "txt",
 7     "isDir": 0
 8 }
 9 
10 PUT    http://127.0.0.1:9200/ws_index/_create/2/
11 {
12     "fileId": "83f311d71e5d4d799e5b254cf9305b04",
13     "fileName": "0703wxb0843",
14     "filePath": "/我的文件/20240703/demo",
15     "extendName": "txt",
16     "isDir": 0
17 }

2、查询(可以查到上面新增的两条数据)

 1 POST    http://127.0.0.1:9200/filesearch/_search
 2 {
 3   "from": 0,
 4   "size": 200,
 5   "query": {
 6     "bool": {
 7       "must": [
 8         {
 9           "bool": {
10             "should": [
11               {
12                 "wildcard": {
13                   "fileName.keyword": "*Wxb*"
14                 }
15               }
16             ]
17           }
18         },
19         {
20           "bool": {
21             "should": [
22               {
23                 "wildcard": {
24                   "filePath.keyword": "/我的文件/20240703/demo*"
25                 }
26               }
27             ]
28           }
29         }
30       ]
31     }
32   }
33 }

 

posted @ 2024-07-03 09:04  王小白吖~  阅读(96)  评论(0编辑  收藏  举报