Elasticsearch模糊查询
Elasticsearch模糊查询
ES模糊查询共有三种:
1.match 分词匹配查询和match_phrase短语查询
Elasticsearch的match_phrase短语查询跟match的区别就是,关键词作为一个整体进行搜索,而不是拆分成一个个关键词。
当匹配对象为中文时,建议使用:
MatchPhraseQueryBuilder query = QueryBuilders.matchPhraseQuery("name", "*"+req.getName()+"*");
2.wildcard 通配符检索
类似SQL中的like,可以拼接*
WildcardQueryBuilder query = QueryBuilders.wildcardQuery("tel", "*"+req.getTel()+"*");
3.fuzzy 纠错查询,有一定容错性
亲测:match_phrase可行;
ES的OR实现:
BoolQueryBuilder query2 = QueryBuilders.boolQuery(); BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery(); shouldQuery.should(QueryBuilders.wildcardQuery("a" , "1")); shouldQuery.should(QueryBuilders.wildcardQuery("b" , "1")); query2.must(shouldQuery);