es搜索优化
1、V1版本:仅做了分词的查询,使用ik分词器ik_max_word
POST /t_sku/_search { "size": 10, "query": { "bool": { "match": { "spu_name": "陕西米脂" } } }, "_source": "spu_name" }
2、V2版本:
(1)优化后支持前缀搜索,配合权重控制
(2)优化短语查询:phrase查询,查询时可以将短语中词语看成一个整体,本质就是相对位置固定,不像分词的拆分,配合权重,返回结果更靠前,更精准
POST /t_sku/_search { "size": 10, "query": { "bool": { "should": [ { "prefix": { "spu_name.keyword": { "value": "陕西米脂", "boost": 5 } } }, { "match_phrase_prefix": { "spu_name": { "query": "陕西米脂", "boost": 2 } } }, { "match": { "spu_name": "陕西米脂" } } ] } }, "_source": "spu_name" }
V3版本:
扩展match查询能力,支持模糊搜索(添加fuzziness 相关参数从而支持 Fuzzy 模糊匹配)
POST /t_sku/_search { "size": 10, "query": { "bool": { "should": [ { "prefix": { "spu_name.keyword": { "value": "陕西眉县", "boost": 5 } } }, { "match_phrase_prefix": { "spu_name": { "query": "陕西眉县", "boost": 2 } } }, { "match": { "spu_name": { "query": "陕西眉县", "fuzziness": "AUTO", "max_expansions": 10, "prefix_length": 2, "fuzzy_transpositions": true } } } ] } }, "_source": "spu_name" }
V4版本:
查询能力扩展:加上一个 Query String 查询,具备支持通配符和多种查询语法的能力
注意:
因为query_string对任何无效的语法都会返回一个错误,所以我们不建议在搜索框中使用query_string查询。 如果你不需要支持查询语法,可以考虑使用匹配查询。如果你需要查询语法的功能,请使用simple_query_string查询,它没有那么严格。因为它对任何无效的语法都会返回错误,我们不建议在搜索框中使用query_string查询。 如果你不需要支持查询语法,可以考虑使用匹配查询。如果你需要查询语法的功能,请使用simple_query_string查询,它不太严格。
POST /t_sku/_search { "size": 10, "query": { "bool": { "should": [ { "prefix": { "spu_name.keyword": { "value": "陕西眉县", "boost": 5 } } }, { "match_phrase_prefix": { "spu_name": { "query": "陕西眉县", "boost": 2 } } }, { "match": { "spu_name": { "query": "陕西眉县", "fuzziness": "AUTO", "max_expansions": 10, "prefix_length": 2, "fuzzy_transpositions": true } } }, { "query_string": { "fields": [ "spu_name", "spu_name.keyword" ], "query": "陕西眉县", "fuzziness": "AUTO", "fuzzy_prefix_length": 2, "fuzzy_max_expansions": 10, "fuzzy_transpositions": true, "allow_leading_wildcard": false } } ] } }, "_source": "spu_name" }