org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder Java Examples
The following examples show how to use
org.elasticsearch.search.fetch.subphase.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: 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 #2
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 #3
Source File: ElasticSqlParseResult.java From elasticsearch-sql with MIT License | 6 votes |
private SearchRequest toRequest() { SearchRequest searchRequest = new SearchRequest(); List<String> indexList = indices.parallelStream().map(StringManager::removeStringSymbol).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(indexList)) { searchRequest.indices(indexList.toArray(new String[0])); } else { throw new ElasticSql2DslException("[syntax error] indices name must be set"); } //这里不会修改from的值 searchSourceBuilder.from(Math.max(from, 0)); searchSourceBuilder.size(Math.max(size, 0)); searchSourceBuilder.trackTotalHits(this.trackTotalHits); if (CollectionUtils.isNotEmpty(highlighter)) { HighlightBuilder highlightBuilder = HighlightBuilders.highlighter(highlighter); searchSourceBuilder.highlighter(highlightBuilder); } searchSourceBuilder.query(whereCondition); if (StringUtils.isNotBlank(distinctName)) { searchSourceBuilder.collapse(new CollapseBuilder(distinctName)); } if (CollectionUtils.isNotEmpty(orderBy)) { for (SortBuilder sortBuilder : orderBy) { searchSourceBuilder.sort(sortBuilder); } } searchSourceBuilder.fetchSource(includeFields.toArray(new String[0]), excludeFields.toArray(new String[0])); if (CollectionUtils.isNotEmpty(routingBy)) { searchRequest.routing(routingBy.toArray(new String[0])); } if (CollectionUtils.isNotEmpty(groupBy)) { for (AggregationBuilder aggItem : groupBy) { searchSourceBuilder.aggregation(aggItem); } } return searchRequest.source(searchSourceBuilder); }
Example #4
Source File: ElasticsearchHelperService.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * 增加高亮词 * * @param keywords * @param highlightBuilder */ private void AddHighLigh(JSONObject keywords, HighlightBuilder highlightBuilder) { if (keywords == null || keywords.size() <= 0) { return; } Iterator<Map.Entry<String, Object>> iterator = keywords.entrySet().iterator(); if (iterator == null) { return; } while (iterator.hasNext()) { Map.Entry<String, Object> next = iterator.next(); String key = next.getKey(); Object value = next.getValue(); if (value == null || value.toString().length() <= 0) { continue; } highlightBuilder.preTags("<span style=color:red>"); highlightBuilder.postTags("</span>"); highlightBuilder.field(key); } }
Example #5
Source File: ElasticsearchQuery.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Highlights the given field or all fields if null. */ @Override public void highlight(IRI property) { String field = (property != null) ? ElasticsearchIndex.toPropertyFieldName(SearchFields.getPropertyField(property)) : ElasticsearchIndex.ALL_PROPERTY_FIELDS; HighlightBuilder hb = new HighlightBuilder(); hb.field(field); hb.preTags(SearchFields.HIGHLIGHTER_PRE_TAG); hb.postTags(SearchFields.HIGHLIGHTER_POST_TAG); // Elastic Search doesn't really have the same support for fragments as Lucene. // So, we have to get back the whole highlighted value (comma-separated if it is a list) // and then post-process it into fragments ourselves. hb.numOfFragments(0); request.highlighter(hb); }
Example #6
Source File: SearchHitParser.java From sql4es with Apache License 2.0 | 6 votes |
/** * Adds _id, _index and/or _type to the current row // * @param idIndexTypeScore = String[]{_id, _index, _type} * @param head * @param row */ private void addIdIndexAndType(String id, String index, String type, Float score, Map<String, HighlightBuilder.Field> highlights, Heading head, List<Object> row){ if(id != null && head.hasAllCols() || head.hasLabel(Heading.ID)){ row.set( head.getColumnByLabel(Heading.ID).getIndex(), id); } if(index != null && head.hasAllCols() || head.hasLabel(Heading.INDEX)){ row.set( head.getColumnByLabel(Heading.INDEX).getIndex(), index); } if(type != null && head.hasAllCols() || head.hasLabel(Heading.TYPE)){ row.set( head.getColumnByLabel(Heading.TYPE).getIndex(), type); } if(score != null && head.hasLabel(Heading.SCORE)){ row.set( head.getColumnByLabel(Heading.SCORE).getIndex(), score); } /* if(highlights != null){ for(String field : highlights.keySet()){ Column col = head.getColumnByNameAndOp(field, Operation.HIGHLIGHT); if(col == null) continue; List<Object> fragments = new ArrayList<Object>(); for(Text fragment : highlights.get(field).getFragments()) fragments.add(fragment.toString()); row.set(col.getIndex(), new ESArray(fragments)); } }*/ }
Example #7
Source File: ESSearchTest.java From summerframework with Apache License 2.0 | 6 votes |
@Test public void highLightResultSet() { HighLight highLight = new HighLight(); HighlightBuilder hBuilder = new HighlightBuilder(); hBuilder.preTags("<h2>"); hBuilder.postTags("</h2>"); hBuilder.field("productName"); queryPair.setFieldNames(new String[] {"productName"}); queryPair.setContent("*"); queryCondition.setSearchType(SearchType.QUERY_THEN_FETCH); queryCondition .setQueryBuilder(QueryBuilders.wildcardQuery(queryPair.getFieldNames()[0], queryPair.getContent())); highLight.setBuilder(hBuilder); highLight.setField("productName"); List<Map<String, Object>> sourceList = elasticsearchTemplate.highLightResultSet(esBasicInfo, queryCondition, highLight); assertThat(sourceList.size(), is(3)); assertThat((String)sourceList.get(0).get("productName"), containsString("运")); assertThat((String)sourceList.get(1).get("productName"), containsString("android")); assertThat((String)sourceList.get(2).get("productName"), containsString("华")); }
Example #8
Source File: ESSearchTest.java From summerframework with Apache License 2.0 | 6 votes |
@Test public void highLightResultSet() { HighLight highLight = new HighLight(); HighlightBuilder hBuilder = new HighlightBuilder(); hBuilder.preTags("<h2>"); hBuilder.postTags("</h2>"); hBuilder.field("productName"); queryPair.setFieldNames(new String[] {"productName"}); queryPair.setContent("*"); queryCondition.setSearchType(SearchType.QUERY_THEN_FETCH); queryCondition .setQueryBuilder(QueryBuilders.wildcardQuery(queryPair.getFieldNames()[0], queryPair.getContent())); highLight.setBuilder(hBuilder); highLight.setField("productName"); List<Map<String, Object>> sourceList = elasticsearchTemplate.highLightResultSet(esBasicInfo, queryCondition, highLight); assertThat(sourceList.size(), is(3)); assertThat((String)sourceList.get(0).get("productName"), containsString("运")); assertThat((String)sourceList.get(1).get("productName"), containsString("android")); assertThat((String)sourceList.get(2).get("productName"), containsString("华")); }
Example #9
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 #10
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 #11
Source File: FessEsClient.java From fess with Apache License 2.0 | 6 votes |
protected void buildHighlighter(final QueryHelper queryHelper, final FessConfig fessConfig) { final String highlighterType = highlightInfo.getType(); final int fragmentSize = highlightInfo.getFragmentSize(); final int numOfFragments = highlightInfo.getNumOfFragments(); final int fragmentOffset = highlightInfo.getFragmentOffset(); final char[] boundaryChars = fessConfig.getQueryHighlightBoundaryCharsAsArray(); final int boundaryMaxScan = fessConfig.getQueryHighlightBoundaryMaxScanAsInteger(); final String boundaryScannerType = fessConfig.getQueryHighlightBoundaryScanner(); final boolean forceSource = fessConfig.isQueryHighlightForceSource(); final String fragmenter = fessConfig.getQueryHighlightFragmenter(); final int noMatchSize = fessConfig.getQueryHighlightNoMatchSizeAsInteger(); final String order = fessConfig.getQueryHighlightOrder(); final int phraseLimit = fessConfig.getQueryHighlightPhraseLimitAsInteger(); final String encoder = fessConfig.getQueryHighlightEncoder(); final HighlightBuilder highlightBuilder = new HighlightBuilder(); queryHelper.highlightedFields(stream -> stream.forEach(hf -> highlightBuilder.field( new HighlightBuilder.Field(hf).highlighterType(highlighterType).fragmentSize(fragmentSize) .numOfFragments(numOfFragments).boundaryChars(boundaryChars).boundaryMaxScan(boundaryMaxScan) .boundaryScannerType(boundaryScannerType).forceSource(forceSource).fragmenter(fragmenter) .fragmentOffset(fragmentOffset).noMatchSize(noMatchSize).order(order).phraseLimit(phraseLimit)) .encoder(encoder))); searchRequestBuilder.highlighter(highlightBuilder); }
Example #12
Source File: ClusteringActionIT.java From elasticsearch-carrot2 with Apache License 2.0 | 6 votes |
public void testComplexQuery() throws IOException { ClusteringActionResponse result = new ClusteringActionRequestBuilder(client) .setQueryHint("data mining") .addSourceFieldMapping("title", LogicalField.TITLE) .addHighlightedFieldMapping("content", LogicalField.CONTENT) .setDefaultLanguage("English") .setSearchRequest( client.prepareSearch() .setIndices(INDEX_TEST) .setSize(100) .setQuery(QueryBuilders.termQuery("content", "data")) .highlighter(new HighlightBuilder().preTags("").postTags("")) .setFetchSource(new String[] {"title"}, null) .highlighter(new HighlightBuilder().field("content"))) .execute().actionGet(); checkValid(result); checkJsonSerialization(result); }
Example #13
Source File: IndexSearchDaoTest.java From herd with Apache License 2.0 | 5 votes |
private void verifyHitHighlightingInteractions(SearchRequestBuilder searchRequestBuilder, boolean isHitHighlightingEnabled, Set<String> match) throws IOException { if (isHitHighlightingEnabled) { // verify interactions with the helpers which is required to fetch highlighting config verify(jsonHelper, times(5)).unmarshallJsonToObject(eq(Map.class), any(String.class)); verify(jsonHelper).unmarshallJsonToObject(eq(IndexSearchHighlightFields.class), any(String.class)); verify(configurationHelper, times(3)).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_POSTTAGS); verify(configurationHelper, times(3)).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_PRETAGS); if (match != null && match.contains(MATCH_COLUMN)) { verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_COLUMN_MATCH_HIGHLIGHT_FIELDS); } else { verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_FIELDS); } } else { // verify highlighting is not added to the query if not highlighting is disabled verify(searchRequestBuilder, times(0)).highlighter(any(HighlightBuilder.class)); // verify highlighting-specific configuration values are not fetched if highlighting is disabled verify(configurationHelper, times(0)).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_POSTTAGS); verify(configurationHelper, times(0)).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_PRETAGS); verify(configurationHelper, times(0)).getProperty(ConfigurationValue.ELASTICSEARCH_HIGHLIGHT_FIELDS); } }
Example #14
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 #15
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 #16
Source File: URLTokenizerIntegrationTest.java From elasticsearch-analysis-url with Apache License 2.0 | 5 votes |
@Test public void testHighlight() throws Exception { final String field = "url_highlight_test"; Map<String, String> docContent = new HashMap<>(); final String url = "http://www.foo.bar.com:8080/baz/bat?bob=blah"; docContent.put(field, url); client().prepareIndex(INDEX, TYPE).setSource(docContent).get(); refresh(INDEX); SearchResponse response = client().prepareSearch(INDEX).setQuery(QueryBuilders.matchQuery(field, "www.foo.bar.com:8080")) .highlighter(new HighlightBuilder().preTags("<b>").postTags("</b>").field("*").forceSource(true)) .get(); SearchHit[] hits = response.getHits().getHits(); assertThat(hits.length, equalTo(1)); SearchHit hit = hits[0]; Map<String, Object> source = hit.getSource(); assertThat(source.size(), equalTo(1)); assertThat(source, hasKey(field)); assertThat("URL was stored correctly", source.get(field), equalTo(url)); assertThat(hit.highlightFields(), hasKey(field)); HighlightField highlightField = hit.highlightFields().get(field); Text[] fragments = highlightField.getFragments(); assertThat(fragments.length, equalTo(1)); Text fragment = fragments[0]; assertThat("URL was highlighted correctly", fragment.string(), equalTo("http://<b>www.foo.bar.com</b>:<b>8080</b>/baz/bat?bob=blah")); }
Example #17
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; }
Example #18
Source File: ElasticQueryBuilder.java From vind with Apache License 2.0 | 5 votes |
public static SearchSourceBuilder buildExperimentalSuggestionQuery( ExecutableSuggestionSearch search, DocumentFactory factory) { final String searchContext = search.getSearchContext(); final SearchSourceBuilder searchSource = new SearchSourceBuilder(); final BoolQueryBuilder baseQuery = QueryBuilders.boolQuery(); final String[] suggestionFieldNames = Stream.of(getSuggestionFieldNames(search, factory, searchContext)) .map(name -> name.concat("_experimental")) .toArray(String[]::new); final MultiMatchQueryBuilder suggestionQuery = QueryBuilders .multiMatchQuery(search.getInput(),suggestionFieldNames) .type(MultiMatchQueryBuilder.Type.BEST_FIELDS) .operator(Operator.OR); baseQuery.must(suggestionQuery); // if(search.getTimeZone() != null) { // query.set(CommonParams.TZ,search.getTimeZone()); // } baseQuery.filter(buildFilterQuery(search.getFilter(), factory, searchContext)); searchSource.query(baseQuery); final HighlightBuilder highlighter = new HighlightBuilder().numOfFragments(0); Stream.of(suggestionFieldNames) .forEach(highlighter::field); searchSource.highlighter(highlighter); searchSource.trackScores(SearchConfiguration.get(SearchConfiguration.SEARCH_RESULT_SHOW_SCORE, true)); searchSource.fetchSource(true); //TODO if nested document search is implemented return searchSource; }
Example #19
Source File: SearchServiceInternalImpl.java From studio with GNU General Public License v3.0 | 5 votes |
/** * Adds the configured highlighting to the given builder * @param builder the search builder to update */ protected void updateHighlighting(SearchSourceBuilder builder) { HighlightBuilder highlight = SearchSourceBuilder.highlight(); for (String field : highlightFields) { highlight.field(field, snippetSize, numberOfSnippets); } builder.highlighter(highlight); }
Example #20
Source File: HighlightHelper.java From fast-elasticsearch-query-builder with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static void set(SearchSourceBuilder searchSource, Annotation annotation, Object value) { Highlighters highlighters = (Highlighters) annotation; HighlightBuilder highlight = new HighlightBuilder(); String type = highlighters.type().trim(); if (type.length() > 0) { highlight.highlighterType(type); } ((Collection<String>) value).forEach(highlight::field); searchSource.highlighter(highlight); }
Example #21
Source File: ElasticsearchUtil.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 深度排序 分页 从当前页为1001开始 * @Author lihaodong * @Description * @Date 20:18 2018/12/21 * @Param [indexName, esType, startPage, pageSize, highlightField] * @return com.li.elasticsearch.EsPage **/ public static EsPage deepPageing(String indexName, String esType, int startPage, int pageSize, String highlightField) { System.out.println("scroll 模式启动!"); long begin = System.currentTimeMillis(); //初始化查询,获取scrollId BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchQuery("name", "名")); // boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").from("1").to("999821")); SearchResponse response = client.prepareSearch(indexName)//对应索引 .setTypes(esType)//对应索引type .setQuery(boolQueryBuilder) .addSort("price", SortOrder.ASC) .setScroll(TimeValue.timeValueMinutes(1)) .setSize(10000) //第一次不返回size条数据 .highlighter(new HighlightBuilder().preTags("<span style='color:red' >").postTags("</span>").field(highlightField)) .setExplain(true) .execute() .actionGet(); long totalHits = response.getHits().totalHits; List<Map<String, Object>> result = disposeScrollResult(response, highlightField); List<Map<String, Object>> sourceList = result.stream().parallel().skip((startPage - 1-(10000/pageSize)) * pageSize).limit(pageSize).collect(Collectors.toList()); long end = System.currentTimeMillis(); System.out.println("耗时: " + (end - begin) + "ms"); System.out.println("耗时: " + (end - begin) / 1000 + "s"); System.out.println("查询"+totalHits+"条数据"); return new EsPage(startPage, pageSize, (int) totalHits, sourceList); }
Example #22
Source File: ElasticsearchTests.java From MyCommunity with Apache License 2.0 | 5 votes |
@Test public void testSearchByRepository() { // 构造搜索条件 SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content")) .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)) .withPageable(PageRequest.of(0, 10)) .withHighlightFields( // 查到的部分高亮 new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"), new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>") ).build(); // elasticTemplate.queryForPage(searchQuery, class, SearchResultMapper) // 底层获取得到了高亮显示的值, 但是没有返回. Page<DiscussPost> page = discussRepository.search(searchQuery); System.out.println(page.getTotalElements()); // 返回的一共有多少条数据 System.out.println(page.getTotalPages()); // 一共分为多少页 System.out.println(page.getNumber()); // 当前处在哪一页 System.out.println(page.getSize()); // 每页有多少条数据 for (DiscussPost post : page) { System.out.println(post); } }
Example #23
Source File: SearchBuilder.java From microservices-platform with Apache License 2.0 | 5 votes |
/** * 设置高亮 * @param preTags 高亮处理前缀 * @param postTags 高亮处理后缀 */ public SearchBuilder setHighlight(String field, String preTags, String postTags) { if (StrUtil.isNotEmpty(field) && StrUtil.isNotEmpty(preTags) && StrUtil.isNotEmpty(postTags)) { HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field(field) .preTags(preTags) .postTags(postTags); searchBuilder.highlighter(highlightBuilder); } return this; }
Example #24
Source File: EsTemplateTest.java From code with Apache License 2.0 | 5 votes |
@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: MultithreadedClusteringIT.java From elasticsearch-carrot2 with Apache License 2.0 | 4 votes |
public void testRequestFlood() throws Exception { final Client client = client(); List<Callable<ClusteringActionResponse>> tasks = new ArrayList<>(); final int requests = 100; final int threads = 10; logger.debug("Stress testing: " + client.getClass().getSimpleName() + "| "); for (int i = 0; i < requests; i++) { tasks.add(() -> { logger.debug(">"); ClusteringActionResponse result = new ClusteringActionRequestBuilder(client) .setQueryHint("data mining") .addFieldMapping("title", LogicalField.TITLE) .addHighlightedFieldMapping("content", LogicalField.CONTENT) .setSearchRequest( client.prepareSearch() .setIndices(INDEX_TEST) .setTypes("test") .setSize(100) .setQuery(QueryBuilders.termQuery("content", "data")) .highlighter(new HighlightBuilder().preTags("").postTags("").field("content")) .storedFields("title")) .execute().actionGet(); logger.debug("<"); checkValid(result); checkJsonSerialization(result); return result; }); } ExecutorService executor = Executors.newFixedThreadPool(threads); try { for (Future<ClusteringActionResponse> future : executor.invokeAll(tasks)) { ClusteringActionResponse response = future.get(); Assertions.assertThat(response).isNotNull(); Assertions.assertThat(response.getSearchResponse()).isNotNull(); } } finally { executor.shutdown(); logger.debug("Done."); } }
Example #26
Source File: ElasticsearchServiceImpl.java From MyCommunity with Apache License 2.0 | 4 votes |
public Page<DiscussPost> searchDiscussPost(String keyword, int current, int limit) { SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "content")) .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC)) .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC)) .withPageable(PageRequest.of(current, limit)) .withHighlightFields( new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"), new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>") ).build(); return elasticTemplate.queryForPage(searchQuery, DiscussPost.class, new SearchResultMapper() { @Override public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) { SearchHits hits = response.getHits(); if (hits.getTotalHits() <= 0) { return null; } List<DiscussPost> list = new ArrayList<>(); for (SearchHit hit : hits) { DiscussPost post = new DiscussPost(); String id = hit.getSourceAsMap().get("id").toString(); post.setId(Integer.valueOf(id)); String userId = hit.getSourceAsMap().get("userId").toString(); post.setUserId(Integer.valueOf(userId)); String title = hit.getSourceAsMap().get("title").toString(); post.setTitle(title); String content = hit.getSourceAsMap().get("content").toString(); post.setContent(content); String status = hit.getSourceAsMap().get("status").toString(); post.setStatus(Integer.valueOf(status)); String createTime = hit.getSourceAsMap().get("createTime").toString(); post.setCreateTime(new Date(Long.valueOf(createTime))); String commentCount = hit.getSourceAsMap().get("commentCount").toString(); post.setCommentCount(Integer.valueOf(commentCount)); // 处理高亮显示的结果 HighlightField titleField = hit.getHighlightFields().get("title"); if (titleField != null) { post.setTitle(titleField.getFragments()[0].toString()); } HighlightField contentField = hit.getHighlightFields().get("content"); if (contentField != null) { post.setContent(contentField.getFragments()[0].toString()); } list.add(post); } return new AggregatedPageImpl(list, pageable, hits.getTotalHits(), response.getAggregations(), response.getScrollId()); } }); }
Example #27
Source File: PostServiceImpl.java From SENS with GNU General Public License v3.0 | 4 votes |
@Override public Page<Post> findPostsByEs(Map<String, Object> criteria, Page<Post> page) { //search request SearchRequest searchRequest = new SearchRequest("blog"); //search builder SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery(); for (String key : criteria.keySet()) { booleanQueryBuilder.must(QueryBuilders.matchQuery(key, criteria.get(key))); } sourceBuilder.query(booleanQueryBuilder); sourceBuilder.from(Integer.parseInt(String.valueOf((page.getCurrent() - 1) * page.getSize()))); sourceBuilder.size(Integer.parseInt(String.valueOf(page.getSize()))); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //sort sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); //highlight HighlightBuilder highlightBuilder = new HighlightBuilder(); HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("postTitle"); highlightTitle.preTags("<span class=\"highlight\">"); highlightTitle.postTags("</span>"); highlightBuilder.field(highlightTitle); sourceBuilder.highlighter(highlightBuilder); // add builder into request searchRequest.indices("blog"); searchRequest.source(sourceBuilder); //response SearchResponse searchResponse = null; List<Post> postList = new ArrayList<>(); try { searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); //search hits SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits(); SearchHit[] searchHits = hits.getHits(); page.setTotal((int) totalHits); for (SearchHit hit : searchHits) { String str = hit.getSourceAsString(); Post esPost = JSONObject.parseObject(str, Post.class); //高亮 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField highlight = highlightFields.get("postTitle"); if (highlight != null) { Text[] fragments = highlight.fragments(); String fragmentString = fragments[0].string(); esPost.setPostTitle(fragmentString); } postList.add(esPost); } } catch (Exception e) { e.printStackTrace(); log.error("ES未开启: {}", e); } return page.setRecords(postList); }
Example #28
Source File: ElasticSearchService.java From mogu_blog_v2 with Apache License 2.0 | 4 votes |
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 #29
Source File: QueryAction.java From elasticsearch-sql with Apache License 2.0 | 4 votes |
protected HighlightBuilder.Field parseHighlightField(Object[] params) { if (params == null || params.length == 0 || params.length > 2) { //todo: exception. } HighlightBuilder.Field field = new HighlightBuilder.Field(params[0].toString()); if (params.length == 1) { return field; } Map<String, Object> highlightParams = (Map<String, Object>) params[1]; for (Map.Entry<String, Object> param : highlightParams.entrySet()) { switch (param.getKey()) { case "type": field.highlighterType((String) param.getValue()); break; case "boundary_chars": field.boundaryChars(fromArrayListToCharArray((ArrayList) param.getValue())); break; case "boundary_max_scan": field.boundaryMaxScan((Integer) param.getValue()); break; case "force_source": field.forceSource((Boolean) param.getValue()); break; case "fragmenter": field.fragmenter((String) param.getValue()); break; case "fragment_offset": field.fragmentOffset((Integer) param.getValue()); break; case "fragment_size": field.fragmentSize((Integer) param.getValue()); break; case "highlight_filter": field.highlightFilter((Boolean) param.getValue()); break; case "matched_fields": field.matchedFields((String[]) ((ArrayList) param.getValue()).toArray(new String[((ArrayList) param.getValue()).size()])); break; case "no_match_size": field.noMatchSize((Integer) param.getValue()); break; case "num_of_fragments": field.numOfFragments((Integer) param.getValue()); break; case "order": field.order((String) param.getValue()); break; case "phrase_limit": field.phraseLimit((Integer) param.getValue()); break; case "post_tags": field.postTags((String[]) ((ArrayList) param.getValue()).toArray(new String[((ArrayList) param.getValue()).size()])); break; case "pre_tags": field.preTags((String[]) ((ArrayList) param.getValue()).toArray(new String[((ArrayList) param.getValue()).size()])); break; case "require_field_match": field.requireFieldMatch((Boolean) param.getValue()); break; } } return field; }
Example #30
Source File: ItemByEsServiceImpl.java From springBoot with MIT License | 4 votes |
@Override public Result<PageInfo<ItemByEsVo>> searchByNative(ItemByEsVo itemByEsVo) { // 统计查询时间,这里开始 Instant start = Instant.now(); // 构造查询条件 QueryBuilder matchQuery = QueryBuilders.matchQuery("title", itemByEsVo.getTitle()) .analyzer("standard") //分词器 .operator(Operator.OR);//or查询 Operator.OR、and查询Operator.AND // 设置高亮,使用默认的highlighter高亮器 HighlightBuilder highlightBuilder = new HighlightBuilder() .field("title") //需要高亮的域(字段) .preTags("<span style=\"color:red;\">") //前缀 .postTags("</span>"); //后缀 // 设置查询字段 SearchResponse response = client.prepareSearch("book") .setQuery(matchQuery) .highlighter(highlightBuilder) // 设置一次返回的文档数量 .setSize(10) .get(); // 返回搜索结果 SearchHits hits = response.getHits(); // 统计搜索结束时间 Instant end = Instant.now(); ArrayList novel = new ArrayList(); for (int i = 0; i < hits.getTotalHits(); i++) { // 得到SearchHit对象 SearchHit hit = hits.getAt(i); // 遍历结果,使用HashMap存放 LinkedHashMap map = new LinkedHashMap(); map.put("Source As String", hit.getSourceAsString()); // 返回String格式的文档结果 System.out.println("Source As String:" + hit.getSourceAsString()); map.put("Source As Map", hit.getSourceAsMap()); // 返回Map格式的文档结果 System.out.println("Source As Map:" + hit.getSourceAsMap()); // 返回文档所在的索引 map.put("Index", hit.getIndex()); System.out.println("Index:" + hit.getIndex()); // 返回文档所在的类型 map.put("Type", hit.getType()); System.out.println("Type:" + hit.getType()); // 返回文档所在的ID编号 map.put("Id", hit.getId()); System.out.println("Id:" + hit.getId()); // 返回指定字段的内容,例如这里返回完整的title的内容 map.put("Title", hit.getSourceAsMap().get("title")); System.out.println("title: " + hit.getSourceAsMap().get("title")); // 返回文档的评分 map.put("Scope", hit.getScore()); System.out.println("Scope:" + hit.getScore()); // 返回文档的高亮字段 Text[] text = hit.getHighlightFields().get("title").getFragments(); StringBuilder hight = new StringBuilder(); if (text != null) { for (Text str : text) { hight.append(str); System.out.println(str.toString()); } } map.put("Highlight", hight.toString()); novel.add(map); } System.out.println(novel); System.out.println("共查出"+hits.getTotalHits()+"条记录!"); System.out.println("共耗时"+ Duration.between(start, end).toMillis()+"ms"); return null; }