|
|
@@ -1,495 +0,0 @@
|
|
|
-package com.sckw.elasticsearch.service.es;
|
|
|
-
|
|
|
-import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
-import co.elastic.clients.elasticsearch._types.ElasticsearchException;
|
|
|
-import co.elastic.clients.elasticsearch._types.FieldValue;
|
|
|
-import co.elastic.clients.elasticsearch._types.SortOrder;
|
|
|
-import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
|
|
-import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation;
|
|
|
-import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
|
|
-import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
|
|
-import co.elastic.clients.elasticsearch.core.*;
|
|
|
-import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
|
|
-import co.elastic.clients.elasticsearch.core.search.Hit;
|
|
|
-import co.elastic.clients.elasticsearch.indices.IndexSettings;
|
|
|
-import co.elastic.clients.json.JsonData;
|
|
|
-import co.elastic.clients.util.ObjectBuilder;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.function.Function;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author yuzhongchuan
|
|
|
- * @description COMMENT='ES相关工具类'
|
|
|
- * @create 2023 -06 -06 10:01
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-public class EsUtils {
|
|
|
-
|
|
|
- private ElasticsearchClient client;
|
|
|
-
|
|
|
- private static EsUtils esUtils;
|
|
|
-
|
|
|
- public EsUtils(ElasticsearchClient client) {
|
|
|
- esUtils = this;
|
|
|
- this.client = client;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param name
|
|
|
- * @return void
|
|
|
- * @desc: 创建索引
|
|
|
- * @author: yzc
|
|
|
- * @date: 2023-06-12 11:46
|
|
|
- */
|
|
|
- public static void addIndex(String name) {
|
|
|
- try {
|
|
|
- if (indexExists(name)) {
|
|
|
- log.error(" idxName={} 已经存在", name);
|
|
|
- return;
|
|
|
- }
|
|
|
- esUtils.client.indices().create(c -> c.index(name));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES addIndex异常", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param name
|
|
|
- * @return boolean
|
|
|
- * @desc: 索引是否存在
|
|
|
- * @author: yzc
|
|
|
- * @date: 2023-06-12 11:46
|
|
|
- */
|
|
|
- public static boolean indexExists(String name) throws IOException {
|
|
|
- return esUtils.client.indices().exists(b -> b.index(name)).value();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param name
|
|
|
- * @return void
|
|
|
- * @desc: 删除索引
|
|
|
- * @author: yzc
|
|
|
- * @date: 2023-06-12 11:47
|
|
|
- */
|
|
|
- public static void delIndex(String name) {
|
|
|
- try {
|
|
|
- if (!indexExists(name)) {
|
|
|
- log.error(" idxName={} 不存在", name);
|
|
|
- return;
|
|
|
- }
|
|
|
- esUtils.client.indices().delete(c -> c.index(name));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES delIndex异常", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param idxName, settingFn, mappingFn
|
|
|
- * @return void
|
|
|
- * @desc: 带mapping、setting创建索引
|
|
|
- * @author: yzc
|
|
|
- * @date: 2023-06-12 11:47
|
|
|
- */
|
|
|
- public static void createIndex(String idxName,
|
|
|
- Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> settingFn,
|
|
|
- Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mappingFn) {
|
|
|
- try {
|
|
|
- if (indexExists(idxName)) {
|
|
|
- log.error(" idxName={} 已经存在", idxName);
|
|
|
- return;
|
|
|
- }
|
|
|
- esUtils.client.indices().create(c -> c
|
|
|
- .index(idxName)
|
|
|
- .settings(settingFn)
|
|
|
- .mappings(mappingFn));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES createIndex异常", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加文档信息
|
|
|
- *
|
|
|
- * @param idxName
|
|
|
- * @param t
|
|
|
- * @param <T>
|
|
|
- */
|
|
|
- public static <T> void add(String idxName, T t) {
|
|
|
- if (Objects.isNull(idxName) || t == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- esUtils.client.index(s ->
|
|
|
- s.index(idxName).document(t));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es插入单条新数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加文档信息 指定id
|
|
|
- *
|
|
|
- * @param idxName
|
|
|
- * @param id
|
|
|
- */
|
|
|
- public static <T> void addSpecifyId(String idxName, String id, T t) {
|
|
|
- if (Objects.isNull(idxName) || t == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- esUtils.client.index(s ->
|
|
|
- s.index(idxName).id(id).document(t));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es插入指定id单条新数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修改文档自定义属性
|
|
|
- *
|
|
|
- * @param idxName
|
|
|
- * @param id
|
|
|
- * @param t
|
|
|
- */
|
|
|
- public static <T> void update(String idxName, String id, T t) {
|
|
|
- if (Objects.isNull(idxName) || t == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- esUtils.client.update(x -> x.index(idxName).id(id).doc(t), Object.class);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es修改文档数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量新增
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param list
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> void bulkSave(String indexName, List<T> list) {
|
|
|
- if (Objects.isNull(indexName) || CollectionUtils.isEmpty(list)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- BulkRequest.Builder br = new BulkRequest.Builder();
|
|
|
- //可不指定id es会自动生成id
|
|
|
- list.forEach(e -> br.operations(op -> op
|
|
|
- .index(idx -> idx.index(indexName).document(e))));
|
|
|
- try {
|
|
|
- esUtils.client.bulk(br.build());
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es批量新增数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量新增 指定id
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param list
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> void bulkSaveSpecifyId(String indexName, List<T> list) {
|
|
|
- if (Objects.isNull(indexName) || CollectionUtils.isEmpty(list)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- BulkRequest.Builder br = new BulkRequest.Builder();
|
|
|
- //对象id为es id
|
|
|
- list.forEach(e -> br.operations(op -> op
|
|
|
- .index(idx -> idx.index(indexName).id(JSON.parseObject((String) e).get("id").toString()).document(e))));
|
|
|
- try {
|
|
|
- esUtils.client.bulk(br.build());
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es批量新增指定id数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * ids批量删除
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param list
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static <T> void bulkDel(String indexName, List<T> list) {
|
|
|
- if (CollectionUtils.isEmpty(list)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- List<BulkOperation> bulkOperations = new ArrayList<>();
|
|
|
- list.forEach(a ->
|
|
|
- bulkOperations.add(BulkOperation.of(b ->
|
|
|
- b.delete(c -> c.id(JSON.parseObject((String) a).get("id").toString()))
|
|
|
- )));
|
|
|
- try {
|
|
|
- esUtils.client.bulk(a -> a.index(indexName).operations(bulkOperations));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es批量ids删除数据异常:", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * term匹配 多次匹配
|
|
|
- *
|
|
|
- * @param index
|
|
|
- * @param field
|
|
|
- * @param fieldValues
|
|
|
- * @return Object
|
|
|
- */
|
|
|
- public static List<Hit<Object>> advancedQueryByTerm(String index, String field, List<FieldValue> fieldValues) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> response = esUtils.client.search(e -> e
|
|
|
- .index(index)
|
|
|
- .query(q -> q.terms(t -> t.field(field).terms(terms -> terms.value(fieldValues))))
|
|
|
- .query(q -> q.matchAll(m -> m)), Object.class);
|
|
|
- return response.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es term匹配多次匹配查询异常:", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * term匹配 单次匹配
|
|
|
- *
|
|
|
- * @param index
|
|
|
- * @param field
|
|
|
- * @param value
|
|
|
- * @return Object
|
|
|
- */
|
|
|
- public static List<Hit<Object>> advancedQueryByTerm(String index, String field, long value) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> response = esUtils.client.search(e -> e
|
|
|
- .index(index)
|
|
|
- .query(q -> q.term(t -> t.field(field).value(value)))
|
|
|
- .query(q -> q.matchAll(m -> m)), Object.class);
|
|
|
- return response.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es term匹配单次匹配查询异常:", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 分页查询
|
|
|
- *
|
|
|
- * @param index
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<Hit<Object>> advancedQueryByPage(String index, Integer from, Integer size) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> response = esUtils.client.search(e -> e
|
|
|
- .index(index)
|
|
|
- .query(q -> q.matchAll(m -> m))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return response.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("es 分页查询异常:", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * multiMatch多field匹配
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param fields
|
|
|
- * @param query
|
|
|
- * @param sortField
|
|
|
- * @param order
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryMultiMatch(String indexName, List<String> fields, String query, String sortField, SortOrder order, Integer from, Integer size) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(e -> e
|
|
|
- .index(indexName)
|
|
|
- .query(q -> q.multiMatch(m -> m.fields(fields).query(query)))
|
|
|
- .sort(sort -> sort.field(f -> f.field(sortField).order(order)))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryMultiMatch查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * Range范围匹配
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param field
|
|
|
- * @param gte 大于等于
|
|
|
- * @param lte 小于等于
|
|
|
- * @param sortField
|
|
|
- * @param order
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryBoolRange(String indexName, String field, Object gte, Object lte, String sortField, SortOrder order, Integer from, Integer size) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(e -> e
|
|
|
- .index(indexName)
|
|
|
- .query(q -> q.bool(b -> b.filter(f -> f.range(r -> r.field(field)
|
|
|
- .gte((Objects.isNull(gte) ? null : JsonData.of(gte)))
|
|
|
- .lte((Objects.isNull(lte) ? null : JsonData.of(lte)))))))
|
|
|
- .sort(sort -> sort.field(f -> f.field(sortField).order(order)))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException | ElasticsearchException e) {
|
|
|
- log.error("ES queryBoolRange查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * filter过滤
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param excludes
|
|
|
- * @param includes
|
|
|
- * @param sortField
|
|
|
- * @param order
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryFilter(String indexName, List<String> excludes, List<String> includes, String sortField, SortOrder order, Integer from, Integer size) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(e -> e
|
|
|
- .index(indexName)
|
|
|
- .query(q -> q.matchAll(m -> m))
|
|
|
- .source(s -> s.filter(f -> f.excludes(excludes).includes(includes)))
|
|
|
- .sort(sort -> sort.field(f -> f.field(sortField).order(order)))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryFilter查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * fuzzy模糊匹配
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param field
|
|
|
- * @param value
|
|
|
- * @param fuzziness
|
|
|
- * @param sortField
|
|
|
- * @param order
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryFuzzy(String indexName, String field, String value, String fuzziness, String sortField, SortOrder order, Integer from, Integer size) {
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(e -> e
|
|
|
- .index(indexName)
|
|
|
- .query(q -> q.fuzzy(f -> f.field(field).value(value).fuzziness(fuzziness)))
|
|
|
- .sort(sort -> sort.field(f -> f.field(sortField).order(order)))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryFuzzy查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param indexName, field
|
|
|
- * @return co.elastic.clients.elasticsearch._types.aggregations.Aggregate
|
|
|
- * @desc: 分组聚合查询
|
|
|
- * @author: yzc
|
|
|
- * @date: 2023-06-12 17:36
|
|
|
- */
|
|
|
- public static Aggregate queryGroupAggregation(String indexName, String field) {
|
|
|
- try {
|
|
|
- SearchResponse<Void> searchResponse = esUtils.client.search(
|
|
|
- b -> b.index(indexName).size(0)
|
|
|
- .aggregations(field, a -> a.terms(TermsAggregation.of(s -> s.field(field)))),
|
|
|
- Void.class);
|
|
|
- return searchResponse.aggregations().get(field);
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryGroupAggregation查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 简单聚合查询
|
|
|
- *
|
|
|
- * @param countRequest
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static long count(CountRequest countRequest) {
|
|
|
- try {
|
|
|
- log.info("开始ES查询indexName:{}的count", Arrays.toString(countRequest.index().toArray()));
|
|
|
- CountResponse count = esUtils.client.count(countRequest);
|
|
|
- return count.count();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES count异常", e);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * SearchRequest原生查询
|
|
|
- *
|
|
|
- * @param searchRequest
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryOriginal(SearchRequest searchRequest) {
|
|
|
- //Query of = Query.of(q -> q.bool(b -> b.must(m -> m.match(match -> match.field("").query("")))));
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(searchRequest, Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryOriginal查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Query原生查询
|
|
|
- *
|
|
|
- * @param indexName
|
|
|
- * @param field
|
|
|
- * @param query
|
|
|
- * @param order
|
|
|
- * @param from
|
|
|
- * @param size
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static List<Hit<Object>> queryOriginal(String indexName, String field, Query query, SortOrder order, Integer from, Integer size) {
|
|
|
- //Query of = Query.of(q -> q.bool(b -> b.must(m -> m.match(match -> match.field("").query("")))));
|
|
|
- try {
|
|
|
- SearchResponse<Object> searchResponse = esUtils.client.search(e -> e
|
|
|
- .index(indexName)
|
|
|
- .query(query)
|
|
|
- .sort(sort -> sort.field(f -> f.field(field).order(order)))
|
|
|
- .from(from).size(size), Object.class);
|
|
|
- return searchResponse.hits().hits();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("ES queryOriginal查询异常", e);
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|