org.springframework.data.elasticsearch.core.query.NativeSearchQuery Java Examples

The following examples show how to use org.springframework.data.elasticsearch.core.query.NativeSearchQuery. 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: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public NativeSearchQuery build(boolean rebuild){
		if(!rebuild && nativeSearchQuery!=null){
			return nativeSearchQuery;
		}
		
		if(queryBuilder!=null){
			searchQueryBuilder.withQuery(queryBuilder);
		}else if(matchAllIfQueryNotExists){
			searchQueryBuilder.withQuery(matchAllQuery());
		}
		if(booleanQuery!=null && booleanQuery.hasClauses()){
			booleanQuery.build();
			searchQueryBuilder.withFilter(booleanQuery.boolQuery);
		}
		this.nativeSearchQuery = searchQueryBuilder.build();
		this.nativeSearchQuery.addIndices(this.indices.toArray(new String[0]));
		this.nativeSearchQuery.addTypes(this.types.toArray(new String[0]));
		this.nativeSearchQuery.addSourceFilter(new FetchSourceFilter(includeSources, excludeSources));
		this.sorts.forEach(s->this.nativeSearchQuery.addSort(s));
//		built = true;
		return nativeSearchQuery;
	}
 
Example #2
Source File: CustomSearchRepositoryImpl.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
/**
     * Return all records, and truncate the content with the ResultTruncatedContentMapper
     *
     * @param pageable
     * @param version
     * @param project
     * @return
     */
    @Override
    public Page<File> customfindAll(Pageable pageable, List<String> version, List<String> project, List<String> extension) {
        NativeSearchQueryBuilder nativeSearchQueryBuilder = Queries.constructSearchQueryBuilder("");
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        SearchRequestBuilder searchRequestBuilder = constructRequestBuilder(nativeSearchQuery, pageable, version, project, extension);
        SearchResponse response = searchRequestBuilder.execute().actionGet();

        SearchHit[] hits = response.getHits().hits();
        ResultTruncatedContentMapper mapper = new ResultTruncatedContentMapper();
        return mapper.mapResults(response, File.class, nativeSearchQuery.getPageable());

//        }

    }
 
Example #3
Source File: CustomSearchRepositoryImpl.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Return records for query, and highlight the fragment of content with the ResultHighlightMapper
 *
 * @param pageable
 * @param query
 * @param version
 * @param project
 * @return
 */
@Override
public Page<File> customSearchWithHighlightedSummary(Pageable pageable, String query, List<String> version, List<String> project, List<String> extension) {
    if (StringUtils.isEmpty(query)) {
        log.error("customSearchWithHighlightedSummary return null in case where query = " + query);
        return null;
    }
    NativeSearchQueryBuilder nativeSearchQueryBuilder = Queries.constructSearchQueryBuilder(query);
    NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

    SearchRequestBuilder searchRequestBuilder = constructRequestBuilder(nativeSearchQuery, pageable, version, project, extension);
    searchRequestBuilder.setFetchSource(null, "content");//dont get the content, we have the highlight !

    log.trace("==> Request  ES ==> \n{}", searchRequestBuilder);
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    log.trace("<== Response ES <== \n{}", response);

    SearchHit[] hits = response.getHits().hits();
    ResultHighlightMapper mapper = new ResultHighlightMapper();
    return mapper.mapResults(response, File.class, nativeSearchQuery.getPageable());

}
 
Example #4
Source File: ItemServiceImpl.java    From poseidon with Apache License 2.0 6 votes vote down vote up
@Override
public Page<EsItem> recommend(List<String> itemIds, int page, int size) {
	Pageable pageable = PageRequest.of(page, size);
	String[] itemCatNames;
	if (itemIds.size() != 0) {
		List<EsItem> esItems = itemRepository.findByItemIdIn(itemIds);
		itemCatNames = esItems.stream().map(EsItem::getItemCatName).toArray(String[]::new);
	}
	else {
		itemCatNames = new String[] { "手机" };
	}
	System.out.println(Arrays.toString(itemCatNames));
	MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = QueryBuilders
			.moreLikeThisQuery(new String[] { "itemCatName" }, itemCatNames, null).minTermFreq(1);
	NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
	nativeSearchQueryBuilder.withQuery(moreLikeThisQueryBuilder).withPageable(pageable)
			.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
	NativeSearchQuery query = nativeSearchQueryBuilder.build();
	// return elasticsearchTemplate.queryForPage(query, EsItem.class);
	return itemRepository.search(query);
}
 
