BoolQueryBuilder fuzzyQuery = QueryBuilders.boolQuery(); String[] matchFieldNames = {"itemName", "categoryNames", "baseCategoryName", "materialNameAilas", "materialName", "supplierNames", "attributeNames", "attributeNamesAilas", "attributeValueNames"}; MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(paramsDto.getFuzzyQuery(), matchFieldNames) .type(MultiMatchQueryBuilder.Type.BEST_FIELDS); // 设置权重 Map<String, Float> mfieldsBoosts = new HashMap<>(); mfieldsBoosts.put("itemName", 11f); mfieldsBoosts.put("categoryNames", 6f); mfieldsBoosts.put("baseCategoryName", 5f); mfieldsBoosts.put("materialNameAilas", 9f); mfieldsBoosts.put("materialName", 10f); mfieldsBoosts.put("supplierNames", 4f); mfieldsBoosts.put("attributeNames", 8f); mfieldsBoosts.put("attributeNamesAilas", 7f); mfieldsBoosts.put("attributeValueNames", 3f); multiMatchQueryBuilder.fields(mfieldsBoosts); fuzzyQuery.should(multiMatchQueryBuilder) .should(QueryBuilders.wildcardQuery("itemSkuCodes", "*" + paramsDto.getFuzzyQuery() + "*")) .should(QueryBuilders.wildcardQuery("itemSkuModels", "*" + paramsDto.getFuzzyQuery() + "*")); filter.must(fuzzyQuery);
这样就可以根据字段权重计算得分,进行查询结果排序
标签:自定义,Spring,fuzzyQuery,should,mfieldsBoosts,Elasticsearch,put,getFuzzyQuery,Query From: https://www.cnblogs.com/xiaoxiaoxingkong/p/18082316