spring-data-elasticsearch 一对多 5C

现在有三个实体类
关系是一个产品有多个属性
一个属性有多个属性值
如下:
实体类EsProduct:

@Document(indexName = "product", type = "productInfo",shards = 2,replicas = 1,refreshInterval = "-1")
public class EsProduct implements Serializable {

    private static final long serialVersionUID = 2372551074091780419L;
    @Id
    private Long id;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String productSn;
    private Long brandId;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String brandName;
    private Long productCategoryId;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String productName;
    private Long sale;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String subTitle;
    private BigDecimal price;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String keywords;

    private List<EsProductAttribute> attributeList;


    private EsProductCategory productCategorie;

实体类EsProductAttribute:

public class EsProductAttribute implements Serializable {

    private static final long serialVersionUID = 4965902919813623705L;
    @Id
    private Long id;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String paProductAttributeName;//属性名称


    private List<EsProductAttributeValue> attributeValues;

实体类EsProductAttributeValue:

public class EsProductAttributeValue implements Serializable {
    private static final long serialVersionUID = 6713756365860464751L;

    private Long id;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String pavProductAttributeValue;

之前的搜索是这样的,可以成功搜索
控制层代码:

    @ApiOperation(value = "简单搜索:根据关键字,品牌名称或者产品名称,产品编号,副标题搜索(字符串:Text类型最大拆分)")
    @RequestMapping(value = "/search/keyword", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<EsProduct>> searchKeyword(@RequestParam(required = false) String keyword,
                                                            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.searchKeyword(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

业务实现层代码:

    @Autowired
    private EsProductRepository productRepository;
    @Override
    public Page<EsProduct> searchKeyword(String keyword, Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return productRepository.findByKeywordsOrProductNameOrBrandNameOrProductSnOrSubTitle(keyword,keyword,keyword,keyword,keyword,pageable);
    }

EsProductRepository接口

public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {


    /**
     * 根据关键字,产品名称,品牌名称,产品编号搜索
     * @param keywords
     * @param productName
     * @param brandName
     * @param page
     * @return
     */
    Page<EsProduct> findByKeywordsOrProductNameOrBrandNameOrProductSnOrSubTitle(String keywords,String productName,String brandName,String productSn,String subTitle,Pageable page);
}

都可以成功搜索
但是在进行属性和属性值搜索的时候告诉我说在EsProduct中找不到paProductAttributeName,我一想有道理,然后改变策略

单独写了一个接口测试
控制层

    @ApiOperation(value = "属性和属性值搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度;1->按销量;2->价格从低到高;3->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                      @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<EsProduct> esProductPage = esProductService.search(keyword,pageNum, pageSize, sort);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }

业务实现层:

@Override
    public Page<EsProduct> search(String keyword,Integer pageNum, Integer pageSize,Integer sort) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        //将连接条件和聚合函数等组合
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //分页
        nativeSearchQueryBuilder.withPageable(pageable);
        //搜索
        if (StringUtils.isEmpty(keyword)) {
            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
        } else {
            List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
            //QueryBuilders:简单的静态工厂”导入静态”使用。主要作用是查询条件(关系),如区间\精确\多值等条件
            //属性名称
            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("paProductAttributeName", keyword),
                    ScoreFunctionBuilders.weightFactorFunction(2)));
            //属性值
            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("value", keyword),
                    ScoreFunctionBuilders.weightFactorFunction(2)));
            //产品名称
//            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productName",keyword),
//                    ScoreFunctionBuilders.weightFactorFunction(2)));
//            //关键字
//            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", keyword),
//                    ScoreFunctionBuilders.weightFactorFunction(2)));
//            //产品编号
//            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productSn", keyword),
//                    ScoreFunctionBuilders.weightFactorFunction(2)));
            FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
            filterFunctionBuilders.toArray(builders);
            FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                    .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                    .setMinScore(2);
            nativeSearchQueryBuilder.withQuery(functionScoreQueryBuilder);
        }
//        //排序
//        if(sort==1){
//            //按销量从高到低
//            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
//        }else if(sort==2){
//            //按价格从低到高
//            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
//        }else if(sort==3){
//            //按价格从高到低
//            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
//        }else{
//            //按相关度
//            nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
//        }
        nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }

es中的数据是:

{
  "code": 200,
  "message": "操作成功",
  "data": {
    "pageNum": 0,
    "pageSize": 5,
    "totalPage": 4,
    "total": 17,
    "list": [
      {
        "id": 40,
        "productSn": "C146179 ",
        "brandId": null,
        "brandName": "PSA(信昌电陶)",
        "productCategoryId": 54,
        "productName": "贴片电容/220nF(224)±10%250V编带",
        "sale": 1000,
        "subTitle": "贴片电容",
        "price": null,
        "keywords": "贴片电容/220nF(224) ±10% 250V 编带 ",
        "attributeList": [
          {
            "id": 49,
            "paProductAttributeName": "容值",
            "attributeValues": [
              {
                "id": 240,
                "pavProductAttributeValue": "0.1pf"
              },
              {
                "id": 241,
                "pavProductAttributeValue": "0.2pf"
              },
              {
                "id": 242,
                "pavProductAttributeValue": "0.25pf"
              },
              {
                "id": 243,
                "pavProductAttributeValue": "0.3pf"
              },
              {
                "id": 244,
                "pavProductAttributeValue": "0.4pf"
              }
            ]
          },
          {
            "id": 51,
            "paProductAttributeName": "额定电压",
            "attributeValues": [
              {
                "id": 290,
                "pavProductAttributeValue": "4v"
              },
              {
                "id": 292,
                "pavProductAttributeValue": "10v"
              },
              {
                "id": 296,
                "pavProductAttributeValue": "30v"
              }
            ]
          }
        ],
        "productCategorie": {
          "id": 54,
          "productCategoryName": "贴片电容"
        }
      },
      {
        "id": 41,
        "productSn": "C394163 ",
        "brandId": null,
        "brandName": "PSA(信昌电陶)",
        "productCategoryId": 54,
        "productName": "贴片电容/10nF(103) ±10% 1KV",
        "sale": 1000,
        "subTitle": "贴片电容",
        "price": null,
        "keywords": "贴片电容/10nF(103) ±10% 1KV ",
        "attributeList": [
          {
            "id": 49,
            "paProductAttributeName": "容值",
            "attributeValues": [
              {
                "id": 245,
                "pavProductAttributeValue": "0.5pf"
              },
              {
                "id": 246,
                "pavProductAttributeValue": "0.6pf"
              },
              {
                "id": 247,
                "pavProductAttributeValue": "0.68pf"
              }
            ]
          },
          {
            "id": 50,
            "paProductAttributeName": "精度",
            "attributeValues": [
              {
                "id": 272,
                "pavProductAttributeValue": "+0.05pf"
              },
              {
                "id": 273,
                "pavProductAttributeValue": "+0.1pf"
              },
              {
                "id": 274,
                "pavProductAttributeValue": "+0.25pf"
              }
            ]
          },
          {
            "id": 51,
            "paProductAttributeName": "额定电压",
            "attributeValues": [
              {
                "id": 291,
                "pavProductAttributeValue": "6.3v"
              },
              {
                "id": 293,
                "pavProductAttributeValue": "16v"
              },
              {
                "id": 297,
                "pavProductAttributeValue": "35v"
              }
            ]
          }
        ],
        "productCategorie": {
          "id": 54,
          "productCategoryName": "贴片电容"
        }
      },
      {
        "id": 44,
        "productSn": "C342641 ",
        "brandId": null,
        "brandName": "TDK",
        "productCategoryId": 54,
        "productName": "贴片电容/47nF(473) ±5% 100V 编带",
        "sale": 1000,
        "subTitle": "贴片电容",
        "price": null,
        "keywords": "贴片电容/47nF(473) ±5% 100V 编带 ",
        "attributeList": [
          {
            "id": 49,
            "paProductAttributeName": "容值",
            "attributeValues": [
              {
                "id": 254,
                "pavProductAttributeValue": "1.3pf"
              },
              {
                "id": 255,
                "pavProductAttributeValue": "1.4pf"
              },
              {
                "id": 256,
                "pavProductAttributeValue": "2pf"
              },
              {
                "id": 257,
                "pavProductAttributeValue": "2.1pf"
              }
            ]
          },
          {
            "id": 50,
            "paProductAttributeName": "精度",
            "attributeValues": [
              {
                "id": 278,
                "pavProductAttributeValue": "+5%"
              }
            ]
          }
        ],
        "productCategorie": {
          "id": 54,
          "productCategoryName": "贴片电容"
        }
      },
      {
        "id": 48,
        "productSn": "C11366 ",
        "brandId": null,
        "brandName": " AVX",
        "productCategoryId": 55,
        "productName": "钽电容/22uF(226) ±10% 10V",
        "sale": 1000,
        "subTitle": "钽电容",
        "price": null,
        "keywords": "钽电容/22uF(226) ±10% 10V ",
        "attributeList": [
          {
            "id": 49,
            "paProductAttributeName": "容值",
            "attributeValues": [
              {
                "id": 269,
                "pavProductAttributeValue": "0.1pf"
              },
              {
                "id": 270,
                "pavProductAttributeValue": "0.2pf"
              }
            ]
          },
          {
            "id": 50,
            "paProductAttributeName": "精度",
            "attributeValues": [
              {
                "id": 271,
                "pavProductAttributeValue": "+0.02pf"
              },
              {
                "id": 284,
                "pavProductAttributeValue": "-20%,+80%"
              }
            ]
          }
        ],
        "productCategorie": {
          "id": 55,
          "productCategoryName": "钽电容"
        }
      },
      {
        "id": 52,
        "productSn": "C391913 ",
        "brandId": null,
        "brandName": "SAMYOUNG(韩国三莹)",
        "productCategoryId": 56,
        "productName": "贴片电解电容/10uF ±20% 50V",
        "sale": 1000,
        "subTitle": "贴片电解电容",
        "price": null,
        "keywords": "贴片电解电容/10uF ±20% 50V ",
        "attributeList": [],
        "productCategorie": {
          "id": 56,
          "productCategoryName": "贴片电解电容"
        }
      }
    ]
  }
}

然后搜索了半天一点反应也没有,也没有头绪,求助大神指点

java_wxid
java小丑 产品下有一个属性集合,要搜索的是属性集合中的属性名称,还要搜索属性对象中的属性值,属于一对多
一年多之前 回复
java_wxid
java小丑 我看了一下不是适合,跟我之前做的根据关键字,品牌名称或者产品名称,产品编号,副标题搜索效果类似
一年多之前 回复
java_wxid
java小丑 数据是:
一年多之前 回复

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