解决 group_concat 默认长度限制问题
GROUP_CONCAT 有个最大长度的限制,超过最大长度就会被截断掉
GROUP_CONCAT 有个最大长度的限制,超过最大长度就会被截断掉
三者之间的区别
public void testSearch(String indexName,String orderName) throws IOException {
long startTime = System.currentTimeMillis();
// 1、创建searchRequest
SearchRequest searchRequest = new SearchRequest(indexName);
// 2、指定查询条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//必须加上track_total_hits,不然就只显示10000
// 页面上的第一页等同于在es中的 0
sourceBuilder.from(0);
// 每页多少条数据
sourceBuilder.size(100);
// 设置唯一排序值定位
sourceBuilder.sort(SortBuilders.fieldSort(orderName).order(SortOrder.DESC));
//将sourceBuilder对象添加到搜索请求中
searchRequest.source(sourceBuilder);
// 发送请求
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
List<Map<String, Object>> result = new ArrayList<>();
if (hits != null && hits.length > 0) {
for (SearchHit hit : hits) {
// 获取需要数据
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
result.add(sourceAsMap);
}
}
logger.info("查询出来的数据个数为:{}", result.size());
// 关闭客户端
esClient.close();
logger.info("运行时间: " + (System.currentTimeMillis() - startTime) + "ms");
}
如果 from size 查询的数据超过 10000 条,会报错误:
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
Result window is too large, from + size must be less than or equal to: [10000] but was [10030]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.
public void testSearch(String indexName,String orderName) throws IOException {
long startTime = System.currentTimeMillis();
// 1、创建searchRequest
SearchRequest searchRequest = new SearchRequest(indexName);
// 2、指定查询条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);//必须加上track_total_hits,不然就只显示10000
//设置每页查询的数据个数
sourceBuilder.size(1000);
// 设置唯一排序值定位
sourceBuilder.sort(SortBuilders.fieldSort(orderName).order(SortOrder.DESC));//多条件查询
//将sourceBuilder对象添加到搜索请求中
searchRequest.source(sourceBuilder);
// 发送请求
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits1 = searchResponse.getHits().getHits();
List<Map<String, Object>> result = new ArrayList<>();
if (hits1 != null && hits1.length > 0) {
do {
for (SearchHit hit : hits1) {
// 获取需要数据
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
result.add(sourceAsMap);
}
// 取得最后得排序值sort,用于记录下次将从这个地方开始取数
SearchHit[] hits = searchResponse.getHits().getHits();
Object[] lastNum = hits[hits.length - 1].getSortValues();
// 设置searchAfter的最后一个排序值
sourceBuilder.searchAfter(lastNum);
searchRequest.source(sourceBuilder);
// 进行下次查询
searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
} while (searchResponse.getHits().getHits().length != 0);
}
logger.info("查询出来的数据个数为:{}", result.size());
// 关闭客户端
esClient.close();
logger.info("运行时间: " + (System.currentTimeMillis() - startTime) + "ms");
}
public void testSearch(String indexName,String orderName) throws IOException {
long startTime = System.currentTimeMillis();
// 1、创建searchRequest
SearchRequest searchRequest = new SearchRequest(indexName);
// 2、指定scroll信息
searchRequest.scroll(TimeValue.timeValueMinutes(1L));
// 3、指定查询条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(1000);
searchSourceBuilder.sort(SortBuilders.fieldSort(orderName).order(SortOrder.DESC));//多条件查询
searchRequest.source(searchSourceBuilder);
//4、获取返回结果scrollId,source
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT); //通过发送初始搜索请求来初始化搜索上下文
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
List<Map<String, Object>> result = new ArrayList<>();
for (SearchHit hit: searchHits) {
result.add(hit.getSourceAsMap());
}
// java也是一样要查询两次,先把我们的首页给查询出来
// 查询出来之后我们要获取他的id
// 然后利用他的id去查询他的下一页
while (true) {
//5、循环 - 创建 SearchScrollRequest 创建一个新的搜索滚动请求,保存最后返回的滚动标识符和滚动间隔
// 获取 scrollId 去查询下一页
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
//6、指定scrollId的生存时间
scrollRequest.scroll(TimeValue.timeValueMinutes(1L));
//7、执行查询获取返回结果
SearchResponse scrollResp = esClient.scroll(scrollRequest, RequestOptions.DEFAULT);
//8、判断是否查询到了数据,输出
SearchHit[] hits = scrollResp.getHits().getHits();
//循环输出下一页
if (hits != null && hits.length > 0) {
for (SearchHit hit : hits) {
result.add(hit.getSourceAsMap());
}
} else {
//9、判断没有查询到数据,退出循环
break;
}
}
//查完之后我们把存进缓存的id给删除 完成滚动后,清除滚动上下文
//10、创建ClearScrollRequest
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
//11、指定scrollId
clearScrollRequest.addScrollId(scrollId);
//12、删除scrollId
ClearScrollResponse clearScrollResponse = esClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
//13、输出结果
boolean succeeded = clearScrollResponse.isSucceeded();
logger.info("删除scrollId:{}", succeeded);
logger.info("查询总个数:{}", result.size());
// 关闭客户端
esClient.close();
logger.info("运行时间: " + (System.currentTimeMillis() - startTime) + "ms");
}
记录在用 Bootstrap Multiselect 的时候,从后台传来的值,动态赋值的解决方法。