|
@ -13,6 +13,8 @@ import org.elasticsearch.action.get.GetRequest;
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
|
import org.elasticsearch.action.index.IndexResponse;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
|
import org.elasticsearch.action.update.UpdateResponse;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
@ -21,13 +23,19 @@ import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.rest.RestStatus;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Modifier;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@ -181,16 +189,76 @@ public class ElasticSearch7Helper {
|
|
|
return !bulkResponse.hasFailures();
|
|
|
}
|
|
|
|
|
|
// public SearchResult search(String index, String type, String queryStr) throws IOException {
|
|
|
// Search search = ((new Search.Builder(queryStr)).addIndex(index)).addType(type).build();
|
|
|
// SearchResult result = jestClient.execute(search);
|
|
|
// this.logger.info("search data count: " + result.getTotal());
|
|
|
// return result;
|
|
|
// }
|
|
|
public <T> List<T> search(String index, SearchSourceBuilder queryStr,Class<T> beanClass) throws Exception {
|
|
|
SearchRequest request = new SearchRequest().indices(index).source(queryStr);
|
|
|
|
|
|
//带入请求执行查询
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
|
|
//得到查询结果
|
|
|
SearchHits hits = searchResponse.getHits();
|
|
|
|
|
|
SearchHit[] searchHits = hits.getHits();
|
|
|
List<T> listData = new ArrayList<>();
|
|
|
//遍历查询结果
|
|
|
for(SearchHit hit : searchHits){
|
|
|
Map<String,Object> datas = hit.getSourceAsMap();
|
|
|
T t = beanClass.newInstance();
|
|
|
BeanUtils.copyProperties(t,datas);
|
|
|
listData.add(t);
|
|
|
logger.info(datas.toString());
|
|
|
}
|
|
|
return listData;
|
|
|
}
|
|
|
|
|
|
public List<String> search(String index, SearchSourceBuilder queryStr) throws Exception {
|
|
|
SearchRequest request = new SearchRequest().indices(index).source(queryStr);
|
|
|
//带入请求执行查询
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
|
|
//得到查询结果
|
|
|
SearchHits hits = searchResponse.getHits();
|
|
|
|
|
|
SearchHit[] searchHits = hits.getHits();
|
|
|
List<String> listData = new ArrayList<>();
|
|
|
//遍历查询结果
|
|
|
for(SearchHit hit : searchHits){
|
|
|
String datas = hit.getSourceAsString();
|
|
|
listData.add(datas);
|
|
|
logger.info(datas);
|
|
|
}
|
|
|
return listData;
|
|
|
}
|
|
|
|
|
|
public int esCount(String index, SearchSourceBuilder queryStr) throws Exception{
|
|
|
SearchRequest request = new SearchRequest().indices(index).source(queryStr);
|
|
|
|
|
|
//带入请求执行查询
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
|
|
//得到查询结果
|
|
|
SearchHits hits = searchResponse.getHits();
|
|
|
SearchHit[] searchHits = hits.getHits();
|
|
|
return searchHits.length;
|
|
|
}
|
|
|
|
|
|
public String search(String index, String id) throws IOException {
|
|
|
GetRequest request = new GetRequest(index, id.toString());
|
|
|
GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT);
|
|
|
return getResponse.getSourceAsString();
|
|
|
}
|
|
|
|
|
|
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
|
|
|
if (map == null)
|
|
|
return null;
|
|
|
Object obj = beanClass.newInstance();
|
|
|
Field[] fields = obj.getClass().getDeclaredFields();
|
|
|
for (Field field : fields) {
|
|
|
int mod = field.getModifiers();
|
|
|
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
|
|
|
continue;
|
|
|
}
|
|
|
field.setAccessible(true);
|
|
|
field.set(obj, map.get(field.getName()));
|
|
|
}
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
}
|