为什么要用ElasticSearch
SQL模糊查询,模糊查询前置配置,会放弃索引,,在百万级别的数据库中,效率非常低下
使用
Java代码
// 构建搜索条件 .trackTotalHits(true) 显示数据的真实条数,不使用最多显示一万条
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
//分页 from size from从第几条开始 size 结束
searchSourceBuilder.from((Current-1)*Size).size(Size);
//查询条件
searchSourceBuilder.query(boolQueryBuilder);
// 构建请求
SearchRequest searchRequest = new SearchRequest("你index的名称");
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//分页数据集合
ArrayList<Map<String, Object>> maps = new ArrayList<>();
//获取总条数
TotalHits totalHits = response.getHits().getTotalHits();
long value = totalHits.value;
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
maps.add(sourceAsMap);
}