Java Code Examples for org.frameworkset.elasticsearch.entity.ESDatas#getDatas()
The following examples show how to use
org.frameworkset.elasticsearch.entity.ESDatas#getDatas() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestByQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 6 votes |
public void deleteByQuery(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/byquery.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("country","UK"); //先查询数据是否存在 ESDatas<Employee> escompanys = clientUtil.searchList("company/employee/_search","deleteByQuery",params,Employee.class); List<Employee> companyList = escompanys.getDatas();//获取符合UK国家条件的公司的员工信息 long totalSize = escompanys.getTotalSize(); if(companyList != null && companyList.size() > 0) {//如果有雇员信息,则删除之 String result = clientUtil.deleteByQuery("company/employee/_delete_by_query?scroll_size=5000", "deleteByQuery", params); System.out.println(result); //删除后再次查询,验证数据是否被删除 escompanys = clientUtil.searchList("company/employee/_search","deleteByQuery",params,Employee.class); companyList = escompanys.getDatas();//获取符合UK国家条件的公司的员工信息 totalSize = escompanys.getTotalSize(); System.out.println("删除后再次查询,验证数据是否被删除:totalSize="+totalSize); } }
Example 2
Source File: ElasticsearchExecutorIT.java From pinpoint with Apache License 2.0 | 6 votes |
@Test public void searchDocuments() throws Exception { PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); try { Map<String, Object> condition = new HashMap<>(); //set description as search condition condition.put("description", "passat"); //search data to cars/car indice that match condition with a simple query dsl named testSearch defined in elasticsearchbboss/car-mapping.xml. ESDatas<Car> carESDatas = configRestClientInterface.searchList("cars/_search", "testSearch", condition, Car.class); //datas that match condition List<Car> cars = carESDatas.getDatas(); //totalsize that match condition long totalSize = carESDatas.getTotalSize(); } catch (Exception e) { } Class configClass = Class.forName("org.frameworkset.elasticsearch.client.ConfigRestClientUtil"); Method searchListMethod = configClass.getDeclaredMethod("searchList", String.class, String.class, Map.class, Class.class); verifier.verifyTrace(event(serviceType, searchListMethod)); }
Example 3
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 6 votes |
/** * 通过公司所在国家检索雇员信息,并返回雇员对应的公司信息 */ public void hasParentSearchByCountryReturnParent2ndChildren(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("country","UK"); try { ESInnerHitSerialThreadLocal.setESInnerTypeReferences(Company.class);//指定inner查询结果对于公司类型,公司只有一个文档类型,索引不需要显示指定company类型信息 ESDatas<Employee> escompanys = clientUtil.searchList("company/employee/_search", "hasParentSearchByCountryReturnParent2ndChildren",params,Employee.class); List<Employee> employeeList = escompanys.getDatas();//获取符合条件的雇员数据 long totalSize = escompanys.getTotalSize(); //查看每个雇员对应的公司信息 for(int i = 0; i < employeeList.size(); i ++) { Employee employee = employeeList.get(i); List<Company> companies = ResultUtil.getInnerHits(employee.getInnerHits(), "company"); System.out.println(companies.size()); } } finally{ ESInnerHitSerialThreadLocal.clean();//清空inner查询结果对于公司类型 } }
Example 4
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 6 votes |
/** * 检索公司信息,并返回公司对应的雇员信息(符合检索条件的雇员信息) */ public void hasChildSearchReturnParent2ndChildren(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("name","Alice Smith"); try { ESInnerHitSerialThreadLocal.setESInnerTypeReferences("employee",Employee.class);//指定inner查询结果对于雇员类型 // ESInnerHitSerialThreadLocal.setESInnerTypeReferences("othersontype",OtherSon.class);//指定inner查询结果对于雇员类型 ESDatas<Company> escompanys = clientUtil.searchList("company/company/_search", "hasChildSearchReturnParent2ndChildren",params,Company.class); long totalSize = escompanys.getTotalSize(); List<Company> companyList = escompanys.getDatas();//获取符合条件的公司 //查看公司下面的雇员信息(符合检索条件的雇员信息) for (int i = 0; i < companyList.size(); i++) { Company company = companyList.get(i); List<Employee> employees = ResultUtil.getInnerHits(company.getInnerHits(), "employee"); System.out.println(employees.size()); // List<OtherSon> otherSons = ResultUtil.getInnerHits(company.getInnerHits(), "othersontype"); // System.out.println(otherSons.size()); } } finally{ ESInnerHitSerialThreadLocal.clean();//清空inner查询结果对于雇员类型 } }
Example 5
Source File: PingyinTest.java From elasticsearch-gradle-example with Apache License 2.0 | 6 votes |
@Test public void searchGoodParent(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/estrace/pinyin.xml"); Map<String,String> params = new HashMap<String,String>(); // params.put("detailName","红谷滩红角洲"); params.put("name","xiahaotang"); params.put("distance","2km"); params.put("lon","115.825472"); params.put("lat","28.665041"); try { ESInnerHitSerialThreadLocal.setESInnerTypeReferences(Shop.class); ESSerialThreadLocal.setESTypeReferences(Good.class); ESDatas<SearchHit> datas = clientUtil.searchList("shop-good-user-1512023940/_search","goodParentSearch",params,SearchHit.class); List<SearchHit> goods = datas.getDatas(); for(int i = 0; i < goods.size(); i ++) { List<Shop> shops = ResultUtil.getInnerHits(goods.get(i).getInnerHits(), "shop", Shop.class); } System.out.print(clientUtil.executeRequest("shop-good-user-1512023940/_search?pretty", "goodParentSearch", params)); } finally { ESInnerHitSerialThreadLocal.clean(); ESSerialThreadLocal.clean(); } }
Example 6
Source File: PingyinTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
@Test public void searchPinyinDemo(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/estrace/pinyin.xml"); Map<String,String> params = new HashMap<String,String>(); params.put("name","zhang学友");//设置中文拼音混合检索条件 ESDatas<Map> esDatas = clientUtil.searchList("demo/_search","searchPinyinDemo",params,Map.class); List<Map> datas = esDatas.getDatas(); long totalSize = esDatas.getTotalSize(); }
Example 7
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
/** * 通过雇员生日检索公司信息 */ public void hasChildSearchByBirthday(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("birthday","1980-01-01"); ESDatas<Company> escompanys = clientUtil.searchList("company/company/_search","hasChildSearchByBirthday",params,Company.class); List<Company> companyList = escompanys.getDatas();//获取符合条件的公司 long totalSize = escompanys.getTotalSize(); }
Example 8
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
/** * 通过雇员姓名检索公司信息 */ public void hasChildSearchByName(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("name","Alice Smith"); ESDatas<Company> escompanys = clientUtil.searchList("company/company/_search","hasChildSearchByName",params,Company.class); List<Company> companyList = escompanys.getDatas();//获取符合条件的公司 long totalSize = escompanys.getTotalSize(); }
Example 9
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
/** * 通过雇员数量检索公司信息 */ public void hasChildSearchByMinChild(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("min_children",2); ESDatas<Company> escompanys = clientUtil.searchList("company/company/_search","hasChildSearchByMinChild",params,Company.class); List<Company> companyList = escompanys.getDatas();//获取符合条件的公司 long totalSize = escompanys.getTotalSize(); }
Example 10
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
/** * 通过公司所在国家检索雇员信息 */ public void hasParentSearchByCountry(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/indexparentchild.xml"); Map<String,Object> params = new HashMap<String,Object>(); params.put("country","UK"); ESDatas<Employee> escompanys = clientUtil.searchList("company/employee/_search","hasParentSearchByCountry",params,Employee.class); List<Employee> companyList = escompanys.getDatas();//获取符合条件的公司 long totalSize = escompanys.getTotalSize(); }
Example 11
Source File: ParentChildTest.java From elasticsearch-gradle-example with Apache License 2.0 | 5 votes |
/** * 查找顾客信息,演示多子文档父子数据查询功能 */ public void hasParentSearchByCountryReturnParent2ndMultiChildren(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/Client_Info.xml"); Map<String,Object> params = new HashMap<String,Object>();//没有检索条件,构造一个空的参数对象 // params.put("name","Alice Smith"); try { //设置子文档的类型和对象映射关系 ESInnerHitSerialThreadLocal.setESInnerTypeReferences("exam",Exam.class);//指定inner查询结果对于exam类型和对应的对象类型Exam ESInnerHitSerialThreadLocal.setESInnerTypeReferences("diagnosis",Diagnosis.class);//指定inner查询结果对于diagnosis类型和对应的对象类型Diagnosis ESInnerHitSerialThreadLocal.setESInnerTypeReferences("medical",Medical.class);//指定inner查询结果对于medical类型和对应的对象类型Medical ESDatas<Basic> escompanys = clientUtil.searchList("client_info/basic/_search", "hasParentSearchByCountryReturnParent2ndMultiChildren",params,Basic.class); // escompanys = clientUtil.searchAll("client_info",Basic.class); long totalSize = escompanys.getTotalSize(); List<Basic> clientInfos = escompanys.getDatas();//获取符合条件的数据 //查看公司下面的雇员信息(符合检索条件的雇员信息) for (int i = 0; clientInfos != null && i < clientInfos.size(); i++) { Basic clientInfo = clientInfos.get(i); List<Exam> exams = ResultUtil.getInnerHits(clientInfo.getInnerHits(), "exam"); if(exams != null) System.out.println(exams.size()); List<Diagnosis> diagnosiss = ResultUtil.getInnerHits(clientInfo.getInnerHits(), "diagnosis"); if(diagnosiss != null) System.out.println(diagnosiss.size()); List<Medical> medicals = ResultUtil.getInnerHits(clientInfo.getInnerHits(), "medical"); if(medicals != null) System.out.println(medicals.size()); } } finally{ ESInnerHitSerialThreadLocal.clean();//清空inner查询结果对于雇员类型 } }
Example 12
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
/** * 并行方式执行slice scroll操作 */ @Test public void testParralSliceScroll() { final ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); final List<String> scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 final int max = 6; final CountDownLatch countDownLatch = new CountDownLatch(max);//线程任务完成计数器,每个线程对应一个sclice,每运行完一个slice任务,countDownLatch计数减去1 for (int j = 0; j < max; j++) { final int i = j; Thread sliceThread = new Thread(new Runnable() {//多线程并行执行scroll操作做,每个线程对应一个sclice @Override public void run() { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas<Map> sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List<Map> sliceDatas = sliceResponse.getDatas(); incrementSize( sliceDatas.size());//统计实际处理的文档数量 long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } incrementSize( sliceDatas.size());//统计实际处理的文档数量 } while (true); } countDownLatch.countDown();//slice检索完毕后计数器减1 } }); sliceThread.start(); } try { countDownLatch.await();//等待所有的线程执行完毕,计数器变成0 } catch (InterruptedException e) { e.printStackTrace(); } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); // System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); }
Example 13
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
@Test public void testScroll(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); //scroll分页检索 List<String > scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); Map params = new HashMap(); params.put("size", 100);//每页100条记录 ESDatas<Map> response = clientUtil.searchList("agentstat-*/_search?scroll=1m","scrollQuery",params,Map.class); List<Map> datas = response.getDatas(); long realTotalSize = datas.size(); long totalSize = response.getTotalSize(); String scrollId = response.getScrollId(); if(scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:"+totalSize); System.out.println("scrollId:"+scrollId); if(datas != null && datas.size() > 0) {//每页1000条记录,迭代scrollid,遍历scroll分页结果 do { response = clientUtil.searchScroll("1m",scrollId,Map.class); scrollId = response.getScrollId(); totalSize = response.getTotalSize(); System.out.println("scroll totalSize:"+totalSize); if(scrollId != null) scrollIds.add(scrollId); datas = response.getDatas(); if(datas == null || datas.size() == 0){ break; } realTotalSize = realTotalSize + datas.size(); } while (true); } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize+",totalSize:"+totalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); // System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); }
Example 14
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
/** * 串行方式执行slice scroll操作 */ @Test public void testSliceScroll() { ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); List<String> scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 int max = 6; long realTotalSize = 0; for (int i = 0; i < max; i++) { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas<Map> sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List<Map> sliceDatas = sliceResponse.getDatas(); realTotalSize = realTotalSize + sliceDatas.size(); long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } realTotalSize = realTotalSize + sliceDatas.size(); } while (true); } } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls); }
Example 15
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
/** * 并行方式执行slice scroll操作 */ @Test public void testParralSliceScroll() { final ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); final List<String> scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 final int max = 6; final CountDownLatch countDownLatch = new CountDownLatch(max);//线程任务完成计数器,每个线程对应一个sclice,每运行完一个slice任务,countDownLatch计数减去1 for (int j = 0; j < max; j++) { final int i = j; Thread sliceThread = new Thread(new Runnable() {//多线程并行执行scroll操作做,每个线程对应一个sclice @Override public void run() { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas<Map> sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List<Map> sliceDatas = sliceResponse.getDatas(); incrementSize( sliceDatas.size());//统计实际处理的文档数量 long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } incrementSize( sliceDatas.size());//统计实际处理的文档数量 } while (true); } countDownLatch.countDown();//slice检索完毕后计数器减1 } }); sliceThread.start(); } try { countDownLatch.await();//等待所有的线程执行完毕,计数器变成0 } catch (InterruptedException e) { e.printStackTrace(); } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); // System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); }
Example 16
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
/** * 串行方式执行slice scroll操作 */ @Test public void testSliceScroll() { ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); List<String> scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 int max = 6; long realTotalSize = 0; for (int i = 0; i < max; i++) { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas<Map> sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List<Map> sliceDatas = sliceResponse.getDatas(); realTotalSize = realTotalSize + sliceDatas.size(); long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } realTotalSize = realTotalSize + sliceDatas.size(); } while (true); } } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls); }
Example 17
Source File: TestScrollQuery.java From elasticsearch-gradle-example with Apache License 2.0 | 4 votes |
@Test public void testScroll(){ ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml"); //scroll分页检索 List<String > scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); Map params = new HashMap(); params.put("size", 100);//每页100条记录 ESDatas<Map> response = clientUtil.searchList("agentstat-*/_search?scroll=1m","scrollQuery",params,Map.class); List<Map> datas = response.getDatas(); long realTotalSize = datas.size(); long totalSize = response.getTotalSize(); String scrollId = response.getScrollId(); if(scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:"+totalSize); System.out.println("scrollId:"+scrollId); if(datas != null && datas.size() > 0) {//每页1000条记录,迭代scrollid,遍历scroll分页结果 do { response = clientUtil.searchScroll("1m",scrollId,Map.class); scrollId = response.getScrollId(); totalSize = response.getTotalSize(); System.out.println("scroll totalSize:"+totalSize); if(scrollId != null) scrollIds.add(scrollId); datas = response.getDatas(); if(datas == null || datas.size() == 0){ break; } realTotalSize = realTotalSize + datas.size(); } while (true); } long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize+",totalSize:"+totalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); // System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); // System.out.println(scrolls); }