org.elasticsearch.search.highlight.HighlightBuilder Java Examples

The following examples show how to use org.elasticsearch.search.highlight.HighlightBuilder. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: Queries.java    From klask-io with GNU General Public License v3.0 6 votes vote down vote up
/**
 * construct a searchQuery with a number of fragments to highlight per results.
 *
 * @param query
 * @param p
 * @param numberOfFragments
 * @return
 */
public static NativeSearchQueryBuilder constructQueryWithHighlight(String query, Pageable p, int numberOfFragments) {
    return new NativeSearchQueryBuilder()
        //.withQuery(termQuery("content", query))
        .withQuery(queryBuilder(query))
        //.withFields("content", "name")
        .withPageable(p)
        //exclu le content de la recherche pour alléger les requêtes
        .withSourceFilter(
            new FetchSourceFilterBuilder()
                .withExcludes("content")
                .build()
        )

        .withHighlightFields(
            new HighlightBuilder.Field("content")
                .preTags("<mark>")
                .postTags("</mark>")

                .numOfFragments(numberOfFragments)
                .fragmentSize(150)
        );
}
 
Example #8
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 #9
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 #10
Source File: SearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
private HighlightBuilder highlightBuilder() {
    return sourceBuilder().highlighter();
}
 
Example #11
Source File: SearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public SearchRequestBuilder<JsonInput, JsonOutput> highlight(HighlightBuilder highlightBuilder) {
    sourceBuilder().highlight(highlightBuilder);
    return this;
}
 
Example #12
Source File: SearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public SearchRequestBuilder<JsonInput, JsonOutput> highlightedField(HighlightBuilder.Field field) {
    highlightBuilder().field(field);
    return this;
}
 
Example #13
Source File: SearchRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private HighlightBuilder highlightBuilder() {
    return sourceBuilder().highlighter();
}
 
Example #14
Source File: SearchRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a highlighted field.
 */
public SearchRequestBuilder addHighlightedField(HighlightBuilder.Field field) {
    highlightBuilder().field(field);
    return this;
}
 
Example #15
Source File: PercolateSourceBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Enables highlighting for the percolate document. Per matched percolate query highlight the percolate document.
 */
public PercolateSourceBuilder setHighlightBuilder(HighlightBuilder highlightBuilder) {
    this.highlightBuilder = highlightBuilder;
    return this;
}
 
Example #16
Source File: PercolateRequestBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates to {@link PercolateSourceBuilder#setHighlightBuilder(HighlightBuilder)}
 */
public PercolateRequestBuilder setHighlightBuilder(HighlightBuilder highlightBuilder) {
    sourceBuilder().setHighlightBuilder(highlightBuilder);
    return this;
}
 
Example #17
Source File: BaseInnerHitBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a highlighted field.
 */
public T addHighlightedField(HighlightBuilder.Field field) {
    highlightBuilder().field(field);
    return (T) this;
}
 
Example #18
Source File: BaseInnerHitBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public HighlightBuilder highlightBuilder() {
    return sourceBuilder().highlighter();
}
 
Example #19
Source File: SearchSourceBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Adds highlight to perform as part of the search.
 */
public SearchSourceBuilder highlight(HighlightBuilder highlightBuilder) {
    this.highlightBuilder = highlightBuilder;
    return this;
}
 
Example #20
Source File: SearchSourceBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * A static factory method to construct new search highlights.
 */
public static HighlightBuilder highlight() {
    return new HighlightBuilder();
}
 
Example #21
Source File: TopHitsBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public HighlightBuilder highlightBuilder() {
    return sourceBuilder().highlighter();
}
 
Example #22
Source File: TopHitsBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a highlighted field.
 */
public TopHitsBuilder addHighlightedField(HighlightBuilder.Field field) {
    highlightBuilder().field(field);
    return this;
}