|
@@ -31,6 +31,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@@ -40,6 +41,7 @@ import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
|
|
+@Slf4j
|
|
public class EsQueryServiceImpl implements EsQueryService {
|
|
public class EsQueryServiceImpl implements EsQueryService {
|
|
@Autowired
|
|
@Autowired
|
|
private ElasticsearchClient esClient;
|
|
private ElasticsearchClient esClient;
|
|
@@ -53,19 +55,19 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
private static List<String> pathCache = null;
|
|
private static List<String> pathCache = null;
|
|
private static long cacheTime = 0;
|
|
private static long cacheTime = 0;
|
|
|
|
|
|
- public void getCacheList(){
|
|
|
|
|
|
+ public void getCacheList() {
|
|
synchronized (lockObject) {
|
|
synchronized (lockObject) {
|
|
//从本地缓存加载数据
|
|
//从本地缓存加载数据
|
|
long timeSpan = (new Date()).getTime() - cacheTime;
|
|
long timeSpan = (new Date()).getTime() - cacheTime;
|
|
- if (timeSpan > 30 * 1000 || indexCache == null || pathCache==null) {
|
|
|
|
|
|
+ if (timeSpan > 30 * 1000 || indexCache == null || pathCache == null) {
|
|
List<EsIndexVo> list1 = esIndexCquery.getList(null, null, null);
|
|
List<EsIndexVo> list1 = esIndexCquery.getList(null, null, null);
|
|
indexCache = list1.stream()
|
|
indexCache = list1.stream()
|
|
- .map(m->m.getIndexCode())
|
|
|
|
|
|
+ .map(m -> m.getIndexCode())
|
|
.distinct().collect(Collectors.toList());
|
|
.distinct().collect(Collectors.toList());
|
|
|
|
|
|
- List<EsIndexfieldVo> list2 = esIndexCquery.getFieldList(null,null);
|
|
|
|
- pathCache = list2.stream().filter(p->p.getDataType().equals("NESTED"))
|
|
|
|
- .map(m->m.getFieldCode())
|
|
|
|
|
|
+ List<EsIndexfieldVo> list2 = esIndexCquery.getFieldList(null, null);
|
|
|
|
+ pathCache = list2.stream().filter(p -> p.getDataType().equals("NESTED"))
|
|
|
|
+ .map(m -> m.getFieldCode())
|
|
.distinct().collect(Collectors.toList());
|
|
.distinct().collect(Collectors.toList());
|
|
|
|
|
|
cacheTime = (new Date()).getTime();
|
|
cacheTime = (new Date()).getTime();
|
|
@@ -73,17 +75,17 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public List<String> getIndexCache(){
|
|
|
|
|
|
+ public List<String> getIndexCache() {
|
|
getCacheList();
|
|
getCacheList();
|
|
return indexCache;
|
|
return indexCache;
|
|
}
|
|
}
|
|
|
|
|
|
- public List<String> getPathCache(){
|
|
|
|
|
|
+ public List<String> getPathCache() {
|
|
getCacheList();
|
|
getCacheList();
|
|
return pathCache;
|
|
return pathCache;
|
|
}
|
|
}
|
|
|
|
|
|
- public List<Query> getComparisonQueryList(EsQueryText queryText){
|
|
|
|
|
|
+ public List<Query> getComparisonQueryList(EsQueryText queryText) {
|
|
List<Query> queryList = new ArrayList<>();
|
|
List<Query> queryList = new ArrayList<>();
|
|
//分析查询字符串,有的字符串需要变成条件查询
|
|
//分析查询字符串,有的字符串需要变成条件查询
|
|
List<ComparisonResult> cmpList = analyzeService.analyzeJavas(queryText.getKeyString());
|
|
List<ComparisonResult> cmpList = analyzeService.analyzeJavas(queryText.getKeyString());
|
|
@@ -93,27 +95,27 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
for (int i = 0; i < cmpList.size(); i++) {
|
|
for (int i = 0; i < cmpList.size(); i++) {
|
|
ComparisonResult cmp = cmpList.get(i);
|
|
ComparisonResult cmp = cmpList.get(i);
|
|
//检查查询、对比的值是否为空
|
|
//检查查询、对比的值是否为空
|
|
- if(StringUtils.IsNullEmpty(cmp.getKeyString())) continue;
|
|
|
|
|
|
+ if (StringUtils.IsNullEmpty(cmp.getKeyString())) continue;
|
|
|
|
|
|
if (cmp.getSearchType().equals("comparison")) { //对比查询,有字段,有对比符号,有值
|
|
if (cmp.getSearchType().equals("comparison")) { //对比查询,有字段,有对比符号,有值
|
|
//第一级,对比搜索
|
|
//第一级,对比搜索
|
|
queryList.addAll(getRangeQueryByComparison(cmp));
|
|
queryList.addAll(getRangeQueryByComparison(cmp));
|
|
//第二级,对比搜索
|
|
//第二级,对比搜索
|
|
- for(String path : pathList) {
|
|
|
|
|
|
+ for (String path : pathList) {
|
|
queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
}
|
|
}
|
|
- } else if(StringUtils.IsNullEmpty(queryText.getField())){//全文查询,没有限制字段
|
|
|
|
|
|
+ } else if (StringUtils.IsNullEmpty(queryText.getField())) {//全文查询,没有限制字段
|
|
//第一级,文字搜索
|
|
//第一级,文字搜索
|
|
queryList.add(getMultiMatchQuery(cmp.getKeyString()));
|
|
queryList.add(getMultiMatchQuery(cmp.getKeyString()));
|
|
//第二级,嵌套类型,文字搜索
|
|
//第二级,嵌套类型,文字搜索
|
|
- for(String path : pathList) {
|
|
|
|
|
|
+ for (String path : pathList) {
|
|
queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
|
|
queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
|
|
}
|
|
}
|
|
} else { //全文查询,限定了字段
|
|
} else { //全文查询,限定了字段
|
|
//第一级,文字搜索
|
|
//第一级,文字搜索
|
|
- queryList.add(getMultiMatchQuery(new String[]{queryText.getField()} ,cmp.getKeyString()));
|
|
|
|
|
|
+ queryList.add(getMultiMatchQuery(new String[]{queryText.getField()}, cmp.getKeyString()));
|
|
//第二级,嵌套类型,文字搜索
|
|
//第二级,嵌套类型,文字搜索
|
|
- for(String path : pathList) {
|
|
|
|
|
|
+ for (String path : pathList) {
|
|
queryList.add(getNestedMultiMatchQuery(path, new String[]{queryText.getField()}, cmp.getKeyString()));
|
|
queryList.add(getNestedMultiMatchQuery(path, new String[]{queryText.getField()}, cmp.getKeyString()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -133,26 +135,26 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
|
|
|
List<Query> queryMustList = new ArrayList<>();
|
|
List<Query> queryMustList = new ArrayList<>();
|
|
//[2]分析查询字符串,有的字符串需要变成条件查询
|
|
//[2]分析查询字符串,有的字符串需要变成条件查询
|
|
- for(EsQueryText queryText : queryList){
|
|
|
|
|
|
+ for (EsQueryText queryText : queryList) {
|
|
List<Query> shouldQuerys = getComparisonQueryList(queryText);
|
|
List<Query> shouldQuerys = getComparisonQueryList(queryText);
|
|
- if(shouldQuerys.size()>0){
|
|
|
|
- queryMustList.add(Query.of(q->q.bool(b->b.should(shouldQuerys))));
|
|
|
|
|
|
+ if (shouldQuerys.size() > 0) {
|
|
|
|
+ queryMustList.addAll(shouldQuerys);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//[3]限定查询条件,都是有条件的查询
|
|
//[3]限定查询条件,都是有条件的查询
|
|
- for(ComparisonResult cmp:limiters){
|
|
|
|
|
|
+ for (ComparisonResult cmp : limiters) {
|
|
List<Query> cmpQuerys = new ArrayList<>();
|
|
List<Query> cmpQuerys = new ArrayList<>();
|
|
//检查查询、对比的值是否为空
|
|
//检查查询、对比的值是否为空
|
|
- if(StringUtils.IsNullEmpty(cmp.getValue())) continue;
|
|
|
|
|
|
+ if (StringUtils.IsNullEmpty(cmp.getValue())) continue;
|
|
|
|
|
|
//第一级,对比搜索
|
|
//第一级,对比搜索
|
|
cmpQuerys.addAll(getRangeQueryByComparison(cmp));
|
|
cmpQuerys.addAll(getRangeQueryByComparison(cmp));
|
|
//第二级,对比搜索
|
|
//第二级,对比搜索
|
|
- for(String path : pathList) {
|
|
|
|
|
|
+ for (String path : pathList) {
|
|
cmpQuerys.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
cmpQuerys.addAll(getNestedRangeQueryByComparison(cmp, path));
|
|
}
|
|
}
|
|
- if(cmpQuerys.size()>0){
|
|
|
|
- queryMustList.add(Query.of(q->q.bool(b->b.should(cmpQuerys))));
|
|
|
|
|
|
+ if (cmpQuerys.size() > 0) {
|
|
|
|
+ queryMustList.addAll(cmpQuerys);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -165,96 +167,101 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
searchRequest.from(page * limit);
|
|
searchRequest.from(page * limit);
|
|
//==>设置查询条件
|
|
//==>设置查询条件
|
|
if (queryMustList.size() > 0) {
|
|
if (queryMustList.size() > 0) {
|
|
- searchRequest.query(q -> q.bool(b -> b.must(queryMustList)));
|
|
|
|
|
|
+ Query queryMust = Query.of(q -> q.bool(b -> b.must(queryMustList)));
|
|
|
|
+ //Query byName = MatchQuery.of(m -> m.field("well_common_name").query("宁12-412"))._toQuery();
|
|
|
|
+ //Query queryMust = Query.of(q -> q.term(TermQuery.of(m -> m.field("well_common_name.keyword").value("宁12-412"))));
|
|
|
|
+ searchRequest.query(queryMust);
|
|
}
|
|
}
|
|
//==>高亮设置
|
|
//==>高亮设置
|
|
- searchRequest.highlight(h->h.fields("*", f->f.matchedFields("*")));
|
|
|
|
|
|
+ searchRequest.highlight(h -> h.fields("*", f -> f.preTags("<font color='red'>")
|
|
|
|
+ .postTags("</font>")));
|
|
//排序
|
|
//排序
|
|
//searchRequest.sort(f->f.field(v->v.field("well_id").order(SortOrder.Asc)));
|
|
//searchRequest.sort(f->f.field(v->v.field("well_id").order(SortOrder.Asc)));
|
|
//查询后分组
|
|
//查询后分组
|
|
- searchRequest.aggregations("group_well",a->a.terms(t->t.field("well_id.keyword")));
|
|
|
|
|
|
+ searchRequest.aggregations("group_well", a -> a.terms(t -> t.field("well_id.keyword")));
|
|
//[5]Es发起查询
|
|
//[5]Es发起查询
|
|
SearchRequest request = searchRequest.build();
|
|
SearchRequest request = searchRequest.build();
|
|
|
|
+
|
|
|
|
+ log.info("dsl:" + request.toString());
|
|
SearchResponse<ObjectNode> response = esClient.search(request, ObjectNode.class);
|
|
SearchResponse<ObjectNode> response = esClient.search(request, ObjectNode.class);
|
|
|
|
|
|
//[6]转换结果,可以对不同的index做出参数输出
|
|
//[6]转换结果,可以对不同的index做出参数输出
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
result.put("rows", rows);
|
|
result.put("rows", rows);
|
|
result.put("total", response.hits().total().value());
|
|
result.put("total", response.hits().total().value());
|
|
- result.put("agg",aggResponse2List(response));
|
|
|
|
|
|
+ result.put("agg", aggResponse2List(response));
|
|
|
|
|
|
//请求参数输出,方便调试
|
|
//请求参数输出,方便调试
|
|
String[] jsonStrings = request.toString().split("typed_keys=true");
|
|
String[] jsonStrings = request.toString().split("typed_keys=true");
|
|
result.put("SearchUrl", jsonStrings[0]);
|
|
result.put("SearchUrl", jsonStrings[0]);
|
|
result.put("SearchRequest", stringToNodeJson(jsonStrings[1]));
|
|
result.put("SearchRequest", stringToNodeJson(jsonStrings[1]));
|
|
System.out.println(response.hits().total() + " " + request.toString());
|
|
System.out.println(response.hits().total() + " " + request.toString());
|
|
- }
|
|
|
|
- catch (Exception ex){
|
|
|
|
|
|
+ } catch (Exception ex) {
|
|
result.put("Message", ex.getMessage());
|
|
result.put("Message", ex.getMessage());
|
|
result.put("StackTrace", ex.getStackTrace());
|
|
result.put("StackTrace", ex.getStackTrace());
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getMultiMatchQuery(String text){
|
|
|
|
|
|
+ public Query getMultiMatchQuery(String text) {
|
|
//不指定,查询所有的一级字段
|
|
//不指定,查询所有的一级字段
|
|
return MultiMatchQuery.of(q -> q.query(text)
|
|
return MultiMatchQuery.of(q -> q.query(text)
|
|
.operator(Operator.Or))._toQuery();
|
|
.operator(Operator.Or))._toQuery();
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getMultiMatchQuery(List<String> fields, String text){
|
|
|
|
|
|
+ public Query getMultiMatchQuery(List<String> fields, String text) {
|
|
//限定查询字段
|
|
//限定查询字段
|
|
return MultiMatchQuery.of(q -> q.fields(fields)
|
|
return MultiMatchQuery.of(q -> q.fields(fields)
|
|
.query(text)
|
|
.query(text)
|
|
.operator(Operator.Or))._toQuery();
|
|
.operator(Operator.Or))._toQuery();
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getMultiMatchQuery(String[] fields, String text){
|
|
|
|
|
|
+ public Query getMultiMatchQuery(String[] fields, String text) {
|
|
//限定查询字段
|
|
//限定查询字段
|
|
return getMultiMatchQuery(Arrays.asList(fields), text);
|
|
return getMultiMatchQuery(Arrays.asList(fields), text);
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getNestedMultiMatchQuery(String path, String text){
|
|
|
|
|
|
+ public Query getNestedMultiMatchQuery(String path, String text) {
|
|
//不指定,查询所有的二级字段
|
|
//不指定,查询所有的二级字段
|
|
- return NestedQuery.of(q-> q.path(path)
|
|
|
|
|
|
+ return NestedQuery.of(q -> q.path(path)
|
|
.query(getMultiMatchQuery(text))
|
|
.query(getMultiMatchQuery(text))
|
|
.ignoreUnmapped(true))._toQuery();
|
|
.ignoreUnmapped(true))._toQuery();
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getNestedMultiMatchQuery(String path, List<String> fields, String text){
|
|
|
|
|
|
+ public Query getNestedMultiMatchQuery(String path, List<String> fields, String text) {
|
|
//限定查询字段
|
|
//限定查询字段
|
|
- return NestedQuery.of(q-> q.path(path)
|
|
|
|
|
|
+ return NestedQuery.of(q -> q.path(path)
|
|
.query(getMultiMatchQuery(fields, text))
|
|
.query(getMultiMatchQuery(fields, text))
|
|
.ignoreUnmapped(true))._toQuery();
|
|
.ignoreUnmapped(true))._toQuery();
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getNestedMultiMatchQuery(String path, String[] fields, String text){
|
|
|
|
|
|
+ public Query getNestedMultiMatchQuery(String path, String[] fields, String text) {
|
|
//限定查询字段
|
|
//限定查询字段
|
|
- return NestedQuery.of(q-> q.path(path)
|
|
|
|
|
|
+ return NestedQuery.of(q -> q.path(path)
|
|
.query(getMultiMatchQuery(fields, text))
|
|
.query(getMultiMatchQuery(fields, text))
|
|
.ignoreUnmapped(true))._toQuery();
|
|
.ignoreUnmapped(true))._toQuery();
|
|
}
|
|
}
|
|
|
|
|
|
- public List<Query> getRangeQueryByComparison(ComparisonResult cmp){
|
|
|
|
|
|
+ public List<Query> getRangeQueryByComparison(ComparisonResult cmp) {
|
|
List<Query> queryList = new ArrayList<>();
|
|
List<Query> queryList = new ArrayList<>();
|
|
- for(int i=0;i<cmp.getFields().length;i++){
|
|
|
|
|
|
+ for (int i = 0; i < cmp.getFields().length; i++) {
|
|
String fieldString = cmp.getFields()[i];
|
|
String fieldString = cmp.getFields()[i];
|
|
//对比类型查询
|
|
//对比类型查询
|
|
Query query = getComparisonQuery(fieldString, cmp.getOpreation(), cmp.getValue());
|
|
Query query = getComparisonQuery(fieldString, cmp.getOpreation(), cmp.getValue());
|
|
- if(query!=null) queryList.add(query);
|
|
|
|
|
|
+ if (query != null) queryList.add(query);
|
|
}
|
|
}
|
|
|
|
|
|
return queryList;
|
|
return queryList;
|
|
}
|
|
}
|
|
|
|
|
|
- public List<Query> getNestedRangeQueryByComparison(ComparisonResult cmp, String path){
|
|
|
|
|
|
+ public List<Query> getNestedRangeQueryByComparison(ComparisonResult cmp, String path) {
|
|
List<Query> queryList = new ArrayList<>();
|
|
List<Query> queryList = new ArrayList<>();
|
|
- for(int i=0;i<cmp.getFields().length;i++){
|
|
|
|
|
|
+ for (int i = 0; i < cmp.getFields().length; i++) {
|
|
String fieldString = cmp.getFields()[i];
|
|
String fieldString = cmp.getFields()[i];
|
|
//对比类型查询
|
|
//对比类型查询
|
|
Query query = getComparisonQuery(path + "." + fieldString, cmp.getOpreation(), cmp.getValue());
|
|
Query query = getComparisonQuery(path + "." + fieldString, cmp.getOpreation(), cmp.getValue());
|
|
- if(query!=null) {
|
|
|
|
- Query nested = NestedQuery.of(q-> q.path(path).query(query)
|
|
|
|
|
|
+ if (query != null) {
|
|
|
|
+ Query nested = NestedQuery.of(q -> q.path(path).query(query)
|
|
.ignoreUnmapped(true))._toQuery();
|
|
.ignoreUnmapped(true))._toQuery();
|
|
queryList.add(nested);
|
|
queryList.add(nested);
|
|
}
|
|
}
|
|
@@ -263,23 +270,23 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
return queryList;
|
|
return queryList;
|
|
}
|
|
}
|
|
|
|
|
|
- public Query getComparisonQuery(String fieldString, String opreation, String value){
|
|
|
|
|
|
+ public Query getComparisonQuery(String fieldString, String opreation, String value) {
|
|
//对比类型查询
|
|
//对比类型查询
|
|
Query query = null;
|
|
Query query = null;
|
|
- if(opreation.equals("大于"))
|
|
|
|
- query = RangeQuery.of(q->q.field(fieldString).gt(JsonData.of(value)))._toQuery();
|
|
|
|
- else if(opreation.equals("大于等于"))
|
|
|
|
- query = RangeQuery.of(q->q.field(fieldString).gte(JsonData.of(value)))._toQuery();
|
|
|
|
- else if(opreation.equals("小于"))
|
|
|
|
- query = RangeQuery.of(q->q.field(fieldString).lt(JsonData.of(value)))._toQuery();
|
|
|
|
- else if(opreation.equals("小于等于"))
|
|
|
|
- query = RangeQuery.of(q->q.field(fieldString).lte(JsonData.of(value)))._toQuery();
|
|
|
|
- else if(opreation.equals("等于"))
|
|
|
|
- query = TermQuery.of(q->q.field(fieldString).value(value))._toQuery();
|
|
|
|
- else if(opreation.equals("包括IN")) {
|
|
|
|
|
|
+ if (opreation.equals("大于"))
|
|
|
|
+ query = RangeQuery.of(q -> q.field(fieldString).gt(JsonData.of(value)))._toQuery();
|
|
|
|
+ else if (opreation.equals("大于等于"))
|
|
|
|
+ query = RangeQuery.of(q -> q.field(fieldString).gte(JsonData.of(value)))._toQuery();
|
|
|
|
+ else if (opreation.equals("小于"))
|
|
|
|
+ query = RangeQuery.of(q -> q.field(fieldString).lt(JsonData.of(value)))._toQuery();
|
|
|
|
+ else if (opreation.equals("小于等于"))
|
|
|
|
+ query = RangeQuery.of(q -> q.field(fieldString).lte(JsonData.of(value)))._toQuery();
|
|
|
|
+ else if (opreation.equals("等于"))
|
|
|
|
+ query = TermQuery.of(q -> q.field(fieldString).value(value))._toQuery();
|
|
|
|
+ else if (opreation.equals("包括IN")) {
|
|
String[] ins = value.split(",");
|
|
String[] ins = value.split(",");
|
|
List<FieldValue> fls = new ArrayList<>();
|
|
List<FieldValue> fls = new ArrayList<>();
|
|
- for(String inString : ins) fls.add(FieldValue.of(inString));
|
|
|
|
|
|
+ for (String inString : ins) fls.add(FieldValue.of(inString));
|
|
query = TermsQuery.of(q -> q.field(fieldString).terms(s -> s.value(fls)))._toQuery();
|
|
query = TermsQuery.of(q -> q.field(fieldString).terms(s -> s.value(fls)))._toQuery();
|
|
}
|
|
}
|
|
return query;
|
|
return query;
|
|
@@ -305,21 +312,21 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
searchRequest.from(page * limit);
|
|
searchRequest.from(page * limit);
|
|
|
|
|
|
//生成查询参数
|
|
//生成查询参数
|
|
- if(!StringUtils.IsNullEmpty(text)) {
|
|
|
|
|
|
+ if (!StringUtils.IsNullEmpty(text)) {
|
|
//非嵌套字段查询
|
|
//非嵌套字段查询
|
|
- String[] fields = new String[]{"well_common_name","testing_name"};
|
|
|
|
|
|
+ String[] fields = new String[]{"well_common_name", "testing_name"};
|
|
Query query1 = MultiMatchQuery.of(q -> q.fields(Arrays.asList(fields)).query(text).operator(Operator.Or))._toQuery();
|
|
Query query1 = MultiMatchQuery.of(q -> q.fields(Arrays.asList(fields)).query(text).operator(Operator.Or))._toQuery();
|
|
//嵌套字段查询
|
|
//嵌套字段查询
|
|
String[] nestedFields = new String[]{"historys.testing_name"};
|
|
String[] nestedFields = new String[]{"historys.testing_name"};
|
|
Query nestedQuery = MultiMatchQuery.of(q -> q.fields(Arrays.asList(nestedFields)).query(text).operator(Operator.Or))._toQuery();
|
|
Query nestedQuery = MultiMatchQuery.of(q -> q.fields(Arrays.asList(nestedFields)).query(text).operator(Operator.Or))._toQuery();
|
|
- Query query3 = NestedQuery.of(q-> q.path("historys").query(nestedQuery).ignoreUnmapped(true))._toQuery();
|
|
|
|
|
|
+ Query query3 = NestedQuery.of(q -> q.path("historys").query(nestedQuery).ignoreUnmapped(true))._toQuery();
|
|
//对比类型查询
|
|
//对比类型查询
|
|
- Query query2 = RangeQuery.of(q->q.field("authorized_md").gte(JsonData.of(500)))._toQuery();
|
|
|
|
|
|
+ Query query2 = RangeQuery.of(q -> q.field("authorized_md").gte(JsonData.of(500)))._toQuery();
|
|
|
|
|
|
Query[] arys = new Query[]{query1, query2, query3};
|
|
Query[] arys = new Query[]{query1, query2, query3};
|
|
- searchRequest.query(q->q.bool(b->b.should(Arrays.asList(arys))));
|
|
|
|
|
|
+ searchRequest.query(q -> q.bool(b -> b.should(Arrays.asList(arys))));
|
|
//高亮设置
|
|
//高亮设置
|
|
- searchRequest.highlight(h->h.fields("historys.testing_name", f->f.matchedFields("historys.testing_name")));
|
|
|
|
|
|
+ searchRequest.highlight(h -> h.fields("historys.testing_name", f -> f.matchedFields("historys.testing_name")));
|
|
}
|
|
}
|
|
|
|
|
|
//Es发起查询
|
|
//Es发起查询
|
|
@@ -330,9 +337,8 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
result.put("rows", rows);
|
|
result.put("rows", rows);
|
|
result.put("total", response.hits().total().value());
|
|
result.put("total", response.hits().total().value());
|
|
- System.out.println(response.hits().total()+" "+ request.toString());
|
|
|
|
- }
|
|
|
|
- catch (Exception ex){
|
|
|
|
|
|
+ System.out.println(response.hits().total() + " " + request.toString());
|
|
|
|
+ } catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
@@ -340,7 +346,7 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
|
|
|
public void updateMappingTest() throws IOException {
|
|
public void updateMappingTest() throws IOException {
|
|
Map<String, Property> maps = new HashMap<>();
|
|
Map<String, Property> maps = new HashMap<>();
|
|
- maps.put("well_common_name", Property.of(p->p.text(TextProperty.of(t->t.index(true).analyzer("ik_max_word")))));
|
|
|
|
|
|
+ maps.put("well_common_name", Property.of(p -> p.text(TextProperty.of(t -> t.index(true).analyzer("ik_max_word")))));
|
|
updateMappings("fact_dwr_well_basic_information", maps);
|
|
updateMappings("fact_dwr_well_basic_information", maps);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -356,9 +362,9 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
JsonNode rootNode = objectMapper.readTree(jsonString);
|
|
JsonNode rootNode = objectMapper.readTree(jsonString);
|
|
return rootNode;
|
|
return rootNode;
|
|
}
|
|
}
|
|
- public List<Map<String, Object>> aggResponse2List(SearchResponse<ObjectNode> searchResponse)
|
|
|
|
- {
|
|
|
|
- List<StringTermsBucket> buckets =searchResponse.aggregations().get("group_well").sterms().buckets().array();
|
|
|
|
|
|
+
|
|
|
|
+ public List<Map<String, Object>> aggResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
|
|
+ List<StringTermsBucket> buckets = searchResponse.aggregations().get("group_well").sterms().buckets().array();
|
|
List<Map<String, Object>> aggs = new ArrayList<>();
|
|
List<Map<String, Object>> aggs = new ArrayList<>();
|
|
for (StringTermsBucket bucket : buckets) {
|
|
for (StringTermsBucket bucket : buckets) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
@@ -368,10 +374,15 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
}
|
|
}
|
|
return aggs;
|
|
return aggs;
|
|
}
|
|
}
|
|
|
|
+
|
|
public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
|
|
|
|
- if (searchResponse == null) {return new ArrayList<>(0);}
|
|
|
|
- if (searchResponse.hits() == null) {return new ArrayList<>(0);}
|
|
|
|
|
|
+ if (searchResponse == null) {
|
|
|
|
+ return new ArrayList<>(0);
|
|
|
|
+ }
|
|
|
|
+ if (searchResponse.hits() == null) {
|
|
|
|
+ return new ArrayList<>(0);
|
|
|
|
+ }
|
|
//if (CommonUtils.isCollectionEmpty(searchResponse.hits().hits())) {return new ArrayList<>(0);}
|
|
//if (CommonUtils.isCollectionEmpty(searchResponse.hits().hits())) {return new ArrayList<>(0);}
|
|
|
|
|
|
List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
|
|
List<Hit<ObjectNode>> hits = searchResponse.hits().hits();
|
|
@@ -386,29 +397,35 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
|
|
|
//输出结果日志
|
|
//输出结果日志
|
|
String line = "=>";
|
|
String line = "=>";
|
|
- for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
|
- line += " "+entry.getKey()
|
|
|
|
- +":"+ (entry.getValue()==null?"null":entry.getValue().toString());
|
|
|
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
|
+ line += " " + entry.getKey()
|
|
|
|
+ + ":" + (entry.getValue() == null ? "null" : entry.getValue().toString());
|
|
}
|
|
}
|
|
System.out.println(line);
|
|
System.out.println(line);
|
|
}
|
|
}
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
|
|
|
|
- public String subFieldIndex(String index, String fieldName){
|
|
|
|
- if(fieldName.startsWith(index) && fieldName.length()> index.length()+1) return fieldName.substring(index.length()+1);
|
|
|
|
|
|
+ public String subFieldIndex(String index, String fieldName) {
|
|
|
|
+ if (fieldName.startsWith(index) && fieldName.length() > index.length() + 1)
|
|
|
|
+ return fieldName.substring(index.length() + 1);
|
|
return fieldName;
|
|
return fieldName;
|
|
}
|
|
}
|
|
|
|
|
|
public Map<String, Object> objectNode2Map(String index, ObjectNode objectNode) {
|
|
public Map<String, Object> objectNode2Map(String index, ObjectNode objectNode) {
|
|
- if (null == objectNode) {return new HashMap<>(0);}
|
|
|
|
- if (objectNode.isEmpty()) {return new HashMap<>(0);}
|
|
|
|
|
|
+ if (null == objectNode) {
|
|
|
|
+ return new HashMap<>(0);
|
|
|
|
+ }
|
|
|
|
+ if (objectNode.isEmpty()) {
|
|
|
|
+ return new HashMap<>(0);
|
|
|
|
+ }
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
- Map<String, Object> map = objectMapper.convertValue(objectNode, new TypeReference<Map<String, Object>>() {});
|
|
|
|
|
|
+ Map<String, Object> map = objectMapper.convertValue(objectNode, new TypeReference<Map<String, Object>>() {
|
|
|
|
+ });
|
|
|
|
|
|
- for(Map.Entry<String, Object> entry : map.entrySet()){
|
|
|
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
String name = subFieldIndex(index, entry.getKey());
|
|
String name = subFieldIndex(index, entry.getKey());
|
|
- if(name.equals(entry.getKey())) continue;
|
|
|
|
|
|
+ if (name.equals(entry.getKey())) continue;
|
|
|
|
|
|
map.remove(entry.getKey());
|
|
map.remove(entry.getKey());
|
|
map.put(name, entry.getValue());
|
|
map.put(name, entry.getValue());
|