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