private TermsResult getTermsAggregationResult(TermsAggregation agg, Iterator<T> it) { String propertyName = agg.getPropertyName(); Map<Object, List<T>> elementsByProperty = getElementsByProperty(it, propertyName, o -> o); elementsByProperty = collapseBucketsByCase(elementsByProperty); List<TermsBucket> buckets = new ArrayList<>(); for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { Object key = entry.getKey(); int count = entry.getValue().size(); Map<String, AggregationResult> nestedResults = getNestedResults(agg.getNestedAggregations(), entry.getValue()); buckets.add(new TermsBucket(key, count, nestedResults)); } return new TermsResult(buckets); }
private Map<Object, List<T>> collapseBucketsByCase(Map<Object, List<T>> elementsByProperty) { Map<String, List<Map.Entry<Object, List<T>>>> stringEntries = new HashMap<>(); Map<Object, List<T>> results = new HashMap<>(); // for strings first group them by there lowercase version for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { if (entry.getKey() instanceof String) { String lowerCaseKey = ((String) entry.getKey()).toLowerCase(); List<Map.Entry<Object, List<T>>> l = stringEntries.computeIfAbsent(lowerCaseKey, s -> new ArrayList<>()); l.add(entry); } else { results.put(entry.getKey(), entry.getValue()); } } // for strings find the best key (the one with the most entries) and use that as the bucket name for (Map.Entry<String, List<Map.Entry<Object, List<T>>>> entry : stringEntries.entrySet()) { results.put( findBestKey(entry.getValue()), entry.getValue().stream() .flatMap(l -> l.getValue().stream()) .collect(Collectors.toList()) ); } return results; }
@Override public <TResult extends AggregationResult> TResult getAggregationResult(String name, Class<? extends TResult> resultType) { for (Aggregation agg : this.aggregations) { if (agg.getAggregationName().equals(name)) { return getAggregationResult(agg, this.iterator(true)); } } return super.getAggregationResult(name, resultType); }
@SuppressWarnings("unchecked") public <TResult extends AggregationResult> TResult getAggregationResult(Aggregation agg, Iterator<T> it) { if (agg instanceof TermsAggregation) { return (TResult) getTermsAggregationResult((TermsAggregation) agg, it); } if (agg instanceof CalendarFieldAggregation) { return (TResult) getCalendarFieldHistogramResult((CalendarFieldAggregation) agg, it); } if (agg instanceof CardinalityAggregation) { return (TResult) getCardinalityAggregationResult((CardinalityAggregation) agg, it); } throw new VertexiumException("Unhandled aggregation: " + agg.getClass().getName()); }
protected QueryResultsIterable<? extends VertexiumObject> extendedData(FetchHints fetchHints, Iterable<? extends Element> elements) { Iterable<ExtendedDataRow> allExtendedData = new SelectManyIterable<Element, ExtendedDataRow>(elements) { @Override protected Iterable<? extends ExtendedDataRow> getIterable(Element element) { return new SelectManyIterable<String, ExtendedDataRow>(element.getExtendedDataTableNames()) { @Override protected Iterable<? extends ExtendedDataRow> getIterable(String tableName) { return element.getExtendedData(tableName); } }; } }; return new DefaultGraphQueryIterableWithAggregations<>(getParameters(), allExtendedData, true, true, true, getAggregations()); }
private HistogramResult getCalendarFieldHistogramResult(final CalendarFieldAggregation agg, Iterator<T> it) { String propertyName = agg.getPropertyName(); final Calendar calendar = GregorianCalendar.getInstance(agg.getTimeZone()); Map<Integer, List<T>> elementsByProperty = getElementsByProperty(it, propertyName, o -> { Date d = (Date) o; calendar.setTime(d); //noinspection MagicConstant return calendar.get(agg.getCalendarField()); }); Map<Integer, HistogramBucket> buckets = new HashMap<>(24); for (Map.Entry<Integer, List<T>> entry : elementsByProperty.entrySet()) { int key = entry.getKey(); int count = entry.getValue().size(); Map<String, AggregationResult> nestedResults = getNestedResults(agg.getNestedAggregations(), entry.getValue()); buckets.put(key, new HistogramBucket(key, count, nestedResults)); } return new HistogramResult(buckets.values()); }
@Override public boolean isAggregationSupported(Aggregation aggregation) { if (DefaultGraphQueryIterableWithAggregations.isAggregationSupported(aggregation)) { return true; } return super.isAggregationSupported(aggregation); } }
private Map<String, AggregationResult> getNestedResults(Iterable<Aggregation> nestedAggregations, List<T> elements) { Map<String, AggregationResult> results = new HashMap<>(); for (Aggregation nestedAggregation : nestedAggregations) { AggregationResult nestedResult = getAggregationResult(nestedAggregation, elements.iterator()); results.put(nestedAggregation.getAggregationName(), nestedResult); } return results; }
@SuppressWarnings("unchecked") public <TResult extends AggregationResult> TResult getAggregationResult(Aggregation agg, Iterator<T> it) { if (agg instanceof TermsAggregation) { return (TResult) getTermsAggregationResult((TermsAggregation) agg, it); } if (agg instanceof CalendarFieldAggregation) { return (TResult) getCalendarFieldHistogramResult((CalendarFieldAggregation) agg, it); } if (agg instanceof CardinalityAggregation) { return (TResult) getCardinalityAggregationResult((CardinalityAggregation) agg, it); } throw new VertexiumException("Unhandled aggregation: " + agg.getClass().getName()); }
protected QueryResultsIterable<? extends VertexiumObject> extendedData(FetchHints fetchHints, Iterable<? extends Element> elements) { Iterable<ExtendedDataRow> allExtendedData = new SelectManyIterable<Element, ExtendedDataRow>(elements) { @Override protected Iterable<? extends ExtendedDataRow> getIterable(Element element) { return new SelectManyIterable<String, ExtendedDataRow>(element.getExtendedDataTableNames()) { @Override protected Iterable<? extends ExtendedDataRow> getIterable(String tableName) { return element.getExtendedData(tableName); } }; } }; return new DefaultGraphQueryIterableWithAggregations<>(getParameters(), allExtendedData, true, true, true, getAggregations()); }
private HistogramResult getCalendarFieldHistogramResult(final CalendarFieldAggregation agg, Iterator<T> it) { String propertyName = agg.getPropertyName(); final Calendar calendar = GregorianCalendar.getInstance(agg.getTimeZone()); Map<Integer, List<T>> elementsByProperty = getElementsByProperty(it, propertyName, o -> { Date d = (Date) o; calendar.setTime(d); //noinspection MagicConstant return calendar.get(agg.getCalendarField()); }); Map<Integer, HistogramBucket> buckets = new HashMap<>(24); for (Map.Entry<Integer, List<T>> entry : elementsByProperty.entrySet()) { int key = entry.getKey(); int count = entry.getValue().size(); Map<String, AggregationResult> nestedResults = getNestedResults(agg.getNestedAggregations(), entry.getValue()); buckets.put(key, new HistogramBucket(key, count, nestedResults)); } return new HistogramResult(buckets.values()); }
@Override public boolean isAggregationSupported(Aggregation aggregation) { if (DefaultGraphQueryIterableWithAggregations.isAggregationSupported(aggregation)) { return true; } return super.isAggregationSupported(aggregation); } }
private Map<String, AggregationResult> getNestedResults(Iterable<Aggregation> nestedAggregations, List<T> elements) { Map<String, AggregationResult> results = new HashMap<>(); for (Aggregation nestedAggregation : nestedAggregations) { AggregationResult nestedResult = getAggregationResult(nestedAggregation, elements.iterator()); results.put(nestedAggregation.getAggregationName(), nestedResult); } return results; }
private TermsResult getTermsAggregationResult(TermsAggregation agg, Iterator<T> it) { String propertyName = agg.getPropertyName(); Map<Object, List<T>> elementsByProperty = getElementsByProperty(it, propertyName, o -> o); elementsByProperty = collapseBucketsByCase(elementsByProperty); List<TermsBucket> buckets = new ArrayList<>(); for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { Object key = entry.getKey(); int count = entry.getValue().size(); Map<String, AggregationResult> nestedResults = getNestedResults(agg.getNestedAggregations(), entry.getValue()); buckets.add(new TermsBucket(key, count, nestedResults)); } return new TermsResult(buckets); }
@Override public QueryResultsIterable<Vertex> vertices(FetchHints fetchHints) { return new DefaultGraphQueryIterableWithAggregations<>( getParameters(), this.<Vertex>getIterableFromElementType(ElementType.VERTEX, fetchHints), true, true, true, getAggregations() ); }
@Override public <TResult extends AggregationResult> TResult getAggregationResult(String name, Class<? extends TResult> resultType) { for (Aggregation agg : this.aggregations) { if (agg.getAggregationName().equals(name)) { return getAggregationResult(agg, this.iterator(true)); } } return super.getAggregationResult(name, resultType); }
@Override public boolean isAggregationSupported(Aggregation aggregation) { if (DefaultGraphQueryIterableWithAggregations.isAggregationSupported(aggregation)) { return true; } return super.isAggregationSupported(aggregation); }
private Map<Object, List<T>> collapseBucketsByCase(Map<Object, List<T>> elementsByProperty) { Map<String, List<Map.Entry<Object, List<T>>>> stringEntries = new HashMap<>(); Map<Object, List<T>> results = new HashMap<>(); // for strings first group them by there lowercase version for (Map.Entry<Object, List<T>> entry : elementsByProperty.entrySet()) { if (entry.getKey() instanceof String) { String lowerCaseKey = ((String) entry.getKey()).toLowerCase(); List<Map.Entry<Object, List<T>>> l = stringEntries.computeIfAbsent(lowerCaseKey, s -> new ArrayList<>()); l.add(entry); } else { results.put(entry.getKey(), entry.getValue()); } } // for strings find the best key (the one with the most entries) and use that as the bucket name for (Map.Entry<String, List<Map.Entry<Object, List<T>>>> entry : stringEntries.entrySet()) { results.put( findBestKey(entry.getValue()), entry.getValue().stream() .flatMap(l -> l.getValue().stream()) .collect(Collectors.toList()) ); } return results; }
@Override public QueryResultsIterable<Edge> edges(FetchHints fetchHints) { Iterable<Edge> edges = allEdges(fetchHints); return new DefaultGraphQueryIterableWithAggregations<>(getParameters(), edges, true, true, true, getAggregations()); }
@Override public boolean isAggregationSupported(Aggregation aggregation) { if (DefaultGraphQueryIterableWithAggregations.isAggregationSupported(aggregation)) { return true; } return super.isAggregationSupported(aggregation); } }