Clase de herramienta ElasticSearchUtils
Herramientas de EsUtils
public class EsUtils {
private static final Logger logger = LoggerFactory.getLogger(EsUtils.class);
public static boolean isExistsIndex(RestHighLevelClient client, String indexName) {
GetIndexRequest indexRequest = new GetIndexRequest(indexName);
try {
return client.indices().exists(indexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
logger.error("=====> isExistsIndex()出错:" + e.getMessage());
}
return false;
}
public static void createIndex(RestHighLevelClient client, String indexName) {
CreateIndexRequest index = new CreateIndexRequest(indexName);
try {
CreateIndexResponse response = client.indices().create(index, RequestOptions.DEFAULT);
logger.info("=====> es创建索引:" + indexName + response);
} catch (Exception e) {
logger.error("=====> createIndex()出错:" + e.getMessage());
}
}
public static void deleteIndex(RestHighLevelClient client, String indexName) {
DeleteIndexRequest index = new DeleteIndexRequest(indexName);
try {
AcknowledgedResponse delete = client.indices().delete(index, RequestOptions.DEFAULT);
logger.info("=====> es删除索引:" + indexName + delete);
} catch (Exception e) {
logger.error("=====> deleteIndex()出错:" + e.getMessage());
}
}
public static void addDocument(RestHighLevelClient client, String indexName, Object object) {
IndexRequest request = new IndexRequest(indexName);
request.source(JSONObject.toJSONString(object), XContentType.JSON);
IndexResponse indexResponse = null;
try {
indexResponse = client.index(request, RequestOptions.DEFAULT);
logger.info("=====> addDocument():" + JSONObject.toJSONString(indexResponse));
} catch (IOException e) {
logger.error("=====> addDocument()出错:" + e.getMessage());
}
}
public static boolean isExistsDocument(RestHighLevelClient client, String indexName, String indexId) {
GetRequest getRequest = new GetRequest(indexName, indexId);
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
try {
return client.exists(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("=====> isExistsDocument()出错:" + e.getMessage());
}
return false;
}
public static Object getDocument(RestHighLevelClient client, String indexName, String indexId) {
GetRequest getRequest = new GetRequest(indexName, indexId);
try {
return client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("=====> getDocument()出错:" + e.getMessage());
}
return null;
}
public static void updateDocument(RestHighLevelClient client, String indexName, String indexId, Object object) {
UpdateRequest request = new UpdateRequest(indexName, indexId);
request.timeout("1s");
request.doc(JSON.toJSONString(object), XContentType.JSON);
try {
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
logger.info("=====> updateDocument():" + JSONObject.toJSONString(response));
} catch (IOException e) {
logger.error("=====> updateDocument()出错:" + e.getMessage());
}
}
public static void deleteDocument(RestHighLevelClient client, String indexName, String indexId) {
DeleteRequest request = new DeleteRequest(indexName, indexId);
request.timeout("1s");
try {
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
logger.info("=====> deleteDocument():" + JSONObject.toJSONString(response));
} catch (IOException e) {
logger.error("=====> deleteDocument()出错:" + e.getMessage());
}
}
public static void multiAdd(RestHighLevelClient client, String indexName, List<Object> list) {
BulkRequest request = new BulkRequest();
request.timeout("10s");
for (int i = 0; i < list.size(); i++) {
request.add(new IndexRequest(indexName)
.id("" + (i + 1))
.source(JSON.toJSONString(list.get(i)), XContentType.JSON));
}
try {
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
logger.info("=====> multiAdd():" + JSONObject.toJSONString(response));
} catch (IOException e) {
logger.error("=====> multiAdd()出错:" + e.getMessage());
}
}
public String getEsResultBysql(String sql) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
String urlLocation = "http://127.0.0.1:9200/_sql";
String content = "{\"query\":\"" + sql + "\"}";
URL realUrl = new URL(urlLocation);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
((HttpURLConnection) conn).setRequestMethod("POST");
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(content);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}