常见的需求场景就是我要根据某几个字段去搜索,数据库搜索的话只能是 or 拼接 like,效率比较低
,下面是结合es来实现,首先创建一下文档的接口如下 o_order_test
elasticsearch 7.10版本 max_ngram_diff 最大的长度,
PUT /o_order_test { "settings": { "index": { "max_ngram_diff": 30, "analysis": { "analyzer": { "ngram_name_analyzer": { "tokenizer": "ngram_name_analyzer" } }, "tokenizer": { "ngram_name_analyzer": { "type": "ngram", "min_gram": 1, "max_gram": 30, "token_chars": ["letter", "digit"] } } } } }, "mappings": { "properties": { "merchantUserMobile": { "type": "text", "analyzer": "ngram_name_analyzer", "copy_to": "merchantUserInfo" }, "merchantUserName": { "type": "text", "analyzer": "ngram_name_analyzer", "copy_to": "merchantUserInfo" }, "merchantUserInfo": { "type": "text", "analyzer": "ngram_name_analyzer" } } } }
用户手机号和姓名模糊搜索
随机插入几条数据
POST /o_order_test/_doc/1 { "merchantUserMobile": "13877777773", "merchantUserName": "谢升" } POST /o_order_test/_doc/2 { "merchantUserMobile": "15811111111", "merchantUserName": "续航额地方" } POST /o_order_test/_doc/3 { "merchantUserMobile": "1312222222", "merchantUserName": "Alice Johnson" } POST /o_order_test/_doc/4 { "merchantUserMobile": "151333333333", "merchantUserName": "谢大爷" } POST /o_order_test/_doc/5 { "merchantUserMobile": "18744444444444", "merchantUserName": "张三" }
搜索的语句
GET /o_order_test/_search { "query": { "term": { "merchantUserInfo": "187" } } }