protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) { List<AggregationBuilder> termsAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Edge.LABEL_PROPERTY_NAME.equals(fieldName)) { TermsBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0")); termsAgg.field(fieldName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); TermsBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), visibilityHash)); termsAgg.field(propertyName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AbstractAggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg);
private Aggregation getTermsAggregation(String aggregationName, JSONObject aggregateJson) { String field = aggregateJson.getString("field"); TermsAggregation terms = new TermsAggregation(aggregationName, field); int size = aggregateJson.optInt("size", 0); if (size > 0) { terms.setSize(size); } return terms; }
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); }
TermsAggregation termsAggregation = new TermsAggregation("name-count", "name"); rangeAggregation.addNestedAggregation(termsAggregation); TermsResult lowerTermsResult = (TermsResult) lowerNestedResult.get(termsAggregation.getAggregationName()); List<TermsBucket> lowerTermsBuckets = IterableUtils.toList(lowerTermsResult.getBuckets()); Collections.sort(lowerTermsBuckets, bucketComparator); TermsResult upperTermsResult = (TermsResult) upperNestedResult.get(termsAggregation.getAggregationName()); List<TermsBucket> upperTermsBuckets = IterableUtils.toList(upperTermsResult.getBuckets()); assertEquals(1, upperNestedResult.size());
@Test public void testVertexQueryWithNestedTermsAggregationOnExtendedData() { graph.defineProperty("name").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH, TextIndexHint.FULL_TEXT).define(); graph.defineProperty("gender").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "name", "Joe", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "gender", "female", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "gender", "female", VISIBILITY_EMPTY) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); Query q = v1.getExtendedData("t1").query(AUTHORIZATIONS_A_AND_B).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.extendedDataRows().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested"); assertEquals(2, vertexPropertyCountByValue.size()); assertEquals(1, vertexPropertyCountByValue.get("Joe").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Joe").get("male")); assertEquals(2, vertexPropertyCountByValue.get("Sam").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Sam").get("male")); assertEquals(2L, (long) vertexPropertyCountByValue.get("Sam").get("female")); }
private Map<Object, Long> queryGraphQueryWithTermsAggregation(String queryString, String propertyName, ElementType elementType, Authorizations authorizations) { Query q = (queryString == null ? graph.query(authorizations) : graph.query(queryString, authorizations)).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", agg.getClass().getName()); return null; } q.addAggregation(agg); QueryResultsIterable<? extends Element> elements = elementType == ElementType.VERTEX ? q.vertices() : q.edges(); TermsResult aggregationResult = elements.getAggregationResult("terms-count", TermsResult.class); return termsBucketToMap(aggregationResult.getBuckets()); }
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); }
TermsAggregation termsAggregation = new TermsAggregation("name-count", "name"); rangeAggregation.addNestedAggregation(termsAggregation); TermsResult lowerTermsResult = (TermsResult) lowerNestedResult.get(termsAggregation.getAggregationName()); List<TermsBucket> lowerTermsBuckets = IterableUtils.toList(lowerTermsResult.getBuckets()); Collections.sort(lowerTermsBuckets, bucketComparator); TermsResult upperTermsResult = (TermsResult) upperNestedResult.get(termsAggregation.getAggregationName()); List<TermsBucket> upperTermsBuckets = IterableUtils.toList(upperTermsResult.getBuckets()); assertEquals(1, upperNestedResult.size());
@Test public void testVertexQueryWithNestedTermsAggregationOnExtendedData() { graph.defineProperty("name").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH, TextIndexHint.FULL_TEXT).define(); graph.defineProperty("gender").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "name", "Joe", VISIBILITY_EMPTY) .addExtendedData("t1", "r1", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r2", "gender", "male", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r3", "gender", "female", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "name", "Sam", VISIBILITY_EMPTY) .addExtendedData("t1", "r4", "gender", "female", VISIBILITY_EMPTY) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); Query q = v1.getExtendedData("t1").query(AUTHORIZATIONS_A_AND_B).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.extendedDataRows().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested"); assertEquals(2, vertexPropertyCountByValue.size()); assertEquals(1, vertexPropertyCountByValue.get("Joe").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Joe").get("male")); assertEquals(2, vertexPropertyCountByValue.get("Sam").size()); assertEquals(1L, (long) vertexPropertyCountByValue.get("Sam").get("male")); assertEquals(2L, (long) vertexPropertyCountByValue.get("Sam").get("female")); }
private Map<Object, Long> queryGraphQueryWithTermsAggregation(String queryString, String propertyName, ElementType elementType, Authorizations authorizations) { Query q = (queryString == null ? graph.query(authorizations) : graph.query(queryString, authorizations)).limit(0); TermsAggregation agg = new TermsAggregation("terms-count", propertyName); if (!q.isAggregationSupported(agg)) { LOGGER.warn("%s unsupported", agg.getClass().getName()); return null; } q.addAggregation(agg); QueryResultsIterable<? extends Element> elements = elementType == ElementType.VERTEX ? q.vertices() : q.edges(); TermsResult aggregationResult = elements.getAggregationResult("terms-count", TermsResult.class); return termsBucketToMap(aggregationResult.getBuckets()); }
protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) { List<AggregationBuilder> termsAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Edge.LABEL_PROPERTY_NAME.equals(fieldName)) { TermsBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0")); termsAgg.field(fieldName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); TermsBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), visibilityHash)); termsAgg.field(propertyName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AbstractAggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg);
TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg);
TermsAggregation agg = new TermsAggregation("agg", ExtendedDataRow.TABLE_NAME); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ELEMENT_ID)) .limit(0L); rows = q.extendedDataRows(); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ROW_ID)) .limit(0L); rows = q.extendedDataRows(); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ELEMENT_TYPE)) .limit(0L); rows = q.extendedDataRows();
protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) { List<AggregationBuilder> termsAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Edge.IN_OR_OUT_VERTEX_ID_PROPERTY_NAME.equals(fieldName)) { throw new VertexiumException("Cannot aggregate by: " + fieldName); fieldName = ELEMENT_TYPE_FIELD_NAME; TermsAggregationBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0")); termsAgg.setMetaData(metadata); termsAgg.field(fieldName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg); String aggregationName = createAggregationName(agg.getAggregationName(), visibilityHash); TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregationName); termsAgg.field(propertyNameWithSuffix); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg);
TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg);
TermsAggregation agg = new TermsAggregation("agg", ExtendedDataRow.TABLE_NAME); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ELEMENT_ID)) .limit(0L); rows = q.extendedDataRows(); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ROW_ID)) .limit(0L); rows = q.extendedDataRows(); .addAggregation(new TermsAggregation("agg", ExtendedDataRow.ELEMENT_TYPE)) .limit(0L); rows = q.extendedDataRows();
protected List<AggregationBuilder> getElasticsearchTermsAggregations(TermsAggregation agg) { List<AggregationBuilder> termsAggs = new ArrayList<>(); String fieldName = agg.getPropertyName(); if (Edge.IN_OR_OUT_VERTEX_ID_PROPERTY_NAME.equals(fieldName)) { throw new VertexiumException("Cannot aggregate by: " + fieldName); fieldName = ELEMENT_TYPE_FIELD_NAME; TermsAggregationBuilder termsAgg = AggregationBuilders.terms(createAggregationName(agg.getAggregationName(), "0")); termsAgg.setMetaData(metadata); termsAgg.field(fieldName); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg); String aggregationName = createAggregationName(agg.getAggregationName(), visibilityHash); TermsAggregationBuilder termsAgg = AggregationBuilders.terms(aggregationName); termsAgg.field(propertyNameWithSuffix); if (agg.getSize() != null) { termsAgg.size(agg.getSize()); for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) { termsAgg.subAggregation(subAgg);
TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg);
@Test public void testCaseSensitivityOfExactMatch() { graph.defineProperty("text").dataType(String.class).textIndexHint(TextIndexHint.EXACT_MATCH).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("text", "Joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v2", VISIBILITY_A) .setProperty("text", "joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v3", VISIBILITY_A) .setProperty("text", "JOE", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.prepareVertex("v4", VISIBILITY_A) .setProperty("text", "Joe", VISIBILITY_A) .save(AUTHORIZATIONS_A); graph.flush(); QueryResultsIterable<Vertex> vertices = graph.query(AUTHORIZATIONS_A) .has("text", Compare.EQUAL, "Joe") .addAggregation(new TermsAggregation("agg1", "text")) .vertices(); assertVertexIdsAnyOrder(vertices, "v1", "v2", "v3", "v4"); TermsResult agg = vertices.getAggregationResult("agg1", TermsResult.class); ArrayList<TermsBucket> buckets = Lists.newArrayList(agg.getBuckets()); assertEquals(1, buckets.size()); assertEquals("Joe", buckets.get(0).getKey()); assertEquals(4L, buckets.get(0).getCount()); }
TermsAggregation agg = new TermsAggregation("terms-count", "name"); agg.addNestedAggregation(new TermsAggregation("nested", "gender")); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg);