项目整合,增加测试项目,es改为rest客户端接口查询
This commit is contained in:
@@ -10,7 +10,7 @@ import java.util.Date;
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-07-07
|
||||
*/
|
||||
@Document(indexName = "zyplayer_doc", indexType = "doc_wiki")
|
||||
@Document(indexName = "zyplayer_doc_wiki", indexType = "_doc")
|
||||
public class EsWikiPage {
|
||||
|
||||
private Long id;
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
package com.zyplayer.doc.data.service.elasticsearch.service;
|
||||
|
||||
import com.zyplayer.doc.data.service.elasticsearch.entity.EsWikiPage;
|
||||
import com.zyplayer.doc.data.service.elasticsearch.support.ElasticSearchConfig;
|
||||
import com.zyplayer.doc.data.service.elasticsearch.support.EsAbstractService;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* wiki文档搜索
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-07-07
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnBean(ElasticSearchConfig.class)
|
||||
public class EsWikiPageService extends EsAbstractService<EsWikiPage> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.zyplayer.doc.data.service.elasticsearch.support;
|
||||
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.TransportAddress;
|
||||
import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 开启es客户端
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-07-07
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "zyplayer.doc.manage.elasticsearch", name = "open", havingValue = "true")
|
||||
public class ElasticSearchConfig {
|
||||
|
||||
@Value(value = "${zyplayer.doc.manage.elasticsearch.host:''}")
|
||||
private String host;
|
||||
@Value("${zyplayer.doc.manage.elasticsearch.port:''}")
|
||||
private String port;
|
||||
@Value("${zyplayer.doc.manage.elasticsearch.cluster-name:''}")
|
||||
private String clusterName;
|
||||
|
||||
@Bean
|
||||
public TransportClient esClient() throws UnknownHostException {
|
||||
Settings settings = Settings.builder()
|
||||
.put("cluster.name", clusterName)
|
||||
.put("client.transport.sniff", true)
|
||||
.build();
|
||||
TransportAddress master = new TransportAddress(InetAddress.getByName(host), Integer.valueOf(port));
|
||||
return new PreBuiltTransportClient(settings).addTransportAddress(master);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.zyplayer.doc.data.service.elasticsearch.support;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 开启es客户端
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-07-07
|
||||
*/
|
||||
@Configuration
|
||||
public class ElasticSearchUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger(ElasticSearchUtil.class);
|
||||
|
||||
@Value(value = "${zyplayer.doc.manage.elasticsearch.hostPort:''}")
|
||||
private String hostAndPort;
|
||||
@Value(value = "${zyplayer.doc.manage.elasticsearch.scheme:''}")
|
||||
private String esScheme;
|
||||
@Value("${zyplayer.doc.manage.elasticsearch.open:''}")
|
||||
private String elasticsearchOpen;
|
||||
|
||||
private static final Object createLock = new Object();
|
||||
private static Map<String, RestHighLevelClient> restClientMap = new ConcurrentHashMap<>();
|
||||
|
||||
public boolean isOpen() {
|
||||
return Objects.equals("true", elasticsearchOpen);
|
||||
}
|
||||
|
||||
public RestHighLevelClient getEsClient() {
|
||||
if (!this.isOpen()) {
|
||||
return null;
|
||||
}
|
||||
String mapKey = esScheme + "_" + hostAndPort;
|
||||
RestHighLevelClient restClient = restClientMap.get(mapKey);
|
||||
if (restClient == null) {
|
||||
synchronized (createLock) {
|
||||
restClient = restClientMap.get(mapKey);
|
||||
if (restClient == null) {
|
||||
try {
|
||||
// rest请求客户端
|
||||
// 例:10.16.32.12:9200,10.16.32.12:9201
|
||||
List<HttpHost> hostPortList = new LinkedList<>();
|
||||
for (String hostPortStr : hostAndPort.split(",")) {
|
||||
String[] splitArr = hostPortStr.split(":");
|
||||
hostPortList.add(new HttpHost(splitArr[0], Integer.valueOf(splitArr[1]), esScheme));
|
||||
}
|
||||
restClient = new RestHighLevelClient(RestClient.builder(hostPortList.toArray(new HttpHost[]{})));
|
||||
restClientMap.put(mapKey, restClient);
|
||||
} catch (Exception e) {
|
||||
logger.error("创建es客户端失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return restClient;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,39 @@
|
||||
package com.zyplayer.doc.data.service.elasticsearch.support;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dozer.Mapper;
|
||||
import org.elasticsearch.action.DocWriteResponse;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.delete.DeleteResponse;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.action.update.UpdateResponse;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* es抽象类
|
||||
@@ -38,7 +44,7 @@ public abstract class EsAbstractService<T> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(EsAbstractService.class);
|
||||
|
||||
@Resource
|
||||
private TransportClient transportClient;
|
||||
private ElasticSearchUtil elasticSearchUtil;
|
||||
@Resource
|
||||
private Mapper mapper;
|
||||
|
||||
@@ -56,37 +62,40 @@ public abstract class EsAbstractService<T> {
|
||||
return annotation.indexType();
|
||||
}
|
||||
|
||||
public boolean create(T table) {
|
||||
String pk = getPrimaryKey(table);
|
||||
IndexResponse indexResponse = this.transportClient
|
||||
.prepareIndex(this.getIndexName(), this.getIndexType())
|
||||
.setId(pk)
|
||||
.setSource(JSONObject.toJSONString(table), XContentType.JSON)
|
||||
.get();
|
||||
logger.debug("ElasticSearch create index with table, pk: {}", pk);
|
||||
return indexResponse.status() == RestStatus.CREATED;
|
||||
public boolean isOpen() {
|
||||
return elasticSearchUtil.isOpen();
|
||||
}
|
||||
|
||||
public boolean update(T table) {
|
||||
public boolean upsert(T table) {
|
||||
String pk = getPrimaryKey(table);
|
||||
UpdateResponse updateResponse = this.transportClient
|
||||
.prepareUpdate(this.getIndexName(), this.getIndexType(), pk)
|
||||
.setDoc(JSONObject.toJSONString(table), XContentType.JSON)
|
||||
.get();
|
||||
logger.info("ElasticSearch update index with table, pk: {}", pk);
|
||||
return updateResponse.status() == RestStatus.OK;
|
||||
UpdateRequest request = new UpdateRequest(this.getIndexName(), pk);
|
||||
request.timeout(TimeValue.timeValueMinutes(2));
|
||||
request.doc(JSON.toJSONString(table), XContentType.JSON);
|
||||
request.docAsUpsert(true);
|
||||
RestHighLevelClient esClient = elasticSearchUtil.getEsClient();
|
||||
try {
|
||||
UpdateResponse updateResponse = esClient.update(request, RequestOptions.DEFAULT);
|
||||
return updateResponse.status() == RestStatus.OK;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void delete(T table) {
|
||||
String pk = getPrimaryKey(table);
|
||||
DeleteResponse response = this.transportClient
|
||||
.prepareDelete(this.getIndexName(), this.getIndexType(), pk)
|
||||
.execute()
|
||||
.actionGet();
|
||||
if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
||||
logger.warn("ElasticSearch delete index id: {} but not found!", pk);
|
||||
} else {
|
||||
logger.warn("ElasticSearch delete index id: {}", pk);
|
||||
RestHighLevelClient esClient = elasticSearchUtil.getEsClient();
|
||||
DeleteRequest request = new DeleteRequest(this.getIndexName(), pk);
|
||||
request.timeout(TimeValue.timeValueMinutes(2));
|
||||
try {
|
||||
DeleteResponse deleteResponse = esClient.delete(request, RequestOptions.DEFAULT);
|
||||
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
||||
logger.warn("ElasticSearch delete index id: {} but not found!", pk);
|
||||
} else {
|
||||
logger.warn("ElasticSearch delete index id: {}", pk);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,16 +159,27 @@ public abstract class EsAbstractService<T> {
|
||||
highlightBuilder.preTags("<span style=\"color:red\">");
|
||||
highlightBuilder.postTags("</span>");
|
||||
highlightBuilder.field("*");
|
||||
SearchRequestBuilder requestBuilder = transportClient.prepareSearch(this.getIndexName()).setTypes(this.getIndexType())
|
||||
.setQuery(queryBuilders)
|
||||
.highlighter(highlightBuilder)
|
||||
.setFrom(startIndex).setSize(pageSize).setExplain(true);
|
||||
// 组装条件
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||
sourceBuilder.query(queryBuilders)
|
||||
.highlighter(highlightBuilder).from(startIndex).size(pageSize)
|
||||
.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
||||
// 查询指定字段
|
||||
if (fields != null && fields.length > 0) {
|
||||
requestBuilder.setFetchSource(fields, new String[]{});
|
||||
sourceBuilder.fetchSource(fields, new String[]{});
|
||||
}
|
||||
SearchResponse response = requestBuilder.execute().actionGet();
|
||||
return responseToList(response);
|
||||
// 组装请求
|
||||
SearchRequest searchRequest = new SearchRequest();
|
||||
searchRequest.indices(this.getIndexName());
|
||||
searchRequest.source(sourceBuilder);
|
||||
RestHighLevelClient esClient = elasticSearchUtil.getEsClient();
|
||||
try {
|
||||
SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
return responseToList(response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public EsPage<T> responseToList(SearchResponse response) {
|
||||
@@ -184,7 +204,7 @@ public abstract class EsAbstractService<T> {
|
||||
tableList.add(table);
|
||||
}
|
||||
EsPage<T> esPage = new EsPage<>();
|
||||
esPage.setTotal(response.getHits().getTotalHits());
|
||||
esPage.setTotal(response.getHits().getTotalHits().value);
|
||||
esPage.setData(tableList);
|
||||
return esPage;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user