org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder Java Examples
The following examples show how to use
org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder.
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: CustomSearchRepositoryImpl.java From klask-io with GNU General Public License v3.0 | 6 votes |
/** * create a SearchResponse with the main search query (from the FileResource /api/_search/files) * * @param query * @param aggregation * @return */ private SearchResponse createResponse(String query, TermsBuilder aggregation) { SearchResponse response; if (StringUtils.isNotEmpty(query)) { response = elasticsearchTemplate.getClient().prepareSearch(Constants.INDEX_NAME) .setTypes(Constants.TYPE_NAME) //ici nous utilisons la même querybuilder que dans la recherche principale pour obtenir justement //le même filtrage sur les versions courantes .setQuery(Queries.constructQuery(query)) .addAggregation(aggregation) .execute().actionGet(); } else { response = elasticsearchTemplate.getClient().prepareSearch(Constants.INDEX_NAME) .setTypes(Constants.TYPE_NAME) .addAggregation(aggregation) .execute().actionGet(); } return response; }
Example #2
Source File: CustomSearchRepositoryImpl.java From klask-io with GNU General Public License v3.0 | 5 votes |
@Override public Map<String, Long> aggregateByRawField(String field, String filtre) { TermsBuilder aggregation = AggregationBuilders.terms("top_" + field) .field(field + ".raw") .size(0)// le résultat n'est pas complet si on ne précise pas la taille, 0 : infini // (voir : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_size) .order(Terms.Order.aggregation("_count", false)); SearchResponse response = createResponse(filtre, aggregation); Map<String, Aggregation> results = response.getAggregations().asMap(); StringTerms topField = (StringTerms) results.get("top_" + field); //sur l'ensemble des buckets, triés par ordre décroissant sur le nombre de documents // on retourne une Map (LinkedHashMap) pour conserver l'ordre avec la clé, le nom du champ (exemple version), et la valeur, le nombre de docs //exemple : // "trunk" -> 34012 // "branche_1" -> 35800 return topField.getBuckets() .stream() .sorted(Comparator.comparing(Terms.Bucket::getDocCount).reversed()) .collect( Collectors.toMap(bucket -> bucket.getKeyAsString(), bucket -> bucket.getDocCount(), (v1, v2) -> v1, LinkedHashMap::new )); }
Example #3
Source File: NpmSearchFacetHosted.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public Content searchV1(final Parameters parameters) throws IOException { String text = npmSearchParameterExtractor.extractText(parameters); int size = npmSearchParameterExtractor.extractSize(parameters); int from = npmSearchParameterExtractor.extractFrom(parameters); // npm search V1 endpoint currently returns an empty result set if no text is provided in the request NpmSearchResponse response; if (text.isEmpty()) { response = npmSearchResponseFactory.buildEmptyResponse(); } else { QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(text) .allowLeadingWildcard(true) .analyzeWildcard(true); TermsBuilder terms = AggregationBuilders.terms("name") .field("assets.attributes.npm.name") .size(v1SearchMaxResults) .subAggregation(AggregationBuilders.topHits("versions") .addSort(SortBuilders.fieldSort("assets.attributes.npm.search_normalized_version") .order(SortOrder.DESC)) .setTrackScores(true) .setSize(1)); SearchResponse searchResponse = searchQueryService.search( repositoryQuery(query).inRepositories(getRepository()), singletonList(terms)); Aggregations aggregations = searchResponse.getAggregations(); Terms nameTerms = aggregations.get("name"); response = npmSearchResponseFactory.buildResponseForResults(nameTerms.getBuckets(), size, from); } String content = npmSearchResponseMapper.writeString(response); return new Content(new StringPayload(content, ContentTypes.APPLICATION_JSON)); }
Example #4
Source File: AnalyticsServiceElasticsearch.java From hawkular-apm with Apache License 2.0 | 5 votes |
@Override public List<NodeTimeseriesStatistics> getNodeTimeseriesStatistics(String tenantId, Criteria criteria, long interval) { String index = client.getIndex(tenantId); if (!refresh(index)) { return null; } AvgBuilder avgBuilder = AggregationBuilders .avg("avg") .field(ElasticsearchUtil.ACTUAL_FIELD); TermsBuilder componentsBuilder = AggregationBuilders .terms("components") .field("componentType") .size(criteria.getMaxResponseSize()) .subAggregation(avgBuilder); DateHistogramBuilder histogramBuilder = AggregationBuilders .dateHistogram("histogram") .interval(interval) .field(ElasticsearchUtil.TIMESTAMP_FIELD) .subAggregation(componentsBuilder); BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, NodeDetails.class); SearchRequestBuilder request = getNodeDetailsRequest(index, criteria, query, 0) .addAggregation(histogramBuilder); SearchResponse response = getSearchResponse(request); DateHistogram histogram = response.getAggregations().get("histogram"); return histogram.getBuckets().stream() .map(AnalyticsServiceElasticsearch::toNodeTimeseriesStatistics) .collect(Collectors.toList()); }
Example #5
Source File: SimpleSearchQueryBuilder.java From onetwo with Apache License 2.0 | 5 votes |
public SimpleAggregationBuilder(PB parentBuilder, TermsBuilder terms, String field, Integer size) { super(parentBuilder); if(size!=null){ terms.size(size); } if(StringUtils.isNotBlank(field)){ terms.field(field); } terms.order(Terms.Order.count(false));//COUNT_DESC this.aggsBuilder = terms; }
Example #6
Source File: AggregationBuilders.java From Elasticsearch with Apache License 2.0 | 4 votes |
/** * Create a new {@link Terms} aggregation with the given name. */ public static TermsBuilder terms(String name) { return new TermsBuilder(name); }
Example #7
Source File: AnalyticsServiceElasticsearch.java From hawkular-apm with Apache License 2.0 | 4 votes |
/** * This method builds a map of communication summary stats related to the supplied * criteria. * * @param stats The map of communication summary stats * @param index The index * @param criteria The criteria * @param addMetrics Whether to add metrics on the nodes/links */ private void buildCommunicationSummaryStatistics(Map<String, CommunicationSummaryStatistics> stats, String index, Criteria criteria, boolean addMetrics) { if (!refresh(index)) { return; } // Don't specify target class, so that query provided that can be used with // CommunicationDetails and CompletionTime BoolQueryBuilder query = buildQuery(criteria, ElasticsearchUtil.TRANSACTION_FIELD, null); // Only want external communications query = query.mustNot(QueryBuilders.matchQuery("internal", "true")); StatsBuilder latencyBuilder = AggregationBuilders .stats("latency") .field(ElasticsearchUtil.LATENCY_FIELD); TermsBuilder targetBuilder = AggregationBuilders .terms("target") .field(ElasticsearchUtil.TARGET_FIELD) .size(criteria.getMaxResponseSize()) .subAggregation(latencyBuilder); TermsBuilder sourceBuilder = AggregationBuilders .terms("source") .field(ElasticsearchUtil.SOURCE_FIELD) .size(criteria.getMaxResponseSize()) .subAggregation(targetBuilder); SearchRequestBuilder request = getBaseSearchRequestBuilder(COMMUNICATION_DETAILS_TYPE, index, criteria, query, 0) .addAggregation(sourceBuilder); SearchResponse response = getSearchResponse(request); for (Terms.Bucket sourceBucket : response.getAggregations().<Terms>get("source").getBuckets()) { Terms targets = sourceBucket.getAggregations().get("target"); CommunicationSummaryStatistics css = stats.get(sourceBucket.getKey()); if (css == null) { css = new CommunicationSummaryStatistics(); css.setId(sourceBucket.getKey()); css.setUri(EndpointUtil.decodeEndpointURI(css.getId())); css.setOperation(EndpointUtil.decodeEndpointOperation(css.getId(), true)); stats.put(css.getId(), css); } if (addMetrics) { css.setCount(sourceBucket.getDocCount()); } for (Terms.Bucket targetBucket : targets.getBuckets()) { Stats latency = targetBucket.getAggregations().get("latency"); String linkId = targetBucket.getKey(); ConnectionStatistics con = css.getOutbound().get(linkId); if (con == null) { con = new ConnectionStatistics(); css.getOutbound().put(linkId, con); } if (addMetrics) { con.setMinimumLatency((long)latency.getMin()); con.setAverageLatency((long)latency.getAvg()); con.setMaximumLatency((long)latency.getMax()); con.setCount(targetBucket.getDocCount()); } } } addNodeInformation(stats, index, criteria, addMetrics, false); addNodeInformation(stats, index, criteria, addMetrics, true); }
Example #8
Source File: SimpleSearchQueryBuilder.java From onetwo with Apache License 2.0 | 4 votes |
private TermsBuilder asTermsBuilder(){ return (TermsBuilder) this.aggsBuilder; }
Example #9
Source File: EsEventPersistence.java From logsniffer with GNU Lesser General Public License v3.0 | 4 votes |
@Override public AspectProvider<AspectSniffer, Integer> getEventsCounter() { return new PostAspectProvider<SnifferPersistence.AspectSniffer, Integer>() { @Override public Integer getApsect(final AspectSniffer host) { return host.getAspect(EVENTS_COUNT, Integer.class); } @Override public void injectAspect(final List<AspectSniffer> hosts) { final long start = System.currentTimeMillis(); final HashMap<Long, AspectSniffer> mapHosts = new HashMap<>(); final long[] hostIds = new long[hosts.size()]; int i = 0; for (final AspectSniffer s : hosts) { hostIds[i++] = s.getId(); mapHosts.put(s.getId(), s); s.setAspect(EVENTS_COUNT, 0); } clientTpl.executeWithClient(new ClientCallback<Object>() { @Override public Object execute(final Client client) { final TermsBuilder terms = AggregationBuilders.terms("eventsCounter") .field(Event.FIELD_SNIFFER_ID).include(hostIds).size(hostIds.length); final SearchRequestBuilder requestBuilder = client.prepareSearch().setSize(0) .addAggregation(terms); final SearchResponse response = requestBuilder.execute().actionGet(); logger.debug("Performed events counting search {} in {}ms", requestBuilder, System.currentTimeMillis() - start); final Terms aventsCounterAgg = response.getAggregations() != null ? (Terms) response.getAggregations().get("eventsCounter") : null; if (aventsCounterAgg != null) { for (final Terms.Bucket entry : aventsCounterAgg.getBuckets()) { final long snifferId = entry.getKeyAsNumber().longValue(); if (mapHosts.containsKey(snifferId)) { mapHosts.get(snifferId).setAspect(EVENTS_COUNT, entry.getDocCount()); } } } return null; } }); } }; }
Example #10
Source File: AST_Stats.java From elasticsearch-rest-command with The Unlicense | 4 votes |
public static TermsBuilder newTermsBucket(String name, int limit, String field, int mincount, boolean script) { TermsBuilder d = AggregationBuilders.terms(name).size(limit).minDocCount(mincount); return script? d.script(field): d.field(field); }
Example #11
Source File: AST_Stats.java From elasticsearch-rest-command with The Unlicense | 4 votes |
public void setBucketLimit(int idx, long limit){ if(buckets.get(idx) instanceof TermsBuilder) ((TermsBuilder)buckets.get(idx)).size((int) limit); }