ElasticSearch不區(qū)分字母大小寫搜索
0、停止使用該索引的服務(wù)(避免新加了數(shù)據(jù)沒備份)
1、備份filesearch索引(檢查備份的索引和原索引數(shù)據(jù)條數(shù)是否一致)
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、新建索引(不包括不區(qū)分大小寫的字段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、關(guān)閉索引
1 POST http://127.0.0.1:9200/filesearch/_close
5、修改索引屬性(添加不區(qū)分大小寫的屬性)
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、更新映射(添加不區(qū)分大小寫的字段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、復(fù)制備份索引到新索引
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、啟動使用該索引的服務(wù)
完成!
測試:
1、寫入數(shù)據(jù)
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、查詢(可以查到上面新增的兩條數(shù)據(jù))
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 }
畢竟人生那么長,如果不喜歡做開發(fā),就別和bug較勁了。

浙公網(wǎng)安備 33010602011771號