private void assertThatFacetHasSize(IssueQuery issueQuery, String facet, int expectedSize) { SearchResponse result = underTest.search(issueQuery, new SearchOptions().addFacets(singletonList(facet))); Facets facets = new Facets(result, system2.getDefaultTimeZone()); assertThat(facets.get(facet)).hasSize(expectedSize); } }
private static SearchProjectsWsResponse.Builder addFacets(SearchResults searchResults, SearchProjectsWsResponse.Builder wsResponse) { Facets esFacets = searchResults.facets; EsToWsFacet esToWsFacet = new EsToWsFacet(); searchResults.query.getLanguages().ifPresent(languages -> addMandatoryValuesToFacet(esFacets, FILTER_LANGUAGES, languages)); searchResults.query.getTags().ifPresent(tags -> addMandatoryValuesToFacet(esFacets, FILTER_TAGS, tags)); Common.Facets wsFacets = esFacets.getAll().entrySet().stream() .map(esToWsFacet) .collect(Collector.of( Common.Facets::newBuilder, Common.Facets.Builder::addFacets, (result1, result2) -> { throw new IllegalStateException("Parallel execution forbidden"); }, Common.Facets.Builder::build)); wsResponse.setFacets(wsFacets); return wsResponse; }
private void processDateHistogram(Histogram aggregation) { LinkedHashMap<String, Long> facet = getOrCreateFacet(aggregation.getName()); for (Histogram.Bucket value : aggregation.getBuckets()) { String day = dateTimeToDate(value.getKeyAsString(), timeZone); if (value.getAggregations().getAsMap().containsKey(FACET_MODE_EFFORT)) { facet.put(day, Math.round(((Sum) value.getAggregations().get(FACET_MODE_EFFORT)).getValue())); } else { facet.put(day, value.getDocCount()); } } }
private void processAggregation(Aggregation aggregation) { if (Missing.class.isAssignableFrom(aggregation.getClass())) { processMissingAggregation((Missing) aggregation); } else if (Terms.class.isAssignableFrom(aggregation.getClass())) { processTermsAggregation((Terms) aggregation); } else if (Filter.class.isAssignableFrom(aggregation.getClass())) { processSubAggregations((Filter) aggregation); } else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) { processSubAggregations((HasAggregations) aggregation); } else if (Histogram.class.isAssignableFrom(aggregation.getClass())) { processDateHistogram((Histogram) aggregation); } else if (Sum.class.isAssignableFrom(aggregation.getClass())) { processSum((Sum) aggregation); } else if (MultiBucketsAggregation.class.isAssignableFrom(aggregation.getClass())) { processMultiBucketAggregation((MultiBucketsAggregation) aggregation); } else { throw new IllegalArgumentException("Aggregation type not supported yet: " + aggregation.getClass()); } }
private void processSubAggregations(HasAggregations aggregation) { if (Filter.class.isAssignableFrom(aggregation.getClass())) { Filter filter = (Filter) aggregation; if (filter.getName().startsWith(NO_DATA_PREFIX)) { LinkedHashMap<String, Long> facet = getOrCreateFacet(filter.getName().replaceFirst(NO_DATA_PREFIX,"")); facet.put("NO_DATA", ((Filter) aggregation).getDocCount()); } } for (Aggregation sub : aggregation.getAggregations()) { processAggregation(sub); } }
private static void addMandatoryValuesToFacet(Facets facets, String facetName, Iterable<String> mandatoryValues) { Map<String, Long> buckets = facets.get(facetName); if (buckets == null) { return; } for (String mandatoryValue : mandatoryValues) { if (!buckets.containsKey(mandatoryValue)) { buckets.put(mandatoryValue, 0L); } } }
@SafeVarargs private final void assertThatFacetHasExactly(IssueQuery.Builder query, String facet, Map.Entry<String, Long>... expectedEntries) { SearchResponse result = underTest.search(query.build(), new SearchOptions().addFacets(singletonList(facet))); Facets facets = new Facets(result, system2.getDefaultTimeZone()); assertThat(facets.getNames()).containsOnly(facet, "effort"); assertThat(facets.get(facet)).containsExactly(expectedEntries); }
/** * Facet with 2 filters * -- lang facet for tag T2 * -- tag facet for lang cpp * -- repository for cpp & T2 */ @Test public void sticky_facets_with_2_filters() { setupStickyFacets(); RuleQuery query = new RuleQuery() .setOrganization(db.getDefaultOrganization()) .setLanguages(ImmutableList.of("cpp")) .setTags(ImmutableList.of("T2")); SearchIdResult result = underTest.search(query, new SearchOptions().addFacets(asList(FACET_LANGUAGES, FACET_REPOSITORIES, FACET_TAGS))); assertThat(result.getIds()).hasSize(1); Facets facets = result.getFacets(); assertThat(facets.getAll()).hasSize(3); assertThat(facets.get(FACET_LANGUAGES).keySet()).containsOnly("cpp", "java"); assertThat(facets.get(FACET_REPOSITORIES).keySet()).containsOnly("foo"); assertThat(facets.get(FACET_TAGS).keySet()).containsOnly("T2", "T3"); }
@Test public void facets_on_languages() { ComponentDto project = ComponentTesting.newPrivateProjectDto(newOrganizationDto()); ComponentDto file = ComponentTesting.newFileDto(project, null); RuleKey ruleKey = RuleKey.of("repo", "X1"); indexIssues(IssueDocTesting.newDoc("I1", file).setLanguage("xoo").setEffort(10L)); Facets facets = search("languages"); assertThat(facets.getNames()).containsOnly("languages", FACET_MODE_EFFORT); assertThat(facets.get("languages")).containsOnly(entry("xoo", 10L)); assertThat(facets.get(FACET_MODE_EFFORT)).containsOnly(entry("total", 10L)); }
public SearchResult(SearchResponse response, Function<Map<String, Object>, DOC> converter, TimeZone timeZone) { this.facets = new Facets(response, timeZone); this.total = response.getHits().getTotalHits(); this.docs = EsUtils.convertToDocs(response.getHits(), converter); }
Facets facets = null; if (!options.getFacets().isEmpty()) { facets = new Facets(result, system2.getDefaultTimeZone()); collectFacets(collector, facets); Set<String> facetsRequiringProjectOrOrganizationParameter = facets.getNames().stream() .filter(FACETS_REQUIRING_PROJECT_OR_ORGANIZATION::contains) .collect(toSet());
private void processSum(Sum aggregation) { getOrCreateFacet(aggregation.getName()).put(TOTAL, Math.round(aggregation.getValue())); }
private static void collectFacets(SearchResponseLoader.Collector collector, Facets facets) { collector.addProjectUuids(facets.getBucketKeys(FACET_PROJECTS)); collector.addComponentUuids(facets.getBucketKeys(PARAM_MODULE_UUIDS)); collector.addComponentUuids(facets.getBucketKeys(PARAM_FILE_UUIDS)); collector.addComponentUuids(facets.getBucketKeys(PARAM_COMPONENT_UUIDS)); collector.addRuleIds(facets.getBucketKeys(PARAM_RULES)); collector.addUserUuids(facets.getBucketKeys(PARAM_ASSIGNEES)); }
public Facets(SearchResponse response, TimeZone timeZone) { this.facetsByName = new LinkedHashMap<>(); this.timeZone = timeZone; Aggregations aggregations = response.getAggregations(); if (aggregations != null) { for (Aggregation facet : aggregations) { processAggregation(facet); } } }
private static void addMandatoryValuesToFacet(Facets facets, String facetName, @Nullable Iterable<String> mandatoryValues) { Map<String, Long> buckets = facets.get(facetName); if (buckets != null && mandatoryValues != null) { for (String mandatoryValue : mandatoryValues) { if (!buckets.containsKey(mandatoryValue)) { buckets.put(mandatoryValue, 0L); } } } }
@SafeVarargs private final void assertThatFacetHasOnly(IssueQuery.Builder query, String facet, Map.Entry<String, Long>... expectedEntries) { SearchResponse result = underTest.search(query.build(), new SearchOptions().addFacets(singletonList(facet))); Facets facets = new Facets(result, system2.getDefaultTimeZone()); assertThat(facets.getNames()).containsOnly(facet, "effort"); assertThat(facets.get(facet)).containsOnly(expectedEntries); } }
private void processAggregation(Aggregation aggregation) { if (Missing.class.isAssignableFrom(aggregation.getClass())) { processMissingAggregation((Missing) aggregation); } else if (Terms.class.isAssignableFrom(aggregation.getClass())) { processTermsAggregation((Terms) aggregation); } else if (Filter.class.isAssignableFrom(aggregation.getClass())) { processSubAggregations((Filter) aggregation); } else if (HasAggregations.class.isAssignableFrom(aggregation.getClass())) { processSubAggregations((HasAggregations) aggregation); } else if (Histogram.class.isAssignableFrom(aggregation.getClass())) { processDateHistogram((Histogram) aggregation); } else if (Sum.class.isAssignableFrom(aggregation.getClass())) { processSum((Sum) aggregation); } else if (MultiBucketsAggregation.class.isAssignableFrom(aggregation.getClass())) { processMultiBucketAggregation((MultiBucketsAggregation) aggregation); } else { throw new IllegalArgumentException("Aggregation type not supported yet: " + aggregation.getClass()); } }
/** * Facet with a language filter * -- lang facet should still have all language */ @Test public void sticky_facets_with_1_filter() { setupStickyFacets(); OrganizationDto organization = db.organizations().insert(); RuleQuery query = new RuleQuery().setOrganization(organization).setLanguages(ImmutableList.of("cpp")); SearchIdResult result = underTest.search(query, new SearchOptions().addFacets(asList(FACET_LANGUAGES, FACET_REPOSITORIES, FACET_TAGS))); assertThat(result.getIds()).hasSize(3); assertThat(result.getFacets().getAll()).hasSize(3); assertThat(result.getFacets().get(FACET_LANGUAGES).keySet()).containsOnly("cpp", "java", "cobol"); assertThat(result.getFacets().get(FACET_REPOSITORIES).keySet()).containsOnly("foo"); assertThat(result.getFacets().get(FACET_TAGS).keySet()).containsOnly("T2", "T3"); }
@Test public void facets_on_directories() { ComponentDto project = ComponentTesting.newPrivateProjectDto(newOrganizationDto()); ComponentDto file1 = ComponentTesting.newFileDto(project, null).setPath("src/main/xoo/F1.xoo"); ComponentDto file2 = ComponentTesting.newFileDto(project, null).setPath("F2.xoo"); indexIssues( IssueDocTesting.newDoc("I1", file1).setDirectoryPath("/src/main/xoo").setEffort(10L), IssueDocTesting.newDoc("I2", file2).setDirectoryPath("/").setEffort(10L)); Facets facets = search("directories"); assertThat(facets.getNames()).containsOnly("directories", FACET_MODE_EFFORT); assertThat(facets.get("directories")).containsOnly(entry("/src/main/xoo", 10L), entry("/", 10L)); assertThat(facets.get(FACET_MODE_EFFORT)).containsOnly(entry("total", 20L)); }
public SearchIdResult(SearchResponse response, Function<String, ID> converter, TimeZone timeZone) { this.facets = new Facets(response, timeZone); this.total = response.getHits().getTotalHits(); this.ids = convertToIds(response.getHits(), converter); }