Example #5
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
    final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
        .filter(termQuery("tags", "elasticsearch"));

    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
        .build();
    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(2, articles.getTotalHits());
}
 
Example #6
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title")
        .field("tags")
        .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
        .build();

    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(2, articles.getTotalHits());
}
 
Example #7
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND)
        .fuzziness(Fuzziness.ONE)
        .prefixLength(3))
        .build();

    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(1, articles.getTotalHits());
}
 
Example #8
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1))
        .build();

    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(1, articles.getTotalHits());
}
 
Example #9
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
    final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None);

    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
        .build();
    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(2, articles.getTotalHits());
}
 
Example #10
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() {
    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
        .build();

    SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(1, articles.getTotalHits());

    searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
        .build();

    articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
    assertEquals(0, articles.getTotalHits());
}
 
Example #11
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
        .build();

    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(3, articles.getTotalHits());
}
 
Example #12
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
        .build();

    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));

    assertEquals(1, articles.getTotalHits());
    assertEquals("Search engines", articles.getSearchHit(0)
        .getContent()
        .getTitle());
}
 
Example #13
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
    final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND))
        .build();
    final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
    assertEquals(1, articles.getTotalHits());
}
 
Example #14
Source File: CustomerRepositoryTest.java    From strategy-spring-security-acl with Apache License 2.0 5 votes vote down vote up
@Test
public void should_search_all_customers_when_strategy_not_applied() {
  doWithoutCustomerFilter(new Runnable() {
    @Override
    public void run() {
      assertThat(repository.search(new NativeSearchQuery(matchAllQuery()))).containsOnly(aliceSmith, bobSmith, johnDoe, aliceDoe);
    }
  });
}
 
Example #15
Source File: AclElasticsearchRepository.java    From strategy-spring-security-acl with Apache License 2.0 5 votes vote down vote up
@Override
public Page<T> search(SearchQuery query) {
  Assert.isInstanceOf(NativeSearchQuery.class, query, "NativeSearchQuery only are supported :(");
  NativeSearchQuery searchQuery = new NativeSearchQuery(and(query.getQuery(), aclFilter()));
  BeanUtils.copyProperties(query, searchQuery, "query");
  return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
}
 
Example #16
Source File: ItemServiceImpl.java    From poseidon with Apache License 2.0 5 votes vote down vote up
@Override
public Page<EsItem> queryPriceRange(BigDecimal price1, BigDecimal price2, int page, int size) {
	Pageable pageable = PageRequest.of(page, size);
	RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("price").gt(price1).lt(price2);
	NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(rangeQueryBuilder).withPageable(pageable)
			.build();
	return itemRepository.search(query);
}
 
Example #17
Source File: EsProductServiceImpl.java    From macrozheng-mall with MIT License 5 votes vote down vote up
@Override
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    //搜索条件
    if(StringUtils.isEmpty(keyword)){
        builder.withQuery(QueryBuilders.matchAllQuery());
    }else{
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
    }
    //聚合搜索品牌名称
    builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
    //集合搜索分类名称
    builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
    //聚合搜索商品属性,去除type=1的属性
    AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues")
            .path("attrValueList")
            .subAggregation(AggregationBuilders.filter("productAttrs")
            .filter(QueryBuilders.termQuery("attrValueList.type",1))
            .subAggregation(AggregationBuilders.terms("attrIds")
                    .field("attrValueList.productAttributeId")
                    .subAggregation(AggregationBuilders.terms("attrValues")
                            .field("attrValueList.value"))
                    .subAggregation(AggregationBuilders.terms("attrNames")
                            .field("attrValueList.name"))));
    builder.addAggregation(aggregationBuilder);
    NativeSearchQuery searchQuery = builder.build();
    return elasticsearchTemplate.query(searchQuery, response -> {
        LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
        return convertProductRelatedInfo(response);
    });
}
 
