|
|
|
@ -10,14 +10,9 @@ import com.ruoyi.biemo.elasticsearch.entity.FieldMapping;
|
|
|
|
|
import com.ruoyi.biemo.elasticsearch.function.GFunction;
|
|
|
|
|
import com.ruoyi.biemo.mongodb.config.Constant;
|
|
|
|
|
import com.ruoyi.common.exception.CustomException;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
import org.apache.poi.ss.formula.functions.T;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
|
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
|
|
import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
|
@ -26,11 +21,13 @@ import org.elasticsearch.action.search.SearchRequest;
|
|
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
|
|
import org.elasticsearch.action.search.SearchScrollRequest;
|
|
|
|
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
|
|
import org.elasticsearch.client.*;
|
|
|
|
|
import org.elasticsearch.client.Request;
|
|
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
|
|
import org.elasticsearch.client.Response;
|
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
|
|
import org.elasticsearch.client.indices.CreateIndexResponse;
|
|
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
|
|
import org.elasticsearch.common.lucene.search.function.CombineFunction;
|
|
|
|
|
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
|
|
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
|
@ -50,14 +47,14 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
import org.springframework.data.elasticsearch.annotations.Document;
|
|
|
|
|
|
|
|
|
|
import javax.print.Doc;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.lang.reflect.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -89,6 +86,7 @@ public abstract class EsService<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量加载数据
|
|
|
|
|
*
|
|
|
|
|
* @param context
|
|
|
|
|
* @param hitArr
|
|
|
|
|
* @return
|
|
|
|
@ -128,8 +126,10 @@ public abstract class EsService<T> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建mapping
|
|
|
|
|
*
|
|
|
|
|
* @param
|
|
|
|
|
* @param
|
|
|
|
|
* @param clazz 索引类型
|
|
|
|
@ -164,6 +164,7 @@ public abstract class EsService<T> {
|
|
|
|
|
/**
|
|
|
|
|
* 根据信息自动创建索引与mapping
|
|
|
|
|
* 构建mapping描述
|
|
|
|
|
*
|
|
|
|
|
* @param index 索引名称
|
|
|
|
|
* @param
|
|
|
|
|
* @param fieldMappingList 字段信息
|
|
|
|
@ -273,7 +274,6 @@ public abstract class EsService<T> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean indexExist() throws Exception {
|
|
|
|
|
RestHighLevelClient client = null;
|
|
|
|
|
GetIndexRequest request = new GetIndexRequest(index);
|
|
|
|
@ -349,6 +349,7 @@ public abstract class EsService<T> {
|
|
|
|
|
ElasticSearchPoolUtil.returnClient(client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<T> search(SearchSourceBuilder builder) {
|
|
|
|
|
SearchRequest request = new SearchRequest(index);
|
|
|
|
|
request.source(builder);
|
|
|
|
@ -395,9 +396,11 @@ public abstract class EsService<T> {
|
|
|
|
|
protected List<T> sequenceLoadData(List<SearchHit> content) {
|
|
|
|
|
return content.stream().map(x -> loadData(null, x)).filter(x -> x != null).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<T> esLoadData(List<SearchHit> content) {
|
|
|
|
|
return content.stream().map(x -> JSON.parseObject(x.getSourceAsString(), entity)).filter(x -> x != null).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected long search(List<SearchHit> content, SearchSourceBuilder searchSourceBuilder) {
|
|
|
|
|
// 用于判断 是否 返回所有数据
|
|
|
|
|
boolean flag = true;
|
|
|
|
@ -441,7 +444,9 @@ public abstract class EsService<T> {
|
|
|
|
|
SearchHits hits = searchResponse.getHits();
|
|
|
|
|
System.out.println(index);
|
|
|
|
|
SearchHit[] hits1 = hits.getHits();
|
|
|
|
|
if (hits1[0] != null) {
|
|
|
|
|
System.out.println(hits1[0].getId());
|
|
|
|
|
}
|
|
|
|
|
total = hits.getTotalHits().value;
|
|
|
|
|
// 不分页情况下 总条数超过10000
|
|
|
|
|
if (total > MAX_RESULT_WINDOW && flag) {
|
|
|
|
@ -462,6 +467,7 @@ public abstract class EsService<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过响应加载hit数据
|
|
|
|
|
*
|
|
|
|
|
* @param searchHitList
|
|
|
|
|
* @param searchResponse
|
|
|
|
|
* @return
|
|
|
|
@ -489,6 +495,7 @@ public abstract class EsService<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* scroll 查询 返回总条数
|
|
|
|
|
*
|
|
|
|
|
* @param searchHitList
|
|
|
|
|
* @param firstRowNum
|
|
|
|
|
* @param lastRowNum
|
|
|
|
@ -569,6 +576,7 @@ public abstract class EsService<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 滚动搜索
|
|
|
|
|
*
|
|
|
|
|
* @param scrollId 滚动Id
|
|
|
|
|
* @return
|
|
|
|
|
* @throws IOException
|
|
|
|
@ -587,6 +595,7 @@ public abstract class EsService<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 循环批量加载数据
|
|
|
|
|
*
|
|
|
|
|
* @param context
|
|
|
|
|
* @param startPosition
|
|
|
|
|
* @param endPosition
|
|
|
|
@ -600,6 +609,7 @@ public abstract class EsService<T> {
|
|
|
|
|
System.arraycopy(hits.getHits(), startPosition, hitArr, 0, hitArr.length);
|
|
|
|
|
return batchLoadData(context, hitArr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<SearchHit> loopLoadDataByPosition(List<SearchHit> searchHitList, int startPosition, int endPosition, SearchHits hits) {
|
|
|
|
|
int length = hits.getHits().length;
|
|
|
|
|
int destLength = endPosition - startPosition;
|
|
|
|
@ -709,6 +719,7 @@ public abstract class EsService<T> {
|
|
|
|
|
.boostMode(CombineFunction.REPLACE)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EsQueryBuilder notEq(GFunction<? extends T, Object> gFunction, Object value) {
|
|
|
|
|
String field = gFunction.field();
|
|
|
|
|
this.searchSourceBuilder.postFilter(QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(field, value)));
|
|
|
|
|