use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket in project alfresco-remote-api by Alfresco.
the class RangeResultMapperTests method testBuildGenericBucketFromRange.
@Test
public void testBuildGenericBucketFromRange() throws Exception {
// =============== Default
GenericBucket response = queryWithoutInclude("head");
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithoutInclude("body");
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithoutInclude("tail");
assertEquals("test:[\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
// =============== start bucket
response = queryWithInclude("head", null);
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("head", "outer");
assertEquals("test:<\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("head", "lower");
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("head", "upper", "lower");
assertEquals("test:[\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("head", "edge", "upper");
assertEquals("test:[\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("head", "upper");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
// =============== Non start end bucket
response = queryWithInclude("body", "lower");
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("body", "upper");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
// =============== End bucket
response = queryWithInclude("tail", "lower");
assertEquals("test:[\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("true", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("tail", "edge");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
response = queryWithInclude("tail", "upper");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
// Before
response = queryWithInclude("head", "before");
assertEquals("test:<\"0\" TO \"10\">", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("false", response.getBucketInfo().get("endInclusive"));
response = queryWithIncludeAndOther("head", "outer", "before");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
// After
response = queryWithIncludeAndOther("head", "outer", "after");
assertEquals("test:<\"0\" TO \"10\"]", response.getFilterQuery());
assertEquals("false", response.getBucketInfo().get("startInclusive"));
assertEquals("true", response.getBucketInfo().get("endInclusive"));
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket in project alfresco-remote-api by Alfresco.
the class ResultMapper method getFacetBucketsForFacetFieldsAsFacets.
protected List<GenericFacetResponse> getFacetBucketsForFacetFieldsAsFacets(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
Object display = null;
String filterQuery = null;
if (searchQuery != null && searchQuery.getFacetFields() != null && searchQuery.getFacetFields().getFacets() != null && !searchQuery.getFacetFields().getFacets().isEmpty()) {
Optional<FacetField> found = searchQuery.getFacetFields().getFacets().stream().filter(queryable -> facet.getKey().equals(queryable.getLabel() != null ? queryable.getLabel() : queryable.getField())).findFirst();
if (found.isPresent()) {
display = propertyLookup.lookup(found.get().getField(), buck.getFirst());
String fq = found.get().toFilterQuery(buck.getFirst());
if (fq != null) {
filterQuery = fq;
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, display, new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count, String.valueOf(buck.getSecond())))), null, null);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.field, facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket in project alfresco-remote-api by Alfresco.
the class ResultMapper method getGenericFacetsForIntervals.
/**
* Returns generic faceting responses for Intervals
* @param facetFields
* @param searchQuery
* @return GenericFacetResponse
*/
protected static List<GenericFacetResponse> getGenericFacetsForIntervals(Map<String, List<Pair<String, Integer>>> facetFields, SearchQuery searchQuery) {
if (facetFields != null && !facetFields.isEmpty()) {
List<GenericFacetResponse> ffcs = new ArrayList<>(facetFields.size());
for (Entry<String, List<Pair<String, Integer>>> facet : facetFields.entrySet()) {
if (facet.getValue() != null && !facet.getValue().isEmpty()) {
List<GenericBucket> buckets = new ArrayList<>(facet.getValue().size());
for (Pair<String, Integer> buck : facet.getValue()) {
String filterQuery = null;
Map<String, String> bucketInfo = new HashMap<>();
if (searchQuery != null && searchQuery.getFacetIntervals() != null && searchQuery.getFacetIntervals().getIntervals() != null && !searchQuery.getFacetIntervals().getIntervals().isEmpty()) {
Optional<Interval> found = searchQuery.getFacetIntervals().getIntervals().stream().filter(interval -> facet.getKey().equals(interval.getLabel() != null ? interval.getLabel() : interval.getField())).findFirst();
if (found.isPresent()) {
if (found.get().getSets() != null) {
Optional<IntervalSet> foundSet = found.get().getSets().stream().filter(aSet -> buck.getFirst().equals(aSet.getLabel())).findFirst();
if (foundSet.isPresent()) {
filterQuery = found.get().getField() + ":" + foundSet.get().toAFTSQuery();
bucketInfo.put(GenericFacetResponse.START, foundSet.get().getStart());
bucketInfo.put(GenericFacetResponse.END, foundSet.get().getEnd());
bucketInfo.put(GenericFacetResponse.START_INC, String.valueOf(foundSet.get().isStartInclusive()));
bucketInfo.put(GenericFacetResponse.END_INC, String.valueOf(foundSet.get().isEndInclusive()));
}
}
}
}
GenericBucket bucket = new GenericBucket(buck.getFirst(), filterQuery, null, new HashSet<Metric>(Arrays.asList(new SimpleMetric(METRIC_TYPE.count, String.valueOf(buck.getSecond())))), null, bucketInfo);
buckets.add(bucket);
}
ffcs.add(new GenericFacetResponse(FACET_TYPE.interval, facet.getKey(), buckets));
}
}
return ffcs;
}
return Collections.emptyList();
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket in project alfresco-remote-api by Alfresco.
the class ResultMapper method getFacetBucketsFromFacetQueries.
/**
* Builds a facet field from facet queries.
* @param facetQueries
* @return
*/
protected List<GenericFacetResponse> getFacetBucketsFromFacetQueries(Map<String, Integer> facetQueries, SearchQuery searchQuery) {
List<GenericFacetResponse> facetResults = new ArrayList<GenericFacetResponse>();
Map<String, List<GenericBucket>> groups = new HashMap<>();
for (Entry<String, Integer> fq : facetQueries.entrySet()) {
String group = null;
String filterQuery = null;
if (searchQuery != null && searchQuery.getFacetQueries() != null) {
Optional<FacetQuery> found = searchQuery.getFacetQueries().stream().filter(facetQuery -> fq.getKey().equals(facetQuery.getLabel())).findFirst();
filterQuery = found.isPresent() ? found.get().getQuery() : fq.getKey();
if (found.isPresent() && found.get().getGroup() != null) {
group = found.get().getGroup();
}
}
// {
if (groups.containsKey(group)) {
Set<Metric> metrics = new HashSet<>(1);
metrics.add(new SimpleMetric(METRIC_TYPE.count, fq.getValue()));
groups.get(group).add(new GenericBucket(fq.getKey(), filterQuery, null, metrics, null));
} else {
List<GenericBucket> l = new ArrayList<GenericBucket>();
Set<Metric> metrics = new HashSet<>(1);
metrics.add(new SimpleMetric(METRIC_TYPE.count, fq.getValue()));
l.add(new GenericBucket(fq.getKey(), filterQuery, null, metrics, null));
groups.put(group, l);
}
}
// }
if (!groups.isEmpty()) {
groups.forEach((a, v) -> facetResults.add(new GenericFacetResponse(FACET_TYPE.query, a, v)));
}
return facetResults;
}
use of org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericBucket in project alfresco-remote-api by Alfresco.
the class ResultMapperTests method testToSearchContext.
@Test
public void testToSearchContext() throws Exception {
ResultSet results = mockResultset(Collections.emptyList(), Collections.emptyList());
SearchQuery searchQuery = helper.searchQueryFromJson();
SearchRequestContext searchRequest = SearchRequestContext.from(searchQuery);
SearchParameters searchParams = searchMapper.toSearchParameters(EMPTY_PARAMS, searchQuery, searchRequest);
SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
assertEquals(34l, searchContext.getConsistency().getlastTxId());
assertEquals(6, searchContext.getFacetQueries().size());
assertEquals(0, searchContext.getFacetQueries().get(0).getCount());
assertEquals("cm:created:bob", searchContext.getFacetQueries().get(0).getFilterQuery());
assertEquals("small", searchContext.getFacetQueries().get(0).getLabel());
assertEquals("searchInsteadFor", searchContext.getSpellCheck().getType());
assertEquals(1, searchContext.getSpellCheck().getSuggestions().size());
assertEquals("alfresco", searchContext.getSpellCheck().getSuggestions().get(0));
assertEquals(1, searchContext.getFacetsFields().size());
assertEquals("content.size", searchContext.getFacetsFields().get(0).getLabel());
// Facet intervals
List<GenericFacetResponse> intervalFacets = searchContext.getFacets().stream().filter(f -> f.getType().equals(FACET_TYPE.interval)).collect(Collectors.toList());
assertEquals(2, intervalFacets.size());
assertEquals("creator", intervalFacets.get(0).getLabel());
assertEquals("last", intervalFacets.get(0).getBuckets().get(0).getLabel());
assertEquals("cm:creator:<\"a\" TO \"b\"]", intervalFacets.get(0).getBuckets().get(0).getFilterQuery());
Metric[] metrics = intervalFacets.get(0).getBuckets().get(0).getMetrics().toArray(new Metric[intervalFacets.get(0).getBuckets().get(0).getMetrics().size()]);
assertEquals(METRIC_TYPE.count, metrics[0].getType());
assertEquals("4", metrics[0].getValue().get("count"));
// Requests search Query
assertNotNull(searchContext.getRequest());
assertEquals("great", searchContext.getRequest().getQuery().getUserQuery());
// Pivot
assertEquals(7, searchContext.getFacets().size());
GenericFacetResponse pivotFacet = searchContext.getFacets().get(4);
assertEquals(FACET_TYPE.pivot, pivotFacet.getType());
assertEquals("creator", pivotFacet.getLabel());
assertEquals(2, pivotFacet.getBuckets().size());
GenericBucket pivotBucket = pivotFacet.getBuckets().get(1);
assertEquals("mjackson", pivotBucket.getLabel());
assertEquals("creator:\"mjackson\"", pivotBucket.getFilterQuery());
metrics = pivotBucket.getMetrics().toArray(new Metric[pivotBucket.getMetrics().size()]);
assertEquals("{count=7}", metrics[0].getValue().toString());
assertEquals(1, pivotBucket.getFacets().size());
GenericFacetResponse nestedFacet = pivotBucket.getFacets().get(0);
assertEquals(FACET_TYPE.pivot, nestedFacet.getType());
assertEquals("mylabel", nestedFacet.getLabel());
assertEquals(2, nestedFacet.getBuckets().size());
GenericBucket nestedBucket = nestedFacet.getBuckets().get(0);
assertEquals("mjackson", nestedBucket.getLabel());
assertEquals("modifier:\"mjackson\"", nestedBucket.getFilterQuery());
metrics = nestedBucket.getMetrics().toArray(new Metric[nestedBucket.getMetrics().size()]);
assertEquals("{count=3}", metrics[0].getValue().toString());
GenericBucket nestedBucket2 = nestedFacet.getBuckets().get(1);
assertEquals("admin", nestedBucket2.getLabel());
assertEquals("modifier:\"admin\"", nestedBucket2.getFilterQuery());
metrics = nestedBucket2.getMetrics().toArray(new Metric[nestedBucket2.getMetrics().size()]);
assertEquals("{count=4}", metrics[0].getValue().toString());
// Stats
GenericFacetResponse statsFacet = searchContext.getFacets().get(5);
assertEquals(FACET_TYPE.stats, statsFacet.getType());
assertEquals("created", statsFacet.getLabel());
Set<Metric> statsMetrics = statsFacet.getBuckets().get(0).getMetrics();
assertEquals(8, statsMetrics.size());
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.sumOfSquares, 2.1513045770343806E27)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.min, "2011-02-15T20:16:27.080Z")));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.max, "2017-04-10T15:06:30.143Z")));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.mean, "2016-09-05T04:20:12.898Z")));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.countValues, 990)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.missing, 290)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.sum, 1.458318720769983E15)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.stddev, 5.6250677994522545E10)));
statsFacet = searchContext.getFacets().get(6);
assertEquals("numericLabel", statsFacet.getLabel());
statsMetrics = statsFacet.getBuckets().get(0).getMetrics();
assertEquals(7, statsMetrics.size());
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.sumOfSquares, 0)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.countValues, 0)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.missing, 0)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.sum, 0)));
assertTrue(statsMetrics.contains(new SimpleMetric(METRIC_TYPE.stddev, 0)));
JSONArray dVals = new JSONArray(Arrays.asList(12, 13, 14, 15, 16, 17, 1));
assertTrue(statsMetrics.contains(new ListMetric(METRIC_TYPE.distinctValues, dVals)));
JSONArray pers = new JSONArray(Arrays.asList("0.99", 20.0685, "0.0", 12.0));
assertTrue(statsMetrics.contains(new PercentileMetric(METRIC_TYPE.percentiles, pers)));
assertEquals("min must be excluded because its null", 0, statsMetrics.stream().filter(metric -> METRIC_TYPE.min.equals(metric.getType())).count());
assertEquals("max must be excluded because its null", 0, statsMetrics.stream().filter(metric -> METRIC_TYPE.max.equals(metric.getType())).count());
assertEquals("mean must be excluded because its NaN", 0, statsMetrics.stream().filter(metric -> METRIC_TYPE.mean.equals(metric.getType())).count());
}
Aggregations