From 1c8b5d11b68e727ef5ccc130327e2400f937321b Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 28 十二月 2019 10:55:51 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/div' into div

---
 fanli/src/main/java/com/yeshi/fanli/dao/ElasticBaseDao.java |  196 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 196 insertions(+), 0 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/dao/ElasticBaseDao.java b/fanli/src/main/java/com/yeshi/fanli/dao/ElasticBaseDao.java
new file mode 100644
index 0000000..36e11b6
--- /dev/null
+++ b/fanli/src/main/java/com/yeshi/fanli/dao/ElasticBaseDao.java
@@ -0,0 +1,196 @@
+package com.yeshi.fanli.dao;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+
+import org.apache.http.HttpHost;
+import org.elasticsearch.action.delete.DeleteRequest;
+import org.elasticsearch.action.get.GetRequest;
+import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.search.SearchRequest;
+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;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.springframework.stereotype.Repository;
+import org.yeshi.utils.JsonUtil;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import org.springframework.data.elasticsearch.annotations.Document;
+
+
+
+@Repository
+public abstract class ElasticBaseDao<T> {
+	// 鍦板潃
+	private static String host = "192.168.1.200";
+	// 绔彛
+	private static Integer port = 9200;
+	
+	// 鍒濆鍖朼pi瀹㈡埛绔�
+	public static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port,"http")));;
+	
+	/**
+	 * 鑾峰彇index
+	 * @param bean
+	 * @return
+	 */
+	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());
+	}
+
+    
+	
+	/**
+	 * 鎻掑叆鏁版嵁
+	 * 
+	 * @param bean
+	 * @return
+	 */
+	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);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * 鎻掑叆鏁版嵁
+	 * 
+	 * @param bean
+	 * @return
+	 */
+	public void save(T bean, String id) {
+		String document = getDocument(bean.getClass());
+		
+		String json = JsonUtil.getSimpleGson().toJson(bean);
+		IndexRequest request = new IndexRequest(document).id(id);
+		request.source(json, XContentType.JSON);
+		// 鍚屾鎵ц
+		try {
+			client.index(request, RequestOptions.DEFAULT);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * 鏇存柊
+	 * @param Document id
+	 * @param bean
+	 */
+	public void update(String id, T bean) {
+		String document = getDocument(bean.getClass());
+		
+		UpdateRequest request = new UpdateRequest(document, id);
+		String json = new GsonBuilder().create().toJson(bean);
+		request.doc(json, XContentType.JSON);
+		try {
+			client.update(request, RequestOptions.DEFAULT);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+
+	/**
+	 *鍒犻櫎
+	 * 
+	 * @param Document id
+	 */
+	public void delete(String id, Class<T> entityClass) {
+		String document = getDocument(entityClass);
+		DeleteRequest request = new DeleteRequest(document, id);
+		try {
+			client.delete(request, RequestOptions.DEFAULT);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * 鏌ヨ
+	 * 
+	 * @param Document id
+	 * @return
+	 */
+	public T get(String id, Class<T> entityClass) {
+		String document = getDocument(entityClass);
+		GetRequest request = new GetRequest(document, id);
+		try {
+			GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
+			if (getResponse.isExists()) {
+			    String content = getResponse.getSourceAsString();        
+			    return new Gson().fromJson(content, entityClass);
+			} 
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	/**
+	 * 鏌ヨ
+	 * @param document 鍒楀悕
+	 * @param key 鍖归厤璇�-灏嗗垎寮�鍗曚釜瀛�
+	 * @param start
+	 * @param count
+	 * @param entityClass
+	 * @return
+	 */
+	public List<T> query(String document, String key, int start, int count, Class<T> entityClass) {
+		String index = getDocument(entityClass);
+		List<T> list = new ArrayList<>();
+		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
+		sourceBuilder.query(QueryBuilders.termQuery(document, key));
+		sourceBuilder.from(start); // 绱㈠紩浠ュ紑濮嬫悳绱㈢殑閫夐」銆傞璁句负0銆�
+		sourceBuilder.size(count); // 杩斿洖鐨勬悳绱㈠懡涓鏁扮殑閫夐」銆傞粯璁や负10
+		sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 璁剧疆涓�涓彲閫夌殑瓒呮椂鏃堕棿锛屼互鎺у埗鍏佽鎼滅储鐨勬椂闂淬��
+
+		SearchRequest searchRequest = new SearchRequest();
+		searchRequest.indices(index);
+		searchRequest.source(sourceBuilder);
+		try {
+			SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
+			SearchHits hits = searchResponse.getHits();
+			SearchHit[] searchHits = hits.getHits();
+			
+			Gson gson = new Gson();
+			for (SearchHit hit : searchHits) {
+				String content = hit.getSourceAsString();
+				list.add(gson.fromJson(content, entityClass));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return list;
+	}
+	
+}

--
Gitblit v1.8.0