JAVA代码实现ElasticSearch搜索:搜索方法、多字段查询、高亮展示

一、搜索方法对比

首先存入一条数据count=" i like eating and kuing" 默认分词器应该将内容分为 “i” “like” “eating” “and” “kuing”

1.QueryBuilders.matchQuery("count",count);

会将搜索词分词,再与目标查询字段进行匹配,若分词中的任意一个词与目标字段匹配上,则可查询到。

count= "i" 可查出
count= "i li" 可查出
count= "i like" 可查出
count= "i like eat" 可查出
count= "and" 可查出
count= "kuing" 可查出
count= "ku"  查不出
count= "li"  查不出
count= "eat"  查不出

2.QueryBuilders.matchPhraseQuery("count",count)

默认使用 match_phrase 时会精确匹配查询的短语,需要全部单词和顺序要完全一样,标点符号除外。(一般用于内容模糊查询)

count= "i" 可查出
count= "i li" 查不出
count= "i like" 可查出
count= "i like eat" 查不出
count= "and" 可查出
count= "kuing" 可查出
count= "ku"  查不出
count= "li"  查不出
count= "eat"  查不出

3.QueryBuilders.matchPhrasePrefixQuery("count",count)

count= "i" 可查出
count= "i li" 可查出
count= "i like" 可查出
count= "i like eat" 可查出
count= "and" 可查出
count= "kuing" 可查出
****count= "ku"  可查出***
**count= "li"  可查出
count= "eat"  可查出***

4.QueryBuilders.termQuery("count",count)

term query,输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词。(一般用于关键字精确查询)

count= "i" 可查出
count = "i li" 查不出
count= "i like" 查不出
count= "i like eat" 查不出
count= "and" 可查出
count= "kuing" 可查出
****count= "ku"  查不出***
**count= "li"  查不出
count= "eat"  查不出***

二、多字段搜索

1.BoolQueryBuilder

BoolQueryBuilder qb = QueryBuilders.boolQuery();
1,qb.must(QueryBuilder queryBuilder);  // 相当于and 
2,qb.mustNot(QueryBuilder queryBuilder)  // 相当于 and !=
3,qb.should(QueryBuilder queryBuilder))  // 相当于 or
4,qb.filter(QueryBuilder queryBuilder))  // 返回的文档必须满足filter子句的条件,但是不会像must一样,参与计算分值

2.示例代码

//1.声明搜索客户端
SearchRequest searchRequest = new SearchRequest();
//2.设置索引库的名称
searchRequest.indices("qfedu-course");
//3.声明搜索资源对象,声明搜索的规则对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//4设置搜索规则   
//4.1QueryBuilders.matchAllQuery() 查询全部数据
searchSourceBuilder .query(QueryBuilders.matchAllQuery());
//4.2 QueryBuilders.termQuery :对搜索关键字不分词进行查询 精准查询
searchSourceBuilder.query(QueryBuilders.termQuery("name","张三"));
//4.2.1 termsQuery: 精准查询,一次匹配多个数据
searchSourceBuilder.query(QueryBuilders.termsQuery("id","25","24"));
//4.3 QueryBuilders.matchQuery(). 关键字分词进行查询
searchSourceBuilder.query(QueryBuilders.matchQuery("courseName","分布式课程入门").operator(Operator.AND));
//4.3.1 QueryBuilders.multiMatchQuery() 关键字匹配多个字段,可以给某一个字段设置 权重,提升的倍数。
searchSourceBuilder.query(QueryBuilders.multiMatchQuery("分布式OK", "courseName", "courseDescription").field("courseName", 10));
//4.4 时间范围搜索 .from(起始时间).to(结束时间)
searchSourceBuilder.query(QueryBuilders.must(QueryBuilders.rangeQuery("commentTime").from("2022-01-02 13:11:00").to("2022-01-02 14:11:00")));
//4.5 QueryBuilders.rangeQuery 范围查询 gte 大于等于 lte 小于等于
searchSourceBuilder.query(QueryBuilders.rangeQuery("pay").gte(1000).lte(2000));
//5.设置分页
searchSourceBuilder.size(realTimeInformationReq.getSize());
searchSourceBuilder.from((realTimeInformationReq.getPage() - 1) * realTimeInformationReq.getSize());
//6.将搜索资源对象设置到搜索客户端中
searchRequest.source(searchSourceBuilder);
//7.进行搜索
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
//8.解析搜索返回值
SearchHit[] hits = search.getHits().getHits();
//9.查询的总数
long count = search.getHits().getTotalHits().value;
//10.解析返回值
List list = new ArrayList();
for (SearchHit his : hits1) {
    Map sourceAsMap = his.getSourceAsMap();
    list.add(sourceAsMap);
}

三、高亮展示

1.工具类

public static String setHighLight(String keywords, String info) {
        if (StringUtils.isBlank(keywords)||StringUtils.isBlank(info)) {
            return info;
        }
        String[] keywordArray = keywords.split(" ");
        String newInfo = info;
        for (String keyword : keywordArray) {
            newInfo = newInfo.replace(keyword, "" + keyword + "");
        }
        return newInfo;
    }

2.高亮展示

for (TopicAnalysisMsg topicAnalysisMsg :list){   
//参数一:需要高亮展示的关键字;参数二:需要高亮展示的字段
    topicAnalysisMsg.setTitle(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getTitle()));
    topicAnalysisMsg.setAbstracts(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getAbstracts()));
    topicAnalysisMsg.setContent(DisplayUtil.setHighLight(topicAnalysisMsg.getKeyword(),topicAnalysisMsg.getContent()));
}

你可能感兴趣的:(elasticsearch,java,大数据)