yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
fanli/src/main/java/com/yeshi/fanli/dao/ElasticBaseDao.java
@@ -5,8 +5,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
@@ -15,8 +15,6 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
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;
@@ -25,23 +23,20 @@
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Repository;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.elasticsearch.Document;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.data.elasticsearch.annotations.Document;
import com.yeshi.fanli.service.manger.ElasticManger;
@Repository
public abstract class ElasticBaseDao<T> {
   // 地址
   private static String host = "192.168.1.200";
   // 端口
   private static Integer port = 9200;
   @Resource
   protected  ElasticManger elasticManger;
   
   // 初始化api客户端
   public static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port,"http")));;
   
   /**
    * 获取index
@@ -51,10 +46,8 @@
   public String getDocument(Class<?> entityClass) {
      if (entityClass.isAnnotationPresent(Document.class)) {
           Document doc = (Document)entityClass.getAnnotation(Document.class);
           System.out.println(doc.indexName());
           return doc.indexName();
      }
      String name = entityClass.getName();
      return name.substring(name.lastIndexOf(".") + 1, name.length());
   }
@@ -69,13 +62,12 @@
    */
   public void save(T bean) {
      String document = getDocument(bean.getClass());
      String json = JsonUtil.getSimpleGson().toJson(bean);
      IndexRequest request = new IndexRequest(document);
      request.source(json, XContentType.JSON);
      // 同步执行
      try {
         client.index(request, RequestOptions.DEFAULT);
         elasticManger.getClient().index(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
@@ -95,7 +87,7 @@
      request.source(json, XContentType.JSON);
      // 同步执行
      try {
         client.index(request, RequestOptions.DEFAULT);
         elasticManger.getClient().index(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
@@ -113,7 +105,7 @@
      String json = new GsonBuilder().create().toJson(bean);
      request.doc(json, XContentType.JSON);
      try {
         client.update(request, RequestOptions.DEFAULT);
         elasticManger.getClient().update(request, RequestOptions.DEFAULT);
      } catch (Exception e) {
         e.printStackTrace();
      }
@@ -129,7 +121,7 @@
      String document = getDocument(entityClass);
      DeleteRequest request = new DeleteRequest(document, id);
      try {
         client.delete(request, RequestOptions.DEFAULT);
         elasticManger.getClient().delete(request, RequestOptions.DEFAULT);
      } catch (IOException e) {
         e.printStackTrace();
      }
@@ -145,7 +137,7 @@
      String document = getDocument(entityClass);
      GetRequest request = new GetRequest(document, id);
      try {
         GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
         GetResponse getResponse = elasticManger.getClient().get(request, RequestOptions.DEFAULT);
         if (getResponse.isExists()) {
             String content = getResponse.getSourceAsString();        
             return new Gson().fromJson(content, entityClass);
@@ -178,7 +170,7 @@
      searchRequest.indices(index);
      searchRequest.source(sourceBuilder);
      try {
         SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
         SearchResponse searchResponse = elasticManger.getClient().search(searchRequest, RequestOptions.DEFAULT);
         SearchHits hits = searchResponse.getHits();
         SearchHit[] searchHits = hits.getHits();