一組獲取Elasticsearch 索引表所有文件API使用案例
阿新 • • 發佈:2018-11-19
he best elasticsearch highlevel java rest api-----bboss
一組獲取Elasticsearch 索引表所有文件API使用案例
- 統計文件總數量api
- 簡單獲取所有文件api
- 並行獲取所有文件api
直接看案例:
package org.bboss.elasticsearchtest.searchall;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.frameworkset.elasticsearch.scroll.ScrollHandler;
import org.junit.Test;
import java.util.List;
import java.util.Map;
/**
* <p>Description: 檢索所有文件資料測試用例</p>
* <p></p>
* <p>Copyright (c) 2018</p>
* @Date 2018/10/14 20:07
* @author biaoping.yin
* @version 1.0
*/
public class SearchAllTest {
/**
* 統計索引中有多少文件
*/
@Test
public void testCountAll(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
long esDatas = clientInterface.countAll("demo");
System.out.println("TotalSize:" +esDatas);
}
/**
* 簡單的檢索索引表所有文件資料,預設分5000條記錄一批從es獲取資料
*/
@Test
public void testSearchAll(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",Map.class);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 簡單的檢索索引表所有文件資料,按指定的10000條記錄一批從es獲取資料
*/
@Test
public void testSearchAllFethchSize(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",10000,Map.class);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 檢索索引表所有文件資料,預設分5000條記錄一批從es獲取資料,分批獲取的資料交個一ScrollHandler來處理
*/
@Test
public void testSearchAllHandler(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo", new ScrollHandler<Map>() {
public void handle(ESDatas<Map> esDatas) throws Exception {
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
},Map.class);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 檢索索引表所有文件資料,按指定的10000條記錄一批從es獲取資料,分批獲取的資料交個一ScrollHandler來處理
*/
@Test
public void testSearchAllFethchSizeHandler(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",10000,new ScrollHandler<Map>() {
public void handle(ESDatas<Map> esDatas) throws Exception {
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
},Map.class);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 並行檢索索引表所有文件資料,預設分5000條記錄一批從es獲取資料,指定了並行的執行緒數為6
*/
@Test
public void testSearchAllParrrel(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",Map.class,6);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 並行檢索索引表所有文件資料,按指定的10000條記錄一批從es獲取資料,指定了並行的執行緒數為6
*/
@Test
public void testSearchAllFethchSizeParrrel(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",10000,Map.class,6);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 並行檢索索引表所有文件資料,預設分5000條記錄一批從es獲取資料,分批獲取的資料交個一ScrollHandler來處理,指定了並行的執行緒數為6
*/
@Test
public void testSearchAllHandlerParrrel(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo", new ScrollHandler<Map>() {
public void handle(ESDatas<Map> esDatas) throws Exception {
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
},Map.class,6);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
/**
* 並行檢索索引表所有文件資料,按指定的10000條記錄一批從es獲取資料,分批獲取的資料交個一ScrollHandler來處理,指定了並行的執行緒數為6
*/
@Test
public void testSearchAllFethchSizeHandlerParrrel(){
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil();
ESDatas<Map> esDatas = clientInterface.searchAll("demo",10000,new ScrollHandler<Map>() {
public void handle(ESDatas<Map> esDatas) throws Exception {
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
},Map.class,6);
List<Map> dataList = esDatas.getDatas();
System.out.println("TotalSize:"+esDatas.getTotalSize());
if(dataList != null) {
System.out.println("dataList.size:" + dataList.size());
}
else
{
System.out.println("dataList.size:0");
}
}
}
開發交流
完整的demo工程
https://gitee.com/bbossgroups/eshelloword-booter
elasticsearch技術交流群:166471282
elasticsearch微信公眾號: