Skip to content

Commit

Permalink
SONAR-6137 Publish createdAt facet in WS API
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Feb 4, 2015
1 parent 39ee3b2 commit f45419c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
Expand Up @@ -556,8 +556,10 @@ private AggregationBuilder getCreatedAtFacet(IssueQuery query, Map<String, Filte
String timeZoneString = tzFormat.format(now);

Interval bucketSize = Interval.YEAR;
long startTime = query.createdAfter() == null ? getMinCreatedAt(filters, esQuery) : query.createdAfter().getTime();
long endTime = query.createdBefore() == null ? now.getTime() : query.createdBefore().getTime();
Date createdAfter = query.createdAfter();
long startTime = createdAfter == null ? getMinCreatedAt(filters, esQuery) : createdAfter.getTime();
Date createdBefore = query.createdBefore();
long endTime = createdBefore == null ? now.getTime() : createdBefore.getTime();
Duration timeSpan = new Duration(startTime, endTime);
if (timeSpan.isShorterThan(TWENTY_DAYS)) {
bucketSize = Interval.DAY;
Expand Down Expand Up @@ -586,7 +588,7 @@ private long getMinCreatedAt(Map<String, FilterBuilder> filters, QueryBuilder es
setQueryFilter(minCount, esQuery, filters);
minCount.addAggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
Min minValue = minCount.get().getAggregations().get(facetNameAndField);
return Double.valueOf(minValue.getValue()).longValue();
return (long) minValue.getValue();
}

private void setSorting(IssueQuery query, SearchRequestBuilder esRequest) {
Expand Down
Expand Up @@ -297,6 +297,7 @@ protected Collection<String> possibleFacets() {
IssueFilterParameters.DIRECTORIES,
IssueFilterParameters.LANGUAGES,
IssueFilterParameters.TAGS,
IssueFilterParameters.CREATED_AT,
});
}

Expand Down
Expand Up @@ -72,36 +72,52 @@ public Result(@Nullable BaseIndex<K, ?, ?> index, SearchResponse response) {

private void processAggregation(Aggregation aggregation) {
if (Missing.class.isAssignableFrom(aggregation.getClass())) {
Missing missing = (Missing) aggregation;
long docCount = missing.getDocCount();
if (docCount > 0L) {
this.facets.put(aggregation.getName().replace("_missing",""), new FacetValue("", docCount));
}
processMissingAggregation(aggregation);
} else if (Terms.class.isAssignableFrom(aggregation.getClass())) {
Terms termAggregation = (Terms) aggregation;
for (Terms.Bucket value : termAggregation.getBuckets()) {
String facetName = aggregation.getName();
if (facetName.contains("__") && !facetName.startsWith("__")) {
facetName = facetName.substring(0, facetName.indexOf("__"));
}
facetName = facetName.replace("_selected", "");
this.facets.put(facetName, new FacetValue(value.getKey(), value.getDocCount()));
}
processTermsAggregation(aggregation);
} else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) {
HasAggregations hasAggregations = (HasAggregations) aggregation;
for (Aggregation internalAggregation : hasAggregations.getAggregations()) {
this.processAggregation(internalAggregation);
}
processSubAggregations(aggregation);
} else if (DateHistogram.class.isAssignableFrom(aggregation.getClass())) {
DateHistogram dateHistogram = (DateHistogram) aggregation;
for (DateHistogram.Bucket value : dateHistogram.getBuckets()) {
this.facets.put(dateHistogram.getName(), new FacetValue(value.getKeyAsText().toString(), value.getDocCount()));
}
processDateHistogram(aggregation);
} else {
LOGGER.warn("Cannot process {} type of aggregation", aggregation.getClass());
}
}

private void processMissingAggregation(Aggregation aggregation) {
Missing missing = (Missing) aggregation;
long docCount = missing.getDocCount();
if (docCount > 0L) {
this.facets.put(aggregation.getName().replace("_missing",""), new FacetValue("", docCount));
}
}

private void processTermsAggregation(Aggregation aggregation) {
Terms termAggregation = (Terms) aggregation;
for (Terms.Bucket value : termAggregation.getBuckets()) {
String facetName = aggregation.getName();
if (facetName.contains("__") && !facetName.startsWith("__")) {
facetName = facetName.substring(0, facetName.indexOf("__"));
}
facetName = facetName.replace("_selected", "");
this.facets.put(facetName, new FacetValue(value.getKey(), value.getDocCount()));
}
}

private void processSubAggregations(Aggregation aggregation) {
HasAggregations hasAggregations = (HasAggregations) aggregation;
for (Aggregation internalAggregation : hasAggregations.getAggregations()) {
this.processAggregation(internalAggregation);
}
}

private void processDateHistogram(Aggregation aggregation) {
DateHistogram dateHistogram = (DateHistogram) aggregation;
for (DateHistogram.Bucket value : dateHistogram.getBuckets()) {
this.facets.put(dateHistogram.getName(), new FacetValue(value.getKeyAsText().toString(), value.getDocCount()));
}
}

public Iterator<K> scroll() {
Preconditions.checkState(scrollId != null, "Result is not scrollable. Please use QueryOptions.setScroll()");
return index.scroll(scrollId);
Expand Down

0 comments on commit f45419c

Please sign in to comment.