Java Code Examples for org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder#withFilter()

The following examples show how to use org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder#withFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomSearchRepositoryImpl.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
@Override
    public Page<File> findWithHighlightedSummary(Pageable pageable, String query, List<String> version, List<String> project) {
        //QueryBuilder searchQuery = Queries.constructQuery(query);
        //return elasticsearchTemplate.queryForPage(new NativeSearchQuery(searchQuery), File.class, new ResultHighlightMapper());

        NativeSearchQueryBuilder nativeQuery = Queries.constructQueryWithHighlight(query, pageable, 3);

        BoolQueryBuilder ensembleVersion = QueryBuilders.boolQuery();
        BoolQueryBuilder ensembleProjet = QueryBuilders.boolQuery();

        if (version != null && !version.isEmpty()) {
            ensembleVersion = ensembleVersion.should(QueryBuilders.termsQuery("version.raw", version));
        }
        if (project != null && !project.isEmpty()) {
            ensembleProjet = ensembleProjet.should(QueryBuilders.termsQuery("project.raw", project));
        }

        nativeQuery = nativeQuery.withFilter(QueryBuilders.boolQuery().must(ensembleVersion).must(ensembleProjet));
        log.debug("query : {}", nativeQuery.toString());
        SearchQuery searchQuery = nativeQuery.build();
        log.debug("query : {}", searchQuery.getQuery());
        log.debug("filter: {}", searchQuery.getFilter());
        return elasticsearchTemplate.queryForPage(searchQuery, File.class, new ResultHighlightMapper());

//        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    }
 
Example 2
Source File: EsProductServiceImpl.java    From mall with Apache License 2.0 5 votes vote down vote up
@Override
public Page<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    List<EsProduct> esProductList = productDao.getAllEsProductList(id);
    if (esProductList.size() > 0) {
        EsProduct esProduct = esProductList.get(0);
        String keyword = esProduct.getName();
        Long brandId = esProduct.getBrandId();
        Long productCategoryId = esProduct.getProductCategoryId();
        //根据商品标题、品牌、分类进行搜索
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword),
                ScoreFunctionBuilders.weightFactorFunction(8)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword),
                ScoreFunctionBuilders.weightFactorFunction(2)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", keyword),
                ScoreFunctionBuilders.weightFactorFunction(2)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("brandId", brandId),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId),
                ScoreFunctionBuilders.weightFactorFunction(3)));
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                .setMinScore(2);
        //用于过滤掉相同的商品
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.mustNot(QueryBuilders.termQuery("id",id));
        //构建查询条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withFilter(boolQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }
    return new PageImpl<>(null);
}
 
Example 3
Source File: EsProductServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    //分页
    nativeSearchQueryBuilder.withPageable(pageable);
    //过滤
    if (brandId != null || productCategoryId != null) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (brandId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId", brandId));
        }
        if (productCategoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("productCategoryId", productCategoryId));
        }
        nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
    }
    //搜索
    if (StringUtils.isEmpty(keyword)) {
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
    } else {
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword),
                ScoreFunctionBuilders.weightFactorFunction(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", 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("id").order(SortOrder.DESC));
    }else if(sort==2){
        //按销量从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
    }else if(sort==3){
        //按价格从低到高
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    }else if(sort==4){
        //按价格从高到低
        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);
}
 
Example 4
Source File: EsProductServiceImpl.java    From mall-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    //分页
    nativeSearchQueryBuilder.withPageable(pageable);
    //过滤
    if (brandId != null || productCategoryId != null) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (brandId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId", brandId));
        }
        if (productCategoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("productCategoryId", productCategoryId));
        }
        nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
    }
    //搜索
    if (StringUtils.isEmpty(keyword)) {
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
    } else {
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword),
                ScoreFunctionBuilders.weightFactorFunction(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", 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("id").order(SortOrder.DESC));
    }else if(sort==2){
        //按销量从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
    }else if(sort==3){
        //按价格从低到高
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    }else if(sort==4){
        //按价格从高到低
        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);
}
 
Example 5
Source File: EsProductServiceImpl.java    From macrozheng with Apache License 2.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    //分页
    nativeSearchQueryBuilder.withPageable(pageable);
    //过滤
    if (brandId != null || productCategoryId != null) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (brandId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId", brandId));
        }
        if (productCategoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("productCategoryId", productCategoryId));
        }
        nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
    }
    //搜索
    if (StringUtils.isEmpty(keyword)) {
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
    } else {
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword),
                ScoreFunctionBuilders.weightFactorFunction(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", 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("id").order(SortOrder.DESC));
    }else if(sort==2){
        //按销量从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
    }else if(sort==3){
        //按价格从低到高
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    }else if(sort==4){
        //按价格从高到低
        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);
}
 
Example 6
Source File: EsProductServiceImpl.java    From mall with Apache License 2.0 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) {
    Pageable pageable = PageRequest.of(pageNum, pageSize);
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    //分页
    nativeSearchQueryBuilder.withPageable(pageable);
    //过滤
    if (brandId != null || productCategoryId != null) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (brandId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId", brandId));
        }
        if (productCategoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("productCategoryId", productCategoryId));
        }
        nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
    }
    //搜索
    if (StringUtils.isEmpty(keyword)) {
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
    } else {
        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("name", keyword),
                ScoreFunctionBuilders.weightFactorFunction(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("subTitle", keyword),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", 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("id").order(SortOrder.DESC));
    }else if(sort==2){
        //按销量从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
    }else if(sort==3){
        //按价格从低到高
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    }else if(sort==4){
        //按价格从高到低
        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);
}
 
Example 7
Source File: EsProductServiceImpl.java    From macrozheng-mall with MIT License 4 votes vote down vote up
@Override
public Page<EsProduct> search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) {
    Pageable pageable = new PageRequest(pageNum, pageSize);
    NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
    //分页
    nativeSearchQueryBuilder.withPageable(pageable);
    //过滤
    if (brandId != null || productCategoryId != null) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (brandId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("brandId", brandId));
        }
        if (productCategoryId != null) {
            boolQueryBuilder.must(QueryBuilders.termQuery("productCategoryId", productCategoryId));
        }
        nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
    }
    //搜索
    FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
            .add(QueryBuilders.matchQuery("name", keyword),
                    ScoreFunctionBuilders.weightFactorFunction(10))
            .add(QueryBuilders.matchQuery("subTitle", keyword),
                    ScoreFunctionBuilders.weightFactorFunction(5))
            .add(QueryBuilders.matchQuery("keywords", keyword),
                    ScoreFunctionBuilders.weightFactorFunction(2))
            .scoreMode("sum")
            .setMinScore(2);
    if (StringUtils.isEmpty(keyword)) {
        nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());
    } else {
        nativeSearchQueryBuilder.withQuery(functionScoreQueryBuilder);
    }
    //排序
    if(sort==1){
        //按新品从新到旧
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
    }else if(sort==2){
        //按销量从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
    }else if(sort==3){
        //按价格从低到高
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
    }else if(sort==4){
        //按价格从高到低
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
    }else{
        //按相关度
        nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
    }
    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);
}