elasticsearch 中获得 分词匹配 +(or) 全词模糊匹配 的查询结果(boolQuery)

 @Override
    public Response selectStructuredDataList(StructuredDataDTO structuredDataDTO) {
        StructuredDataTypeEnum structuredDataTypeEnum = StructuredDataTypeEnum.getByStructuredType(structuredDataDTO.getStructuredType());
        // 是否需要中转到ai库中
        if(structuredDataTypeEnum !=null && structuredDataTypeEnum.isAi()){
                BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                if(StringUtils.isNotBlank(structuredDataDTO.getStructuredName())) {
                    // 获得分词或者全词模糊匹配的boolQuery
                    boolQueryBuilder.must(getOrBoolQuery("minSymptom", structuredDataDTO.getStructuredName()));
                }
                if (structuredDataTypeEnum.getBiOneClassify() != null) {
                    boolQueryBuilder.must(QueryBuilders.termQuery("oneClassify", structuredDataTypeEnum.getBiOneClassify()));
                }
                if (structuredDataTypeEnum.getBiTwoClassify() != null) {
                    boolQueryBuilder.must(QueryBuilders.termQuery("twoClassify", structuredDataTypeEnum.getBiTwoClassify()));
                }
            // 现在只取最大500条(20200415 迪迪确认)
            return Response.ok(getBiStructuredDataList(boolQueryBuilder, 0, 500));
        }
        // 中医证型
        if(StructuredDataTypeEnum.SYNDROME_TCM.getStructuredType().equals(structuredDataDTO.getStructuredType())){
            return Response.ok(getBiTypeSyndromeWordList(getOrBoolQuery("zx", structuredDataDTO.getStructuredName()), 0, 500));
        }

        // 中医病名
        if(StructuredDataTypeEnum.CHINESE_DISEASE_NAME.getStructuredType().equals(structuredDataDTO.getStructuredType())){
            return Response.ok(getBiStandardNameLibraryNewList(getOrBoolQuery("bm", structuredDataDTO.getStructuredName()), 0, 500));
        }


        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!ObjectUtil.objectIsNull(structuredDataDTO.getStructuredName())) {
            queryWrapper.like("structured_name", structuredDataDTO.getStructuredName());
        }
        queryWrapper.eq("structured_type", structuredDataDTO.getStructuredType());
        queryWrapper.eq("is_deleted", 1);
        List tongues = structuredDataMapper.selectList(queryWrapper);
        logger.info("舌像数据返回参数:tongues {}", JSON.toJSONString(tongues));
        return Response.ok(tongues);
    }

    /**
     * 获得分词或者全词模糊匹配的boolQuery
     * @param fieldName
     * @param value
     * @return
     */
    private BoolQueryBuilder getOrBoolQuery(String fieldName, String value){
        BoolQueryBuilder boolQueryBuilderTwo = QueryBuilders.boolQuery();
        // 分词
        boolQueryBuilderTwo.should(new MatchQueryBuilder(fieldName, value));
        // 全词模糊匹配
        boolQueryBuilderTwo.should(new WildcardQueryBuilder(fieldName, "*" + value + "*"));
        return boolQueryBuilderTwo;
    }

你可能感兴趣的:(java,spring,boot,es,java,elasticsearch)