import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.*;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Title: ElasticsearchUtil
* @Description: 工具类
*/
@Component
@Slf4j
public class ElasticsearchUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchUtil.class);
public static final String HTTP_METHOD_GET = "GET";
public static final String HTTP_METHOD_POST = "POST";
public static final String HTTP_METHOD_PUT = "PUT";
public static final String HTTP_METHOD_DELETE = "DELETE";
public static final String HTTP_METHOD_HEAD = "HEAD";
@Resource(name="restHighLevelClient")
private RestHighLevelClient restHighLevelClient;
private static RestHighLevelClient client;
private static ObjectMapper mapper = new ObjectMapper();
/**
* @PostContruct是spring框架的注解
* spring容器初始化的时候执行该方法
*/
@PostConstruct
public void init() {
client = this.restHighLevelClient;
}
/**
* 创建索引
*
* @param index
* @return
*/
// public static boolean createIndex(String index) {
// //index名必须全小写,否则报错
// CreateIndexRequest request = new CreateIndexRequest(index);
// try {
// CreateIndexResponse indexResponse = client.getLowLevelClient()..create(request);
// if (indexResponse.isAcknowledged()) {
// LOGGER.info("创建索引成功");
// } else {
// LOGGER.info("创建索引失败");
// }
// return indexResponse.isAcknowledged();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// return false;
// }
/**
* 插入数据
* @param index
* @param type
* @param object
* @return
*/
public static String addData(String index,String type,String id,JSONObject object) {
IndexRequest indexRequest = new IndexRequest(index, type, id);
try {
indexRequest.source(mapper.writeValueAsString(object), XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest);
return indexResponse.getId();
} catch (Exception e) {
log.warn("addData fail ",e);
}
return null;
}
/**
*
* @param index
* @param type
* @param id
* @param object
* @return
*/
public static String modifyData(String index,String type,String id,JSONObject object) {
try {
UpdateRequest updateRequest = new UpdateRequest(index, type, id);
updateRequest.doc(mapper.writeValueAsString(object),XContentType.JSON);
UpdateResponse response = client.update(updateRequest);
// urb.setDoc(data);
// urb.setDetectNoop(false);//默认是true
// urb.execute().actionGet();
return response.toString();
} catch (Exception e) {
log.warn("modifyData fail ",e);
}
return null;
}
public static String queryData(String method,IndexRequest indexRequest,String endPoint) throws IOException {
String source = indexRequest.source().utf8ToString();
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
Response response = client.getLowLevelClient().performRequest(method, endPoint, Collections.
return EntityUtils.toString(response.getEntity());
}
/**
* 检查索引
* @param index
* @return
* @throws IOException
*/
public static boolean checkIndexExist(String index) {
try {
Response response = client.getLowLevelClient().performRequest("HEAD", index);
boolean exist = response.getStatusLine().getReasonPhrase().equals("OK");
return exist;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static String search(String method,String endpoint,String queryString) throws IOException {
Map
HttpEntity entity = new NStringEntity(queryString, ContentType.APPLICATION_JSON);
// try {
Response response = client.getLowLevelClient().performRequest(method, endpoint, params, entity);
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSON.parseObject(responseBody);
return responseBody;
// }catch (ResponseException e){
// e.printStackTrace();
// }
}
public static JSONObject searchByReturnJSONObject(String method,String endpoint,String queryString) throws IOException {
Map
HttpEntity entity = new NStringEntity(queryString, ContentType.APPLICATION_JSON);
Response response = client.getLowLevelClient().performRequest(method, endpoint, params, entity);
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSON.parseObject(responseBody);
return jsonObject;
}
public Boolean getSourceByFilters(String index,String fieldName, String filtervalue) throws IOException {
//Single request quantity
int elasticsearchsize = 10;
//High Level Client init
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.scroll(scroll);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//Aggregate statement
searchSourceBuilder.query(QueryBuilders.boolQuery()
//.must(QueryBuilders.queryStringQuery(filters))
//.must(QueryBuilders.termsQuery("uri",filters))
.must(QueryBuilders.matchPhraseQuery(fieldName,filtervalue))
// .must(QueryBuilders.rangeQuery("@timestamp").gte(gte).lte(lte))
)
.timeout(new TimeValue(10000, TimeUnit.SECONDS))
.size(elasticsearchsize);
searchRequest.source(searchSourceBuilder);
//Print the executed DSL statement, which can be used directly in kibana
//LOGGER.info(searchSourceBuilder.toString());
// BasicHeader header = new BasicHeader("Content-Type", "application/json");
SearchResponse searchResponse = client.search(searchRequest);
if (searchResponse.getHits().totalHits == 0){
return null;
}else {
Boolean kafkaresstatus = new Boolean(true);
if ("OK".equals(searchResponse.status().toString())){
List list = new ArrayList<>();
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit hit : searchResponse.getHits().getHits()) {
String res = hit.getSourceAsString();
}
long totalHits = searchResponse.getHits().getTotalHits();
long length = searchResponse.getHits().getHits().length;
LOGGER.info("A total of [{}] data was retrieved, and the number of data processed [{}]",totalHits, length);
while (searchHits != null && searchHits.length > 0) {
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = client.searchScroll(scrollRequest);
for (SearchHit hit : searchResponse.getHits().getHits()) {
String res = hit.getSourceAsString();
}
length += searchResponse.getHits().getHits().length;
LOGGER.info("A total of [{}] data was retrieved, and the number of data processed [{}]",totalHits, length);
if (length == totalHits){
break;
}
}
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(scrollId);
ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest);
boolean succeeded = clearScrollResponse.isSucceeded();
return kafkaresstatus;
}else {
return false;
}
}
}
/**
* 获取低水平客户端
* @return
*/
public static RestClient getLowLevelClient() {
return client.getLowLevelClient();
}
}