deploy:加入es是否启用开关
This commit is contained in:
parent
36f168fa2d
commit
2be78755f4
@ -43,7 +43,11 @@ import java.util.List;
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EsUtil {
|
||||
|
||||
/**
|
||||
* 是否启用es
|
||||
*/
|
||||
@Value("${es.status}")
|
||||
public boolean status;
|
||||
@Value("${es.host}")
|
||||
public String host;
|
||||
@Value("${es.port}")
|
||||
@ -57,6 +61,9 @@ public class EsUtil {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (client != null) {
|
||||
client.close();
|
||||
@ -88,6 +95,9 @@ public class EsUtil {
|
||||
* @date 2019/7/24 14:57
|
||||
*/
|
||||
public boolean indexExist(String index) throws Exception {
|
||||
if (!status) {
|
||||
return false;
|
||||
}
|
||||
GetIndexRequest request = new GetIndexRequest(index);
|
||||
request.local(false);
|
||||
request.humanReadable(true);
|
||||
@ -104,6 +114,9 @@ public class EsUtil {
|
||||
* @date 2019/7/24 15:02
|
||||
*/
|
||||
public void insertOrUpdateOne(String index, EsEntity entity) {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
IndexRequest request = new IndexRequest(index);
|
||||
request.id(entity.getId());
|
||||
request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
|
||||
@ -123,6 +136,9 @@ public class EsUtil {
|
||||
* @date 2019/7/24 17:38
|
||||
*/
|
||||
public <T> void insertBatch(String index, List<EsEntity<T>> list) {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
BulkRequest request = new BulkRequest();
|
||||
list.forEach(item -> request.add(new IndexRequest(index).id(item.getId())
|
||||
.source(JSON.toJSONString(item.getData()), XContentType.JSON)));
|
||||
@ -142,6 +158,9 @@ public class EsUtil {
|
||||
* @date 2019/7/25 14:24
|
||||
*/
|
||||
public <T> void deleteBatch(String index, Collection<String> idList) {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
BulkRequest request = new BulkRequest();
|
||||
idList.forEach(item -> request.add(new DeleteRequest(index, item)));
|
||||
try {
|
||||
@ -162,6 +181,9 @@ public class EsUtil {
|
||||
* @date 2019/7/25 13:46
|
||||
*/
|
||||
public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) {
|
||||
if (!status) {
|
||||
return null;
|
||||
}
|
||||
SearchRequest request = new SearchRequest(index);
|
||||
request.source(builder);
|
||||
try {
|
||||
@ -186,6 +208,9 @@ public class EsUtil {
|
||||
* @date 2019/7/26 11:30
|
||||
*/
|
||||
public void deleteIndex(String index) {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
@ -202,6 +227,9 @@ public class EsUtil {
|
||||
* @date 2019/7/26 15:16
|
||||
*/
|
||||
public void deleteByQuery(String index, QueryBuilder builder) {
|
||||
if (!status) {
|
||||
return;
|
||||
}
|
||||
DeleteByQueryRequest request = new DeleteByQueryRequest(index);
|
||||
request.setQuery(builder);
|
||||
//设置批量操作数量,最大为10000
|
||||
@ -219,4 +247,5 @@ public class EsUtil {
|
||||
EsUtil util = new EsUtil();
|
||||
System.out.println(util.indexExist("bookmark"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -62,6 +62,8 @@ mybatis-plus:
|
||||
debug: false
|
||||
|
||||
es:
|
||||
#是否启动es
|
||||
status: false
|
||||
host: localhost
|
||||
port: 9200
|
||||
scheme: http
|
||||
|
Loading…
x
Reference in New Issue
Block a user