private SearchSourceBuilder searchMallEsCondition(MallEsSearchFormDTO form) {
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 关键词查询[商品名称/货品名称/品牌名称]
// if (StringUtils.isNotBlank(form.getKeywords())) {
// List<String> analyzedWords = IkAnalzyerUtil.segmentPhraseByIk(form.getKeywords());
// for (String word : analyzedWords) {
// boolQuery.must(queryStringQuery(String.format("*%s*", word.trim())).field("goodsName").field("goodsInfoName"));
// }
// }
if (StringUtils.isNotBlank(form.getKeywords())) {
List<String> analyzedWords = IkAnalzyerUtil.segmentPhraseByIk(form.getKeywords());
StringBuilder stringBuilder = new StringBuilder();
analyzedWords.forEach(s -> {
stringBuilder.append(s).append(" ");
});
boolQuery.must(QueryBuilders.multiMatchQuery(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString(), "goodsInfoName", "goodsName").operator(Operator.OR).minimumShouldMatch("2<70%"));
}
// 指定货品编码参数组合查询
// List<String> goodsInfoItemNos = form.getGoodsInfoItemNos();
// if (!ListHelper.isObjectNullOrEmpty(goodsInfoItemNos) && goodsInfoItemNos.size() > 0) {
// BoolQueryBuilder includeBoolQuery = QueryBuilders.boolQuery();
// for (String goodsInfoItemNo : goodsInfoItemNos) {
// BoolQueryBuilder inBoolQuery = QueryBuilders.boolQuery();
// inBoolQuery.must(matchQuery("goodsInfoItemNo", goodsInfoItemNo));
// includeBoolQuery.should(inBoolQuery);
// }
// boolQuery.must(includeBoolQuery);
// }
//可销售渠道
if (!ListHelper.isNullOrEmpty(form.getSaleChannels())) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("saleChannels.identityCode", form.getSaleChannels()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("saleChannels", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
//商品名称模糊查询
if (StringUtils.isNotEmpty(form.getGoodsName())) {
//boolQuery.must(queryStringQuery(String.format("*%s*", form.getGoodsName())).field("goodsName"));
boolQuery.must(queryStringQuery(String.format("\"*%s*\" OR *%s*", form.getGoodsName(), form.getGoodsName())).field("goodsName"));
}
//货品名称模糊查询
if (StringUtils.isNotEmpty(form.getGoodsInfoName())) {
//boolQuery.must(queryStringQuery(String.format("*%s*", form.getGoodsInfoName())).field("goodsInfoName"));
boolQuery.must(queryStringQuery(String.format("\"*%s*\" OR *%s*", form.getGoodsInfoName(), form.getGoodsInfoName())).field("goodsInfoName"));
}
if (!StringHelper.isNullOrEmpty(form.getPromotionFlag()) && "1".equals(form.getPromotionFlag())) {
boolQuery.must(QueryBuilders.nestedQuery("marketingActivityList", QueryBuilders.existsQuery("marketingActivityList"), ScoreMode.None));
searchSourceBuilder.sort("activityGoodsSort", SortOrder.ASC);
}
if (!StringHelper.isNullOrEmpty(form.getPromotionGrade())) {
boolQuery.must(QueryBuilders.nestedQuery("marketingActivityList", QueryBuilders.queryStringQuery(String.format("*%s*", form.getPromotionGrade())).field("marketingActivityList.marketJoinGrade"), ScoreMode.None));
}
if (!StringHelper.isNullOrEmpty(form.getShowChannel())) {
boolQuery.must(QueryBuilders.nestedQuery("marketingActivityList", QueryBuilders.queryStringQuery(String.format("*%s*", form.getShowChannel())).field("marketingActivityList.showChannel"), ScoreMode.None));
}
if (!StringHelper.isNullOrEmpty(form.getShowStock()) && "0".equals(form.getShowStock())) {
RangeQueryBuilder goodsInfoStock = rangeQuery("goodsInfoStock")
.gt(0);
boolQuery.must(goodsInfoStock);
}
/******查询是否上架商品********/
if (StringUtil.isNotEmptyOrWhiteSpace(form.getGoodsInfoAdded())) {
/****-1的时候表示全部****/
if (!form.getGoodsInfoAdded().equals("-1")) {
//默认查询是要在列表展示的
if (form.getKeywords() != null) {
// 搜索是否上架的商品
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", form.getGoodsInfoAdded()));
} else {
if (form.isShowList()) {
// 搜索是否上架的商品
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", form.getGoodsInfoAdded()))
// 是否列表显示
.must(QueryBuilders.termQuery("showList", "1"));
} else {
// 搜索是否上架的商品
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", form.getGoodsInfoAdded()));
}
}
}
} else {
//默认查询是要在列表展示的
if (form.getKeywords() != null) {
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", "1"));
} else {
if (form.isShowList()) {
// 搜索上架商品
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", "1"))
// 是否列表显示
.must(QueryBuilders.termQuery("showList", "1"));
} else {
// 搜索上架商品
boolQuery.must(QueryBuilders.termQuery("goodsInfoAdded", "1"));
}
}
}
//灰度上架:0=否;1=是
if (!StringUtil.isNotEmptyOrWhiteSpace(form.getAlpha())) {
//未设置,默认不看灰度上架货品
boolQuery.must(QueryBuilders.termQuery("alpha", "0"));
} else {
if (form.getAlpha().equals("1")) {
//能看灰度发布商品,不加条件筛选
} else {
boolQuery.must(QueryBuilders.termQuery("alpha", "0"));
}
}
//Spu展示标记
if (StringUtil.isNotEmptyOrWhiteSpace(form.getDisplaySpuFlag())) {
boolQuery.must(QueryBuilders.termQuery("displaySpuFlag", form.getDisplaySpuFlag()));
}
//查询品牌id
if (form.getBrandId() != null && form.getBrandId() > 0L) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("brand.brandId", form.getBrandId().toString()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("brand", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
//分类id
if (null != form.getCatId() && form.getCatId() > 0L) {
boolQuery.must(termQuery("catId", form.getCatId()));
}
// 指定商品ID查询
if (null != form.getGoodsId() && form.getGoodsId() > 0L) {
boolQuery.must(termQuery("goodsId", form.getGoodsId()));
}
// 多个商品ID查询
if (!ListHelper.isNullOrEmpty(form.getGoodsIds())) {
boolQuery.must(QueryBuilders.termsQuery("goodsId", form.getGoodsIds()));
}
// 指定货品ID查询
if (null != form.getGoodsInfoId() && form.getGoodsInfoId() > 0L) {
boolQuery.must(termQuery("goodsInfoId", form.getGoodsInfoId()));
}
// 多个货品ID查询
if (!ListHelper.isNullOrEmpty(form.getGoodsInfoIds())) {
boolQuery.must(QueryBuilders.termsQuery("goodsInfoId", form.getGoodsInfoIds()));
}
// 指定货品编码查询
if (StringUtils.isNotEmpty(form.getGoodsInfoItemNo())) {
boolQuery.must(matchQuery("goodsInfoItemNo", form.getGoodsInfoItemNo()));
}
// 多个货号询
if (!ListHelper.isNullOrEmpty(form.getGoodsInfoItemNos())) {
boolQuery.must(QueryBuilders.termsQuery("goodsInfoItemNo", form.getGoodsInfoItemNos()));
}
// 指定商品编码查询
if (StringUtils.isNotEmpty(form.getGoodsNo())) {
boolQuery.must(termQuery("goodsNo", form.getGoodsNo()));
}
// 多个商品编码
if (!ListHelper.isNullOrEmpty(form.getGoodsNos())) {
boolQuery.must(QueryBuilders.termsQuery("goodsNo", form.getGoodsNos()));
}
// 分类查询
if (ArrayUtils.isNotEmpty(form.getCids()) && form.isVisitGcpt()) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("cateList.id", form.getCids()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("cateList", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
//剔除生产配套分类
if (ArrayUtils.isNotEmpty(form.getCids()) && !form.isVisitGcpt()) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.mustNot(QueryBuilders.termsQuery("cateList.id", form.getCids()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("cateList", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
//至造云排除三级分类
if (null != form.getExcludeFirstCateIds() && form.getExcludeFirstCateIds().size() > 0) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("cateList.id", form.getExcludeFirstCateIds().stream().map(x -> x + "").collect(Collectors.toList())));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("cateList", boolQueryBuilder, ScoreMode.None);
boolQuery.mustNot(nestedQueryBuilder);
}
// 品牌查询
if (ArrayUtils.isNotEmpty(form.getBrands())) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("brand.brandName", form.getBrands()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("brand", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
// 品牌查询
if (ArrayUtils.isNotEmpty(form.getBrandIds())) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("brand.brandId", Arrays.asList(form.getBrandIds())));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("brand", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
if (!StringHelper.isNullOrEmpty(form.getBrandName())) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("brand.brandName", form.getBrandName()));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("brand", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
// 扩展参数
if (ArrayUtils.isNotEmpty(form.getParams())) {
for (String param : form.getParams()) {
String[] paramArr = param.split(":");
if ("价格".equals(paramArr[0]) && paramArr.length > 1 && StringUtils.isNotEmpty(paramArr[1])) {
long[] prices = Arrays.stream(paramArr[1].split("-")).mapToLong(a -> {
try {
return Long.parseLong(a);
} catch (Exception e) {
log.error("商品根据价格查询出现异常", e);
return 0L;
}
}).toArray();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("goodsInfoPreferPrice");
rangeQuery.gte(prices[0]);
rangeQuery.lte(prices.length > 1 ? prices[1] : 0);
boolQuery.filter(rangeQuery);
} else {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("paramList.attributeName", paramArr[0]));
boolQueryBuilder.must(QueryBuilders.termQuery("paramList.attributeValue", paramArr[1]));
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("paramList", boolQueryBuilder, ScoreMode.None);
boolQuery.must(nestedQueryBuilder);
}
}
}
// 只显示有货
if (Objects.nonNull(form.getShowStock()) && "0".equals(form.getShowStock())) {
// searchRequest.addFilter(FilterBuilders.scriptFilter(filter_script)
// .addParam(CHECKWARE, Objects.isNull(form.getWareIds()) ? null : form.getWareIds()[0]));
}
if (form.getGroupGoodsId()) {
CollapseBuilder collapseBuilder = new CollapseBuilder("goodsId");
searchSourceBuilder.collapse(collapseBuilder);
}
// 排序
if (StringUtils.isNotBlank(form.getSort())) {
switch (form.getSort()) {
// 价格升序
case "11D":
// Script script = new Script(sort_script);
// ScriptSortBuilder scriptSortBuilder = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.ASC);
// searchSourceBuilder.sort(scriptSortBuilder);
searchSourceBuilder.sort("goodsInfoMarketPrice", SortOrder.ASC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
break;
// 价格降序
case "1D":
searchSourceBuilder.sort("goodsInfoMarketPrice", SortOrder.DESC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
break;
// 销量降序
case "2D":
//默认过滤售罄的货品,三大专区,热销
BoolQueryBuilder includeBoolQuery = QueryBuilders.boolQuery();
//库存大于0
RangeQueryBuilder goodsInfoStock = rangeQuery("goodsInfoStock")
.gt(0);
//允许超卖
includeBoolQuery.should(QueryBuilders.termQuery("overSold", "1"));
includeBoolQuery.should(goodsInfoStock);
boolQuery.must(includeBoolQuery);
searchSourceBuilder.sort("mallSales", SortOrder.DESC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
searchSourceBuilder.sort("goodsInfoAddedTime", SortOrder.DESC);
break;
// 销量升序
case "22D":
searchSourceBuilder.sort("mallSales", SortOrder.ASC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
searchSourceBuilder.sort("goodsInfoAddedTime", SortOrder.ASC);
break;
// 创建时间升序
case "33D":
searchSourceBuilder.sort("createDate", SortOrder.ASC);
break;
// 创建时间降序
case "3D":
searchSourceBuilder.sort("createDate", SortOrder.DESC);
// searchRequest.addSort(new ScriptSortBuilder(date_sort_script, "number")
// .order(SortOrder.DESC));
// searchRequest.addSort(new ScriptSortBuilder(stock_sort_script, "number")
// .order(SortOrder.DESC));
break;
// 收藏升序
case "44D":
// searchRequest.addSort("collectionCount", SortOrder.ASC);
break;
// 收藏降序
case "4D":
// searchRequest.addSort("collectionCount", SortOrder.DESC);
break;
// 上架时间升序
case "55D":
searchSourceBuilder.sort("_score", SortOrder.DESC);
searchSourceBuilder.sort("goodsInfoAddedTime", SortOrder.ASC);
break;
// 上架时间降序
case "5D":
searchSourceBuilder.sort("_score", SortOrder.DESC);
searchSourceBuilder.sort("goodsInfoAddedTime", SortOrder.DESC);
break;
// 评论数升序
case "66D":
// searchRequest.addSort("comment.commentCount", SortOrder.ASC);
break;
// 评论数降序
case "6D":
// searchRequest.addSort("comment.commentCount", SortOrder.DESC);
break;
case "7D":
String marketJoinGrade = getMarketJoinGrade(form.getPriceGradeId());
//按照促销活动排序
if (!StringHelper.isNullOrEmpty(marketJoinGrade)) {
Map<String, Object> params = new HashMap<>();
params.put("zero", 0);
params.put("one", 1);
params.put("nullCode", null);
params.put("emptyStr", "");
params.put("marketJoinGrade", marketJoinGrade);
Script marketJoinGradeScript = new Script(ScriptType.INLINE, "painless", marketing_sort, params);
ScriptSortBuilder gradeScriptSortBuilder = SortBuilders.scriptSort(marketJoinGradeScript, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
searchSourceBuilder.sort(gradeScriptSortBuilder);
} else {
Map<String, Object> params = new HashMap<>();
params.put("zero", 0);
params.put("one", 1);
params.put("marketJoinGrade", marketJoinGrade);
params.put("nullCode", null);
params.put("emptyStr", "");
Script marketJoinGradeScript = new Script(ScriptType.INLINE, "painless", no_login_marketing_sort, params);
ScriptSortBuilder gradeScriptSortBuilder = SortBuilders.scriptSort(marketJoinGradeScript, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
searchSourceBuilder.sort(gradeScriptSortBuilder);
}
//按照商品名称带关键字货品
searchSourceBuilder.sort("weight", SortOrder.DESC);
//按照库存排序
Map<String, Object> stockParams = new HashMap<>();
stockParams.put("zero", 0);
stockParams.put("minusOne", -1);
stockParams.put("zeroStr", "0");
stockParams.put("oneStr", "1");
Script stockScript = new Script(ScriptType.INLINE, "painless", stock_sort, stockParams);
ScriptSortBuilder stockOrder = SortBuilders.scriptSort(stockScript, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
searchSourceBuilder.sort(stockOrder);
searchSourceBuilder.sort("_score", SortOrder.DESC);
// searchSourceBuilder.sort("cat1Sort",SortOrder.ASC);
// searchSourceBuilder.sort("cat2Sort",SortOrder.ASC);
// searchSourceBuilder.sort("cat3Sort",SortOrder.ASC);
searchSourceBuilder.sort("goodsInfoAddedTime", SortOrder.DESC);
break;
//根据价格升序
case "88D": {
/* if (StringHelper.isNullOrEmpty(form.getPriceGradeId())){
break;
}
String sortField = getPriceLevel(form.getPriceGradeId());
Script script2 = new Script("def price=_source."+sortField+"; if(price<0){return 0;}else{return price;}");
ScriptSortBuilder scriptSortBuilder2 = SortBuilders.scriptSort(script2, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.ASC);
searchSourceBuilder.sort(scriptSortBuilder2);*/
searchSourceBuilder.sort("goodsInfoMarketPrice", SortOrder.ASC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
}
break;
//根据价格降序
case "8D": {
/* if (StringHelper.isNullOrEmpty(form.getPriceGradeId())){
break;
}
String sortField = getPriceLevel(form.getPriceGradeId());
Script script3 = new Script("def price=_source."+sortField+"; if(price<0){return 0;}else{return price;}");
ScriptSortBuilder scriptSortBuilder3 = SortBuilders.scriptSort(script3, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
searchSourceBuilder.sort(scriptSortBuilder3);*/
searchSourceBuilder.sort("goodsInfoMarketPrice", SortOrder.DESC);
searchSourceBuilder.sort("_score", SortOrder.DESC);
}
break;
default:
break;
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (form.getGoodsInfoAddedTimeSort() != null && form.getGoodsInfoAddedTimeSort() == 0) {
boolQuery.must(termQuery("goodsInfoAdded", "1"));
searchSourceBuilder.sort(sdf.format(form.getGoodsInfoAddedTime()), SortOrder.DESC);
} else if (form.getGoodsInfoAddedTimeSort() != null && form.getGoodsInfoAddedTimeSort() == 1) {
boolQuery.must(termQuery("goodsInfoAdded", "1"));
searchSourceBuilder.sort(sdf.format(form.getGoodsInfoAddedTime()), SortOrder.ASC);
}
searchSourceBuilder.query(boolQuery);
AggregationBuilder aggregation =
AggregationBuilders.nested("paramList", "paramList")
.subAggregation(AggregationBuilders.terms("attributeName")
.field("paramList.attributeName").size(Integer.MAX_VALUE)
.subAggregation(AggregationBuilders.terms("attributeValue").field("paramList.attributeValue"))
);
AggregationBuilder aggregation2 =
AggregationBuilders.nested("brand", "brand")
.subAggregation(AggregationBuilders.terms("brandName").field("brand.brandName").size(Integer.MAX_VALUE)
.subAggregation(AggregationBuilders.terms("brandLogo").field("brand.brandLogo"))
);
searchSourceBuilder.aggregation(aggregation);
searchSourceBuilder.aggregation(aggregation2);
searchSourceBuilder.from((form.getPageNo() - 1) * form.getPageSize()).size(form.getPageSize());
return searchSourceBuilder;
}
文章来源地址https://www.toymoban.com/news/detail-405001.html
文章来源:https://www.toymoban.com/news/detail-405001.html
到了这里,关于ES java生产相关api操作(SearchSourceBuilder)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!