Example #18
Source File: EsProductServiceImpl.java    From macrozheng-mall with MIT License 5 votes vote down vote up
@Override
public Page<EsProduct> recommend(Long id, Integer pageNum, Integer pageSize) {
    Pageable pageable = new PageRequest(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();
        //根据商品标题、品牌、分类进行搜索
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
                .add(QueryBuilders.matchQuery("name",keyword),ScoreFunctionBuilders.weightFactorFunction(8))
                .add(QueryBuilders.matchQuery("subTitle",keyword),ScoreFunctionBuilders.weightFactorFunction(2))
                .add(QueryBuilders.matchQuery("keywords",keyword),ScoreFunctionBuilders.weightFactorFunction(2))
                .add(QueryBuilders.termQuery("brandId",brandId),ScoreFunctionBuilders.weightFactorFunction(10))
                .add(QueryBuilders.matchQuery("productCategoryId",productCategoryId),ScoreFunctionBuilders.weightFactorFunction(6))
                .scoreMode("sum")
                .setMinScore(2);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }
    return new PageImpl<>(null);
}
 
Example #19
Source File: ItemServiceImpl.java    From poseidon with Apache License 2.0 5 votes vote down vote up
@Override
public Page<EsItem> getByKey(String keyWord, int page, int limit) {
	Pageable pageable = PageRequest.of(page, limit);
	QueryBuilder queryBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name", keyWord))
			.should(QueryBuilders.matchQuery("sellPoint", keyWord));
	NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
	nativeSearchQueryBuilder.withQuery(queryBuilder).withPageable(pageable)
			.withSort(SortBuilders.fieldSort("sale").order(SortOrder.DESC));
	NativeSearchQuery query = nativeSearchQueryBuilder.build();
	LOGGER.info("DSL:{}", query.getQuery().toString());
	return elasticsearchTemplate.queryForPage(query, EsItem.class);
}
 
Example #20
Source File: EsProductServiceImpl.java    From mall with Apache License 2.0 5 votes vote down vote up
@Override
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    //搜索条件
    if(StringUtils.isEmpty(keyword)){
        builder.withQuery(QueryBuilders.matchAllQuery());
    }else{
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
    }
    //聚合搜索品牌名称
    builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
    //集合搜索分类名称
    builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
    //聚合搜索商品属性,去除type=1的属性
    AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues","attrValueList")
            .subAggregation(AggregationBuilders.filter("productAttrs",QueryBuilders.termQuery("attrValueList.type",1))
                    .subAggregation(AggregationBuilders.terms("attrIds")
                            .field("attrValueList.productAttributeId")
                            .subAggregation(AggregationBuilders.terms("attrValues")
                                    .field("attrValueList.value"))
                            .subAggregation(AggregationBuilders.terms("attrNames")
                                    .field("attrValueList.name"))));
    builder.addAggregation(aggregationBuilder);
    NativeSearchQuery searchQuery = builder.build();
    return elasticsearchTemplate.query(searchQuery, response -> {
        LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
        return convertProductRelatedInfo(response);
    });
}
 
Example #21
Source File: EsProductServiceImpl.java    From BigDataPlatform with GNU General Public License v3.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(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId),
                ScoreFunctionBuilders.weightFactorFunction(6)));
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                .setMinScore(2);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }
    return new PageImpl<>(null);
}
 
Example #22
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 #23
Source File: EsProductServiceImpl.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    //搜索条件
    if(StringUtils.isEmpty(keyword)){
        builder.withQuery(QueryBuilders.matchAllQuery());
    }else{
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
    }
    //聚合搜索品牌名称
    builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
    //集合搜索分类名称
    builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
    //聚合搜索商品属性,去除type=1的属性
    AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues","attrValueList")
            .subAggregation(AggregationBuilders.filter("productAttrs",QueryBuilders.termQuery("attrValueList.type",1))
                    .subAggregation(AggregationBuilders.terms("attrIds")
                            .field("attrValueList.productAttributeId")
                            .subAggregation(AggregationBuilders.terms("attrValues")
                                    .field("attrValueList.value"))
                            .subAggregation(AggregationBuilders.terms("attrNames")
                                    .field("attrValueList.name"))));
    builder.addAggregation(aggregationBuilder);
    NativeSearchQuery searchQuery = builder.build();
    return elasticsearchTemplate.query(searchQuery, response -> {
        LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
        return convertProductRelatedInfo(response);
    });
}
 
