private Map<Object, Map<Object, Long>> nestedTermsBucketToMap(Iterable<TermsBucket> buckets, String nestedAggName) { Map<Object, Map<Object, Long>> results = new HashMap<>(); for (TermsBucket entry : buckets) { TermsResult nestedResults = (TermsResult) entry.getNestedResults().get(nestedAggName); if (nestedResults == null) { throw new VertexiumException("Could not find nested: " + nestedAggName); } results.put(entry.getKey(), termsBucketToMap(nestedResults.getBuckets())); } return results; }
private Map<Object, Map<Object, Long>> nestedTermsBucketToMap(Iterable<TermsBucket> buckets, String nestedAggName) { Map<Object, Map<Object, Long>> results = new HashMap<>(); for (TermsBucket entry : buckets) { TermsResult nestedResults = (TermsResult) entry.getNestedResults().get(nestedAggName); if (nestedResults == null) { throw new VertexiumException("Could not find nested: " + nestedAggName); } results.put(entry.getKey(), termsBucketToMap(nestedResults.getBuckets())); } return results; }
private ClientApiSearchResponse.TermsAggregateResult toClientApiTermsAggregateResult(TermsResult agg) { ClientApiSearchResponse.TermsAggregateResult result = new ClientApiSearchResponse.TermsAggregateResult(); for (TermsBucket termsBucket : agg.getBuckets()) { ClientApiSearchResponse.TermsAggregateResult.Bucket b = new ClientApiSearchResponse.TermsAggregateResult.Bucket( termsBucket.getCount(), toClientApiNestedResults(termsBucket.getNestedResults()) ); result.getBuckets().put(termsBucket.getKey().toString(), b); } return result; }
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 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()); }
@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")); }
q.addAggregation(agg); QueryResultsIterable<ExtendedDataRow> rows = q.extendedDataRows(); Map<Object, Long> aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(2, aggResult.size()); assertEquals(4L, (long) aggResult.get("table1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(3, aggResult.size()); assertEquals(2L, (long) aggResult.get("v1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(6, aggResult.size()); assertEquals(1L, (long) aggResult.get("row1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(2, aggResult.size()); assertEquals(4L, (long) aggResult.get(ElementType.VERTEX.name()));
@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")); }
q.addAggregation(agg); QueryResultsIterable<ExtendedDataRow> rows = q.extendedDataRows(); Map<Object, Long> aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(2, aggResult.size()); assertEquals(4L, (long) aggResult.get("table1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(3, aggResult.size()); assertEquals(2L, (long) aggResult.get("v1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(6, aggResult.size()); assertEquals(1L, (long) aggResult.get("row1")); .limit(0L); rows = q.extendedDataRows(); aggResult = termsBucketToMap(rows.getAggregationResult("agg", TermsResult.class).getBuckets()); assertEquals(2, aggResult.size()); assertEquals(4L, (long) aggResult.get(ElementType.VERTEX.name()));
q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested");
q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested");
List<TermsBucket> lowerTermsBuckets = IterableUtils.toList(lowerTermsResult.getBuckets()); Collections.sort(lowerTermsBuckets, bucketComparator); assertEquals(1, lowerNestedResult.size()); List<TermsBucket> upperTermsBuckets = IterableUtils.toList(upperTermsResult.getBuckets()); assertEquals(1, upperNestedResult.size()); assertEquals(1, upperTermsBuckets.size());
q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested");
q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Map<Object, Long>> vertexPropertyCountByValue = nestedTermsBucketToMap(aggregationResult.getBuckets(), "nested");
List<TermsBucket> lowerTermsBuckets = IterableUtils.toList(lowerTermsResult.getBuckets()); Collections.sort(lowerTermsBuckets, bucketComparator); assertEquals(1, lowerNestedResult.size()); List<TermsBucket> upperTermsBuckets = IterableUtils.toList(upperTermsResult.getBuckets()); assertEquals(1, upperNestedResult.size()); assertEquals(1, upperTermsBuckets.size());
@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()); }
@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()); }
@Test public void testGraphQueryWithTermsAggregationAndPredicates() { 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(); Query q = graph.query(AUTHORIZATIONS_EMPTY) .has("name", Compare.EQUAL, "Susan"); TermsAggregation agg = new TermsAggregation("terms-count", "name"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Long> vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(0, vertexPropertyCountByValue.size()); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addPropertyValue("k1", "gender", "female", VISIBILITY_A) .save(AUTHORIZATIONS_A); getGraph().flush(); q = graph.query(AUTHORIZATIONS_A) .has("gender", Compare.EQUAL, "female"); agg = new TermsAggregation("terms-count", "gender"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(1, vertexPropertyCountByValue.size()); }
@Test public void testGraphQueryWithTermsAggregationAndPredicates() { 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(); Query q = graph.query(AUTHORIZATIONS_EMPTY) .has("name", Compare.EQUAL, "Susan"); TermsAggregation agg = new TermsAggregation("terms-count", "name"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); TermsResult aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); Map<Object, Long> vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(0, vertexPropertyCountByValue.size()); graph.prepareVertex("v1", VISIBILITY_EMPTY) .addPropertyValue("k1", "gender", "female", VISIBILITY_A) .save(AUTHORIZATIONS_A); getGraph().flush(); q = graph.query(AUTHORIZATIONS_A) .has("gender", Compare.EQUAL, "female"); agg = new TermsAggregation("terms-count", "gender"); assumeTrue("terms aggregation not supported", q.isAggregationSupported(agg)); q.addAggregation(agg); aggregationResult = q.vertices().getAggregationResult("terms-count", TermsResult.class); vertexPropertyCountByValue = termsBucketToMap(aggregationResult.getBuckets()); assertEquals(1, vertexPropertyCountByValue.size()); }