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

The following examples show how to use org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder#withHighlightFields() . 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: EkmKnowledgeMasterRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Page<EkmKnowledgeMaster> processQuery(BoolQueryBuilder boolQueryBuilder, Pageable page){
	
	//过滤掉已过期的知识
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").to(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").from(new Date().getTime())) ;
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
	searchQueryBuilder.withHighlightFields(new Field("title") , new Field("content")) ;
	
	searchQueryBuilder.withPageable(page) ;
	
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class , new EKMResultMapper()) ;
	}
	
	return knowledgeList;
}
 
Example 2
Source File: EkmKnowledgeMasterRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Page<EkmKnowledgeMaster> processQueryItem(BoolQueryBuilder boolQueryBuilder, Pageable page){
	
	//过滤掉已过期的知识
	//QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").to(new Date().getTime())) ;
	//QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").from(new Date().getTime())) ;
	
	NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
	searchQueryBuilder.withHighlightFields(new Field("title") , new Field("content")) ;
	
	searchQueryBuilder.withPageable(page) ;
	
	Page<EkmKnowledgeMaster> knowledgeList = null ;
	if(elasticsearchTemplate.indexExists(EkmKnowledgeMaster.class)){
		knowledgeList = elasticsearchTemplate.queryForPage(searchQueryBuilder.build() , EkmKnowledgeMaster.class , new EKMResultMapper()) ;
	}
	
	return knowledgeList;
}
 
Example 3
Source File: TopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<Topic> getTopicByCateAndOrgi(String cate ,String orgi, String q, final int p , final int ps) {

	Page<Topic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	if(!StringUtils.isBlank(cate)) {
		boolQueryBuilder.must(termQuery("cate" , cate)) ;
	}
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(Topic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new XiaoEUKResultMapper());
    }
    return pages ; 
}
 
Example 4
Source File: TopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<Topic> getTopicByTopAndOrgi(boolean top ,String orgi, String aiid ,final int p , final int ps) {

	Page<Topic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("top" , top)) ;
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	if(!StringUtils.isBlank(aiid)) {
		boolQueryBuilder.must(termQuery("aiid" , aiid)) ;
	}
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").to(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").from(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
   
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(Topic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class , new XiaoEUKResultMapper());
    }
    return pages ; 
}
 
Example 5
Source File: QuickReplyRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<QuickReply> getByOrgiAndCate(String orgi ,String cate , String q, Pageable page) {

	Page<QuickReply> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("cate" , cate)) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example 6
Source File: QuickReplyRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<QuickReply> getByQuicktype(String quicktype , final int p , final int ps) {

	Page<QuickReply> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("type" , quicktype)) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example 7
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public Page<KbsTopic> getTopicByCate(String cate , String q, final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("cate" , cate)) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example 8
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<KbsTopic> getTopicByTop(boolean top , final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("top" , top)) ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class , new UKResultMapper());
    }
    return pages ; 
}
 
Example 9
Source File: KbsTopicCommentRepositoryImpl.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@Override
public Page<KbsTopicComment> findByCon(
		NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
	searchQueryBuilder.withPageable(new PageRequest(p, ps)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)) ;
	searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("content").fragmentSize(200)) ;
	if(!StringUtils.isBlank(q)){
	   	searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
	}
    return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class  , new UKResultMapper()) ;
}
 
Example 10
Source File: QuickReplyRepositoryImpl.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@Override
public List<QuickReply> findByOrgiAndCreater(String orgi ,String creater , String q) {

	List<QuickReply> pages  = null ;
	
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
	
	BoolQueryBuilder quickQueryBuilder = QueryBuilders.boolQuery();
	
	quickQueryBuilder.should(termQuery("type" , UKDataContext.QuickTypeEnum.PUB.toString())) ;
	
	BoolQueryBuilder priQueryBuilder = QueryBuilders.boolQuery();
	
	priQueryBuilder.must(termQuery("type" , UKDataContext.QuickTypeEnum.PRI.toString())) ;
	priQueryBuilder.must(termQuery("creater" , creater)) ;
	
	quickQueryBuilder.should(priQueryBuilder) ;
	
	boolQueryBuilder.must(quickQueryBuilder) ;
	
    if(!StringUtils.isBlank(q)){
    	boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND)) ;
    }
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200)) ;
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(0, 10000)) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
    }
    return pages ; 
}
 
