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;
|
|
@Repository
|
public abstract class ElasticBaseDao<T> {
|
// 地址
|
private static String host = "192.168.1.200";
|
// 端口
|
private static Integer port = 9200;
|
// 数据库名称
|
public static String index = "order";
|
|
// 初始化api客户端
|
public static RestHighLevelClient client = new RestHighLevelClient(
|
RestClient.builder(new HttpHost(host, port, "http")));
|
|
/**
|
* 插入数据
|
*
|
* @param bean
|
* @return
|
*/
|
public void save(T bean) {
|
String json = JsonUtil.getSimpleGson().toJson(bean);
|
IndexRequest request = new IndexRequest(index);
|
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 json = JsonUtil.getSimpleGson().toJson(bean);
|
IndexRequest request = new IndexRequest(index).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) {
|
UpdateRequest request = new UpdateRequest(index, 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) {
|
DeleteRequest request = new DeleteRequest(index, id);
|
try {
|
client.delete(request, RequestOptions.DEFAULT);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 查询
|
*
|
* @param Document id
|
* @return
|
*/
|
public T get(String id, Class<T> entityClass) {
|
GetRequest request = new GetRequest(index, 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) {
|
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;
|
}
|
|
|
|
|
|
|
}
|