Example #24
Source File: EsTemplateTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Test
public void testHighlightQuery() {
    QueryVO query = new QueryVO();
    query.setKeyword("手机");
    query.setPage(1);
    query.setSize(10);

    // 复合查询
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

    // 以下为查询条件, 使用 must query 进行查询组合
    MultiMatchQueryBuilder matchQuery = QueryBuilders.multiMatchQuery(query.getKeyword(), "title", "category");
    boolQuery.must(matchQuery);

    PageRequest pageRequest = PageRequest.of(query.getPage() - 1, query.getSize());

    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withQuery(boolQuery)
            .withHighlightFields(
                    new HighlightBuilder.Field("title").preTags("<span style=\"color:#F56C6C\">").postTags("</span>"),
                    new HighlightBuilder.Field("category").preTags("<span style=\"color:#F56C6C\">").postTags("</span>"))
            .withPageable(pageRequest)
            .build();
    Page<Item> items = esTemplate.queryForPage(searchQuery, Item.class, highlightResultMapper);

    items.forEach(System.out::println);
    // Item{id=1, title='小米<span style="color:#F56C6C">手机</span>10', category='<span style="color:#F56C6C">手机</span>',
    // brand='小米', price=3499.0, images='http://image.baidu.com/13123.jpg'}
}
 
Example #25
Source File: EsProductServiceImpl.java    From macrozheng with Apache License 2.0 5 votes vote down vote up
@Override
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    //搜索条件
    if(StringUtils.isEmpty(keyword)){
        builder.withQuery(QueryBuilders.matchAllQuery());
    }else{
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
    }
    //聚合搜索品牌名称
    builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
    //集合搜索分类名称
    builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
    //聚合搜索商品属性,去除type=1的属性
    AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues","attrValueList")
            .subAggregation(AggregationBuilders.filter("productAttrs",QueryBuilders.termQuery("attrValueList.type",1))
            .subAggregation(AggregationBuilders.terms("attrIds")
                    .field("attrValueList.productAttributeId")
                    .subAggregation(AggregationBuilders.terms("attrValues")
                            .field("attrValueList.value"))
                    .subAggregation(AggregationBuilders.terms("attrNames")
                            .field("attrValueList.name"))));
    builder.addAggregation(aggregationBuilder);
    NativeSearchQuery searchQuery = builder.build();
    return elasticsearchTemplate.query(searchQuery, response -> {
        LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
        return convertProductRelatedInfo(response);
    });
}
 
Example #26
Source File: EsProductServiceImpl.java    From mall-swarm 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(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId),
                ScoreFunctionBuilders.weightFactorFunction(6)));
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                .setMinScore(2);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }
    return new PageImpl<>(null);
}
 
Example #27
Source File: EsProductServiceImpl.java    From mall-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public EsProductRelatedInfo searchRelatedInfo(String keyword) {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    //搜索条件
    if(StringUtils.isEmpty(keyword)){
        builder.withQuery(QueryBuilders.matchAllQuery());
    }else{
        builder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","subTitle","keywords"));
    }
    //聚合搜索品牌名称
    builder.addAggregation(AggregationBuilders.terms("brandNames").field("brandName"));
    //集合搜索分类名称
    builder.addAggregation(AggregationBuilders.terms("productCategoryNames").field("productCategoryName"));
    //聚合搜索商品属性,去除type=1的属性
    AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested("allAttrValues","attrValueList")
            .subAggregation(AggregationBuilders.filter("productAttrs",QueryBuilders.termQuery("attrValueList.type",1))
            .subAggregation(AggregationBuilders.terms("attrIds")
                    .field("attrValueList.productAttributeId")
                    .subAggregation(AggregationBuilders.terms("attrValues")
                            .field("attrValueList.value"))
                    .subAggregation(AggregationBuilders.terms("attrNames")
                            .field("attrValueList.name"))));
    builder.addAggregation(aggregationBuilder);
    NativeSearchQuery searchQuery = builder.build();
    return elasticsearchTemplate.query(searchQuery, response -> {
        LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
        return convertProductRelatedInfo(response);
    });
}
 
Example #28
Source File: EsProductServiceImpl.java    From macrozheng 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(10)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("productCategoryId", productCategoryId),
                ScoreFunctionBuilders.weightFactorFunction(6)));
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                .setMinScore(2);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(functionScoreQueryBuilder);
        builder.withPageable(pageable);
        NativeSearchQuery searchQuery = builder.build();
        LOGGER.info("DSL:{}", searchQuery.getQuery().toString());
        return productRepository.search(searchQuery);
    }
    return new PageImpl<>(null);
}
 
Example #29
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 #30
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);
}