Example 11
Source File: ElasticSearchService.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> search(String keywords, Integer currentPage, Integer pageSize) {
    currentPage = Math.max(currentPage - 1, 0);

    List<HighlightBuilder.Field> highlightFields = new ArrayList<>();

    HighlightBuilder.Field titleField = new HighlightBuilder.Field(SysConf.TITLE).preTags("<span style='color:red'>").postTags("</span>");
    HighlightBuilder.Field summaryField = new HighlightBuilder.Field(SysConf.SUMMARY).preTags("<span style='color:red'>").postTags("</span>");
    highlightFields.add(titleField);
    highlightFields.add(summaryField);

    HighlightBuilder.Field[] highlightFieldsAry = highlightFields.toArray(new HighlightBuilder.Field[highlightFields.size()]);
    //创建查询构造器
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

    queryBuilder.withPageable(PageRequest.of(currentPage, pageSize));

    //过滤
    QueryStringQueryBuilder queryStrBuilder = new QueryStringQueryBuilder(keywords);
    queryStrBuilder.field("title").field("summary");

    queryBuilder.withQuery(queryStrBuilder);

    queryBuilder.withHighlightFields(highlightFieldsAry);

    log.error("查询语句:{}", queryBuilder.build().getQuery().toString());

    //查询
    AggregatedPage<ESBlogIndex> result = elasticsearchTemplate.queryForPage(queryBuilder.build(), ESBlogIndex.class, highlightResultHelper);

    //解析结果
    long total = result.getTotalElements();
    int totalPage = result.getTotalPages();
    List<ESBlogIndex> blogList = result.getContent();
    Map<String, Object> map = new HashMap<>();
    map.put(SysConf.TOTAL, total);
    map.put(SysConf.TOTAL_PAGE, totalPage);
    map.put(SysConf.PAGE_SIZE, pageSize);
    map.put(SysConf.CURRENT_PAGE, currentPage + 1);
    map.put(SysConf.BLOG_LIST, blogList);
    return map;
}
 
Example 12
Source File: ApiCourseBiz.java    From roncoo-education with MIT License 4 votes vote down vote up
/**
 * 课程搜索列表接口
 *
 * @param courseInfoSearchBO
 * @author wuyun
 */
public Result<Page<CourseInfoSearchPageDTO>> searchList(CourseInfoSearchBO bo) {
	if (StringUtils.isEmpty(bo.getOrgNo())) {
		return Result.error("orgNo不能为空");
	}
	if (bo.getPageCurrent() <= 0) {
		bo.setPageCurrent(1);
	}
	if (bo.getPageSize() <= 0) {
		bo.setPageSize(20);
	}

	if (StringUtils.isEmpty(bo.getCourseName())) {
		return Result.success(new Page<CourseInfoSearchPageDTO>());
	}

	String heightField = "courseName";

	Field hfield = null;
	if (bo.getIsHfield() != null && bo.getIsHfield().equals(IsHfield.YES.getCode())) {
		hfield = new HighlightBuilder.Field(heightField).preTags("<mark>").postTags("</mark>");
	}

	NativeSearchQueryBuilder nsb = new NativeSearchQueryBuilder();
	if (bo.getIsHfield() != null && bo.getIsHfield().equals(IsHfield.YES.getCode())) {
		nsb.withHighlightFields(hfield);// 高亮字段
	}
	nsb.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));// 评分排序(_source)
	nsb.withSort(new FieldSortBuilder("courseSort").order(SortOrder.DESC));// 课程排序(courseSort)
	nsb.withPageable(PageRequest.of(bo.getPageCurrent() - 1, bo.getPageSize()));
	// 复合查询,外套boolQuery
	BoolQueryBuilder qb = QueryBuilders.boolQuery();
	// 精确查询termQuery不分词,must参数等价于AND
	qb.must(QueryBuilders.termQuery("orgNo", bo.getOrgNo()));
	// 模糊查询multiMatchQuery,最佳字段best_fields
	qb.must(QueryBuilders.multiMatchQuery(bo.getCourseName(), "courseName", "lecturerName").type(MultiMatchQueryBuilder.Type.BEST_FIELDS));

	nsb.withQuery(qb);

	org.springframework.data.domain.Page<EsCourse> page = elasticsearchTemplate.queryForPage(nsb.build(), EsCourse.class, resultMapperExt);
	return Result.success(EsPageUtil.transform(page, CourseInfoSearchPageDTO.class));
}
 
Example 13
Source File: EsServiceImpl.java    From mall with Apache License 2.0 2 votes vote down vote up
/**
 * 设置高亮
 *
 * @param nativeSearchQueryBuilder 查询主体
 */
private void setHighlighted(NativeSearchQueryBuilder nativeSearchQueryBuilder) {
    nativeSearchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("spuName").preTags("<span class='red'>").postTags("</span>"));
}