| | |
| | | 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;
|
| | |
| | | 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.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
|
| | |
| | | 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());
|
| | | }
|
| | |
| | | */
|
| | | 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();
|
| | | }
|
| | |
| | | request.source(json, XContentType.JSON);
|
| | | // 同步执行
|
| | | try {
|
| | | client.index(request, RequestOptions.DEFAULT);
|
| | | elasticManger.getClient().index(request, RequestOptions.DEFAULT);
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
| | | 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();
|
| | | }
|
| | |
| | | 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();
|
| | | }
|
| | |
| | | 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);
|
| | |
| | | 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();
|
| | |
|