diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java index 20646ebb5..37cd33739 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java @@ -25,7 +25,6 @@ import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.functionscore.FieldValueFactorFunctionBuilder; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; -import org.elasticsearch.index.query.functionscore.GaussDecayFunctionBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.search.aggregations.*; import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested; @@ -57,7 +56,7 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { // 最小分词匹配数 - private static final String MINIMUM_SHOULD_MATCH = "1"; + private static final String MINIMUM_SHOULD_MATCH = "2"; private static final String ATTR_PATH = "attrList"; private static final String ATTR_VALUE = "attrList.value"; @@ -367,16 +366,10 @@ private NativeSearchQueryBuilder createSearchQueryBuilder(EsGoodsSearchDTO searc //关键字检索 if (CharSequenceUtil.isEmpty(searchDTO.getKeyword())) { - List filterFunctionBuilders = new ArrayList<>(); - GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 50, 10, 50).setWeight(2); - FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchAllQuery(), skuNoScore); - filterFunctionBuilders.add(skuNoBuilder); - FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3); - FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchAllQuery(), buyCountScore); - filterFunctionBuilders.add(buyCountBuilder); + List filterFunctionBuilders = this.buildFunctionSearch(); FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()]; filterFunctionBuilders.toArray(builders); - FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders) + FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery(), builders) .scoreMode(FunctionScoreQuery.ScoreMode.SUM) .setMinScore(2); //聚合搜索则将结果放入过滤条件 @@ -532,18 +525,16 @@ private void propSearch(BoolQueryBuilder filterBuilder, EsGoodsSearchDTO searchD * @param keyword 关键字 */ private void keywordSearch(BoolQueryBuilder filterBuilder, String keyword) { - List filterFunctionBuilders = new ArrayList<>(); - if (keyword.contains(" ")) { - for (String s : keyword.split(" ")) { - filterFunctionBuilders.addAll(this.buildKeywordSearch(s)); - } - } else { - filterFunctionBuilders = this.buildKeywordSearch(keyword); - } + + List filterFunctionBuilders = this.buildFunctionSearch(); + + //分词匹配 + // operator 为 AND 时 需全部分词匹配。为 OR 时 需配置 minimumShouldMatch(最小分词匹配数)不设置默认为1 + MatchQueryBuilder goodsNameMatchQuery = QueryBuilders.matchQuery("goodsName", keyword).operator(Operator.OR).minimumShouldMatch(MINIMUM_SHOULD_MATCH); FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()]; filterFunctionBuilders.toArray(builders); - FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(QueryBuilders.matchQuery("goodsName", keyword).operator(Operator.OR).minimumShouldMatch(MINIMUM_SHOULD_MATCH), builders) + FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(goodsNameMatchQuery, builders) .scoreMode(FunctionScoreQuery.ScoreMode.SUM) .setMinScore(2); //聚合搜索则将结果放入过滤条件 @@ -553,26 +544,20 @@ private void keywordSearch(BoolQueryBuilder filterBuilder, String keyword) { /** * 构造关键字查询 * - * @param keyword 关键字 * @return 构造查询的集合 */ - private List buildKeywordSearch(String keyword) { + private List buildFunctionSearch() { List filterFunctionBuilders = new ArrayList<>(); - // operator 为 AND 时 需全部分词匹配。为 OR 时 需配置 minimumShouldMatch(最小分词匹配数)不设置默认为1 - MatchQueryBuilder goodsNameQuery = QueryBuilders.matchQuery("goodsName", keyword).operator(Operator.OR).minimumShouldMatch(MINIMUM_SHOULD_MATCH); - //分词匹配 - filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(goodsNameQuery, - ScoreFunctionBuilders.weightFactorFunction(10))); - //属性匹配 - filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.nestedQuery(ATTR_PATH, QueryBuilders.wildcardQuery(ATTR_VALUE, "*" + keyword + "*"), ScoreMode.None), - ScoreFunctionBuilders.weightFactorFunction(8))); - - GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 50, 10, 50).setWeight(2); - FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(goodsNameQuery, skuNoScore); + +// GaussDecayFunctionBuilder skuNoScore = ScoreFunctionBuilders.gaussDecayFunction("skuSource", 100, 10).setWeight(2); +// FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore); +// filterFunctionBuilders.add(skuNoBuilder); + FieldValueFactorFunctionBuilder skuNoScore = ScoreFunctionBuilders.fieldValueFactorFunction("skuSource").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3); + FunctionScoreQueryBuilder.FilterFunctionBuilder skuNoBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(skuNoScore); filterFunctionBuilders.add(skuNoBuilder); FieldValueFactorFunctionBuilder buyCountScore = ScoreFunctionBuilders.fieldValueFactorFunction("buyCount").modifier(FieldValueFactorFunction.Modifier.LOG1P).setWeight(3); - FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(goodsNameQuery, buyCountScore); + FunctionScoreQueryBuilder.FilterFunctionBuilder buyCountBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(buyCountScore); filterFunctionBuilders.add(buyCountBuilder); return filterFunctionBuilders; }