|
@@ -0,0 +1,111 @@
|
|
|
+package edu.travel.elastic;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
+import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
+import org.elasticsearch.client.RestHighLevelClient;
|
|
|
+import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
+import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 操作ES索引
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class EsIndexOperation {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestHighLevelClient restHighLevelClient;
|
|
|
+ private final RequestOptions options = RequestOptions.DEFAULT;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断索引是否存在
|
|
|
+ */
|
|
|
+ public boolean checkIndex(String index) {
|
|
|
+ try {
|
|
|
+ return restHighLevelClient.indices().exists(new GetIndexRequest(index), options);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("EsIndexOperation checkIndex error.", e);
|
|
|
+ }
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建索引
|
|
|
+ *
|
|
|
+ * @param indexName es索引名
|
|
|
+ * @param esSettingFilePath es索引的alias、settings和mapping的配置文件
|
|
|
+ */
|
|
|
+ public boolean createIndex(String indexName, String esSettingFilePath) {
|
|
|
+ String aliases = null;
|
|
|
+ String mappings = null;
|
|
|
+ String settings = null;
|
|
|
+ if (StringUtils.isNotBlank(esSettingFilePath)) {
|
|
|
+ try {
|
|
|
+ String fileContent = FileUtils.readFileContent(esSettingFilePath);
|
|
|
+ if (StringUtils.isNotBlank(fileContent)) {
|
|
|
+ JSONObject jsonObject = JSON.parseObject(fileContent);
|
|
|
+ aliases = jsonObject.getString("aliases");
|
|
|
+ mappings = jsonObject.getString("mappings");
|
|
|
+ settings = jsonObject.getString("settings");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("createIndex error.", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (checkIndex(indexName)) {
|
|
|
+ log.error("createIndex indexName:[{}]已存在", indexName);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ CreateIndexRequest request = new CreateIndexRequest(indexName);
|
|
|
+ if ((StringUtils.isNotBlank(aliases))) {
|
|
|
+ request.aliases(aliases, XContentType.JSON);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(mappings)) {
|
|
|
+ request.mapping(mappings, XContentType.JSON);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(settings)) {
|
|
|
+ request.settings(settings, XContentType.JSON);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ this.restHighLevelClient.indices().create(request, options);
|
|
|
+ return true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("EsIndexOperation createIndex error.", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除索引
|
|
|
+ */
|
|
|
+ public boolean deleteIndex(String indexName) {
|
|
|
+ try {
|
|
|
+ if (checkIndex(indexName)) {
|
|
|
+ DeleteIndexRequest request = new DeleteIndexRequest(indexName);
|
|
|
+ AcknowledgedResponse response = restHighLevelClient.indices().delete(request, options);
|
|
|
+ return response.isAcknowledged();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("EsIndexOperation deleteIndex error.", e);
|
|
|
+ }
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
+}
|