es数据查询优化

This commit is contained in:
暮光:城中城
2019-07-10 22:38:36 +08:00
parent 02a56bc6b5
commit 9a50db6b5d
3 changed files with 76 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package com.zyplayer.doc.data.service.elasticsearch.support;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.elasticsearch.action.DocWriteResponse;
@@ -105,6 +106,26 @@ public abstract class EsAbstractService<T> {
public EsPage<T> getDataByCondition(List<EsQueryColumn> condition, String[] fields, Integer startIndex, Integer pageSize) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 组装条件
for (EsQueryColumn column : condition) {
if (StringUtils.isBlank(column.getValue())) {
if (CollectionUtils.isEmpty(column.getValues())) {
continue;
}
for (Object value : column.getValues()) {
if (column.getType() == 0) {
boolQueryBuilder.must(QueryBuilders.wildcardQuery(column.getKey(), value.toString()));
} else if (column.getType() == 1) {
boolQueryBuilder.must(QueryBuilders.termQuery(column.getKey(), value));
}
}
} else {
if (column.getType() == 0) {
boolQueryBuilder.must(QueryBuilders.wildcardQuery(column.getKey(), column.getValue()));
} else if (column.getType() == 1) {
boolQueryBuilder.must(QueryBuilders.termQuery(column.getKey(), column.getValue()));
}
}
}
condition.forEach(val -> {
if (StringUtils.isNotBlank(val.getValue())) {
if (val.getType() == 0) {

View File

@@ -1,5 +1,7 @@
package com.zyplayer.doc.data.service.elasticsearch.support;
import java.util.List;
/**
* es查询字段封装
* @author 暮光:城中城
@@ -8,6 +10,7 @@ package com.zyplayer.doc.data.service.elasticsearch.support;
public class EsQueryColumn {
private String key;
private String value;
private List<Object> values;
// 类型0=分词搜索 1=不分词
private int type;
@@ -21,6 +24,12 @@ public class EsQueryColumn {
this.type = type;
}
public EsQueryColumn(String key, List<Object> values, int type) {
this.key = key;
this.values = values;
this.type = type;
}
public static EsQueryColumn like(String key, String value){
return new EsQueryColumn(key, value, 0);
}
@@ -29,6 +38,10 @@ public class EsQueryColumn {
return new EsQueryColumn(key, value, 1);
}
public static EsQueryColumn in(String key, List<Object> values){
return new EsQueryColumn(key, values, 1);
}
public String getKey() {
return key;
}
@@ -52,4 +65,12 @@ public class EsQueryColumn {
public void setType(int type) {
this.type = type;
}
public List<Object> getValues() {
return values;
}
public void setValues(List<Object> values) {
this.values = values;
}
}