Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#highlighter()
The following examples show how to use
org.elasticsearch.action.search.SearchRequestBuilder#highlighter() .
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: ElasticsearchTransportFactory.java From database-transform-tool with Apache License 2.0 | 7 votes |
public String selectMatchAll(String indexs,String types,String field,String value){ try { if(client==null){ init(); } SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(",")); request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); request.setQuery(QueryBuilders.matchQuery(field, value)); request.highlighter(new HighlightBuilder().field(field)); request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword")); request.setExplain(false); SearchResponse response = request.get(); return response.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
Example 2
Source File: ElasticsearchExtendTransportFactory.java From database-transform-tool with Apache License 2.0 | 7 votes |
public String selectTermAll(String indexs,String types,String field,String value){ try { if(client==null){ init(); } SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(",")); request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); request.setQuery(QueryBuilders.termQuery(field, value)); request.highlighter(new HighlightBuilder().field(field)); request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword")); request.setExplain(false); SearchResponse response = request.get(); return response.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
Example 3
Source File: QueryAction.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
protected void updateRequestWithHighlight(Select select, SearchRequestBuilder request) { boolean foundAnyHighlights = false; HighlightBuilder highlightBuilder = new HighlightBuilder(); for (Hint hint : select.getHints()) { if (hint.getType() == HintType.HIGHLIGHT) { HighlightBuilder.Field highlightField = parseHighlightField(hint.getParams()); if (highlightField != null) { foundAnyHighlights = true; highlightBuilder.field(highlightField); } } } if (foundAnyHighlights) { request.highlighter(highlightBuilder); } }
Example 4
Source File: SearchService.java From jakduk-api with MIT License | 6 votes |
private SearchRequestBuilder getCommentSearchRequestBuilder(String query, Integer from, Integer size, String preTags, String postTags) { HighlightBuilder highlightBuilder = new HighlightBuilder() .noMatchSize(Constants.SEARCH_NO_MATCH_SIZE) .fragmentSize(Constants.SEARCH_FRAGMENT_SIZE) .field("content", Constants.SEARCH_FRAGMENT_SIZE, 1); SearchRequestBuilder searchRequestBuilder = client.prepareSearch() .setIndices(elasticsearchProperties.getIndexBoard()) .setTypes(Constants.ES_TYPE_COMMENT) .setFetchSource(null, new String[]{"content"}) .setQuery( QueryBuilders.boolQuery() .must(QueryBuilders.matchQuery("content", query)) .must(JoinQueryBuilders .hasParentQuery(Constants.ES_TYPE_ARTICLE, QueryBuilders.matchAllQuery(), false) .innerHit(new InnerHitBuilder()) ) ) .setFrom(from) .setSize(size); if (StringUtils.isNotBlank(preTags)) highlightBuilder.preTags(preTags); if (StringUtils.isNotBlank(postTags)) highlightBuilder.postTags(postTags); searchRequestBuilder.highlighter(highlightBuilder); log.debug("getBoardCommentSearchRequestBuilder Query:\n{}", searchRequestBuilder); return searchRequestBuilder; }
Example 5
Source File: ElasticsearchUtil.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 使用分词查询 排序 高亮 * @param index 索引名称 * @param type 类型名称,可传入多个type逗号分隔 * @param query 查询条件 * @param size 文档大小限制 * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) * @param sortField 排序字段 * @param highlightField 高亮字段 * @return 结果 */ public static List<Map<String, Object>> searchListData(String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index); if (StringUtils.isNotEmpty(type)) { searchRequestBuilder.setTypes(type.split(",")); } if (StringUtils.isNotEmpty(highlightField)) { HighlightBuilder highlightBuilder = new HighlightBuilder(); // 设置高亮字段 highlightBuilder.field(highlightField); searchRequestBuilder.highlighter(highlightBuilder); } searchRequestBuilder.setQuery(query); if (StringUtils.isNotEmpty(fields)) { searchRequestBuilder.setFetchSource(fields.split(","), null); } searchRequestBuilder.setFetchSource(true); if (StringUtils.isNotEmpty(sortField)) { searchRequestBuilder.addSort(sortField, SortOrder.ASC); } if (size != null && size > 0) { searchRequestBuilder.setSize(size); }//打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询 log.info("\n{}", searchRequestBuilder); SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); long totalHits = searchResponse.getHits().totalHits; long length = searchResponse.getHits().getHits().length; log.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length); if (searchResponse.status().getStatus() == 200) { // 解析对象 return setSearchResponse(searchResponse, highlightField); } return null; }
Example 6
Source File: SearchService.java From jakduk-api with MIT License | 5 votes |
private SearchRequestBuilder getArticleSearchRequestBuilder(String query, Integer from, Integer size, String preTags, String postTags) { HighlightBuilder highlightBuilder = new HighlightBuilder() .noMatchSize(Constants.SEARCH_NO_MATCH_SIZE) .fragmentSize(Constants.SEARCH_FRAGMENT_SIZE) .field("subject", Constants.SEARCH_FRAGMENT_SIZE, 0) .field("content", Constants.SEARCH_FRAGMENT_SIZE, 1); SearchRequestBuilder searchRequestBuilder = client.prepareSearch() .setIndices(elasticsearchProperties.getIndexBoard()) .setTypes(Constants.ES_TYPE_ARTICLE) .setFetchSource(null, new String[]{"subject", "content"}) .setQuery( QueryBuilders.boolQuery() .should(QueryBuilders.multiMatchQuery(query, "subject", "content").field("subject", 1.5f)) ) .setFrom(from) .setSize(size); if (StringUtils.isNotBlank(preTags)) highlightBuilder.preTags(preTags); if (StringUtils.isNotBlank(postTags)) highlightBuilder.postTags(postTags); searchRequestBuilder.highlighter(highlightBuilder); log.debug("getArticleSearchRequestBuilder Query:\n{}", searchRequestBuilder); return searchRequestBuilder; }
Example 7
Source File: SearchService.java From jakduk-api with MIT License | 5 votes |
private SearchRequestBuilder getGallerySearchRequestBuilder(String query, Integer from, Integer size, String preTags, String postTags) { HighlightBuilder highlightBuilder = new HighlightBuilder() .noMatchSize(Constants.SEARCH_NO_MATCH_SIZE) .fragmentSize(Constants.SEARCH_FRAGMENT_SIZE) .field("name", Constants.SEARCH_FRAGMENT_SIZE, 0); SearchRequestBuilder searchRequestBuilder = client.prepareSearch() .setIndices(elasticsearchProperties.getIndexGallery()) .setTypes(Constants.ES_TYPE_GALLERY) .setFetchSource(null, new String[]{"name"}) .setQuery(QueryBuilders.matchQuery("name", query)) .setFrom(from) .setSize(size); if (StringUtils.isNotBlank(preTags)) highlightBuilder.preTags(preTags); if (StringUtils.isNotBlank(postTags)) highlightBuilder.postTags(postTags); searchRequestBuilder.highlighter(highlightBuilder); log.debug("getGallerySearchRequestBuilder Query:\n{}", searchRequestBuilder); return searchRequestBuilder; }