第二阶段 15 · prefix / wildcard / fuzzy 模糊匹配

15 · prefix / wildcard / fuzzy 模糊匹配

阶段:第二阶段 / 查询能力
ES 查询:prefix / wildcard / fuzzy | PostgreSQL 对照:LIKE 'abc%' / LIKE '%x%' / 模糊纠错


1. 概念

三种「非精确」匹配,都作用在 keyword(或 text 的词项)上:

ES 查询作用SQL 对应
prefix前缀匹配LIKE 'abc%'
wildcard通配符匹配(* 多字符,? 单字符)LIKE '%x%' / LIKE 'a?c'
fuzzy允许拼写误差(编辑距离)PG 没有直接语法(近似 pg_trgm

⚠️ 这三种都可能很慢(尤其前置通配 *abc),生产环境慎用大范围。


2. PostgreSQL 对照

-- prefix
SELECT * FROM salesdata WHERE invoice_number LIKE 'INV-2026%';

-- wildcard(包含)
SELECT * FROM salesdata WHERE invoice_number LIKE '%2026%';

-- fuzzy(拼写容错,PG 需 pg_trgm 扩展)
SELECT * FROM salesdata WHERE material % 'ThinkPd';  -- 相似度

3. ES DSL

prefix(前缀)

GET salesdata_idx/_search
{
  "query": { "prefix": { "invoice_number": "INV-2026" } }
}

wildcard(通配符)

GET salesdata_idx/_search
{
  "query": { "wildcard": { "invoice_number": "*2026*" } }
}

fuzzy(拼写容错)

GET salesdata_idx/_search
{
  "query": {
    "fuzzy": {
      "material": { "value": "ThinkPd", "fuzziness": "AUTO" }
    }
  }
}

fuzziness: AUTO 会根据词长自动决定允许的编辑距离(通常 1~2)。


4. Spring Boot 实现

@Component
public class Doc15FuzzyQuery {

    @Autowired
    private ElasticsearchClient elasticsearchClient;

    /** LIKE 'prefix%' */
    public List<Map<String, Object>> prefix(String indexName, String field, String prefix)
            throws IOException {
        SearchResponse<Map> resp = elasticsearchClient.search(s -> s
                .index(indexName)
                .query(q -> q.prefix(p -> p.field(field).value(prefix))),
                Map.class);
        return toSourceList(resp);
    }

    /** LIKE '%pattern%',pattern 里用 * 和 ? */
    public List<Map<String, Object>> wildcard(String indexName, String field, String pattern)
            throws IOException {
        SearchResponse<Map> resp = elasticsearchClient.search(s -> s
                .index(indexName)
                .query(q -> q.wildcard(w -> w.field(field).value(pattern))),
                Map.class);
        return toSourceList(resp);
    }

    /** 拼写容错 */
    public List<Map<String, Object>> fuzzy(String indexName, String field, String value)
            throws IOException {
        SearchResponse<Map> resp = elasticsearchClient.search(s -> s
                .index(indexName)
                .query(q -> q.fuzzy(f -> f.field(field).value(value).fuzziness("AUTO"))),
                Map.class);
        return toSourceList(resp);
    }

    private List<Map<String, Object>> toSourceList(SearchResponse<Map> resp) {
        return resp.hits().hits().stream()
                .map(Hit::source).filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}

5. 坑与最佳实践

  1. 前置通配符 *abc 极慢:等于全表扫倒排词典,能避免就避免;必要时用 wildcard 字段类型或 n-gram。
  2. 作用在 keyword:对分词后的 text 用 wildcard 结果常常反直觉。
  3. 大小写keyword 默认大小写敏感,wildcard 也是。需要不敏感可加 case_insensitive: true(8.x 支持)。
  4. fuzzy 有性能代价:编辑距离越大越慢,AUTO 通常够用。
  5. 能用 prefix 就别用 wildcard:前缀匹配比通配快得多。

下一篇

16-match_phrase-短语.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倒流时光三十年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值