public JanusGraphIndex getExternalIndex(Class<? extends Element> clazz, String backingIndex) { String prefix; if (Vertex.class.isAssignableFrom(clazz)) prefix = "v"; else if (Edge.class.isAssignableFrom(clazz)) prefix = "e"; else if (JanusGraphVertexProperty.class.isAssignableFrom(clazz)) prefix = "p"; else throw new AssertionError(clazz.toString()); final String indexName = prefix+backingIndex; JanusGraphIndex index = mgmt.getGraphIndex(indexName); if (index==null) { index = mgmt.buildIndex(indexName,clazz).buildMixedIndex(backingIndex); } return index; }
mgmt.buildIndex("store1", Vertex.class).addKey(field1Key).buildMixedIndex(INDEX); mgmt.commit();
/** * Tests indexing dates */ @Test public void testDateIndexing() { final PropertyKey name = makeKey("date", Date.class); mgmt.buildIndex("dateIndex", Vertex.class). addKey(name).buildMixedIndex(INDEX); finishSchema(); clopen(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("date", new Date(1)); final JanusGraphVertex v2 = graph.addVertex(); v2.property("date", new Date(2000)); assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.EQUAL, new Date(1)))); assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.GREATER_THAN, new Date(1)))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.GREATER_THAN_EQUAL, new Date(1)).vertices())); assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.LESS_THAN, new Date(2000)))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.LESS_THAN_EQUAL, new Date(2000)).vertices())); assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.NOT_EQUAL, new Date(1)))); clopen();//Flush the index assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.EQUAL, new Date(1)))); assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.GREATER_THAN, new Date(1)))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.GREATER_THAN_EQUAL, new Date(1)).vertices())); assertEquals(v1, getOnlyVertex(graph.query().has("date", Cmp.LESS_THAN, new Date(2000)))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("date", Cmp.LESS_THAN_EQUAL, new Date(2000)).vertices())); assertEquals(v2, getOnlyVertex(graph.query().has("date", Cmp.NOT_EQUAL, new Date(1)))); }
final JanusGraphIndex mixed = mgmt.buildIndex("mixed", Vertex.class).addKey(name, Mapping.TEXTSTRING.asParameter()).buildMixedIndex(INDEX); mixed.name(); finishSchema();
final PropertyKey name = makeKey("instant", Instant.class); mgmt.buildIndex("instantIndex", Vertex.class). addKey(name).buildMixedIndex(INDEX); finishSchema(); clopen();
@Test public void testSimpleUpdate() { final PropertyKey name = makeKey("name", String.class); makeLabel("knows"); mgmt.buildIndex("namev", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.buildIndex("namee", Edge.class).addKey(name).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez"); Edge e = v.addEdge("knows", v, "name", "Hulu Bubab"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.single, "name", "Marko"); e = getOnlyEdge(v.query().direction(Direction.OUT)); e.property("name", "Tubu Rubu"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); }
mgmt.buildIndex("namev", Vertex.class).addKey(name).addKey(alias, indexFeatures.supportsStringMapping(Mapping.TEXTSTRING) ?Mapping.TEXTSTRING.asParameter(): Mapping.DEFAULT.asParameter()).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez");
@Test public void testSetUpdate() { if (!indexFeatures.supportsCardinality(Cardinality.SET)) { return; } final PropertyKey name = makeKey("name", String.class); final PropertyKey alias = mgmt.makePropertyKey("alias").dataType(String.class).cardinality(Cardinality.SET).make(); mgmt.buildIndex("namev", Vertex.class).addKey(name).addKey(alias, indexFeatures.supportsStringMapping(Mapping.TEXTSTRING) ?Mapping.TEXTSTRING.asParameter(): Mapping.DEFAULT.asParameter()).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.set, "alias", "Marko"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.set, "alias", "mRodriguez"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); if (indexFeatures.supportsStringMapping(Mapping.TEXTSTRING)) { assertCount(1, tx.query().has("alias", Cmp.EQUAL, "Marko").vertices()); assertCount(1, tx.query().has("alias", Cmp.EQUAL, "mRodriguez").vertices()); } }
/** * Tests indexing boolean */ @Test public void testUUIDIndexing() { final PropertyKey name = makeKey("uid", UUID.class); mgmt.buildIndex("uuidIndex", Vertex.class). addKey(name).buildMixedIndex(INDEX); finishSchema(); clopen(); final UUID uid1 = UUID.randomUUID(); final UUID uid2 = UUID.randomUUID(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("uid", uid1); final JanusGraphVertex v2 = graph.addVertex(); v2.property("uid", uid2); assertCount(2, graph.query().vertices()); assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1))); assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2))); assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1))); assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2))); clopen();//Flush the index assertCount(2, graph.query().vertices()); assertEquals(v1, getOnlyVertex(graph.query().has("uid", uid1))); assertEquals(v2, getOnlyVertex(graph.query().has("uid", uid2))); assertEquals(v2, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid1))); assertEquals(v1, getOnlyVertex(graph.query().has("uid", Cmp.NOT_EQUAL, uid2))); }
private void testIndexing(Cardinality cardinality) { if (supportsCollections()) { final PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make(); final PropertyKey intProperty = mgmt.makePropertyKey("age").dataType(Integer.class).cardinality(cardinality).make(); final PropertyKey longProperty = mgmt.makePropertyKey("long").dataType(Long.class).cardinality(cardinality).make(); final PropertyKey uuidProperty = mgmt.makePropertyKey("uuid").dataType(UUID.class).cardinality(cardinality).make(); final PropertyKey geopointProperty = mgmt.makePropertyKey("geopoint").dataType(Geoshape.class).cardinality(cardinality).make(); mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).addKey(intProperty).addKey(longProperty).addKey(uuidProperty).addKey(geopointProperty).buildMixedIndex(INDEX); finishSchema(); testCollection(cardinality, "name", "Totoro", "Hiro"); testCollection(cardinality, "age", 1, 2); testCollection(cardinality, "long", 1L, 2L); testCollection(cardinality, "geopoint", Geoshape.point(1.0, 1.0), Geoshape.point(2.0, 2.0)); final String backend = readConfig.get(INDEX_BACKEND, INDEX); // Solr 6 has issues processing UUIDs with Multivalues // https://issues.apache.org/jira/browse/SOLR-11264 if (!"solr".equals(backend)) { testCollection(cardinality, "uuid", UUID.randomUUID(), UUID.randomUUID()); } } else { try { final PropertyKey stringProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(cardinality).make(); //This should throw an exception mgmt.buildIndex("collectionIndex", Vertex.class).addKey(stringProperty, getStringMapping()).buildMixedIndex(INDEX); Assert.fail("Should have thrown an exception"); } catch (final JanusGraphException ignored) { } } }
@Test public void testAndForceIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(ageProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); assertCount(1, g.V().has("name", "Totoro")); assertCount(1, g.V().has("age", 2)); assertCount(1, g.V().and(__.has("name", "Hiro"),__.has("age", 2))); assertCount(0, g.V().and(__.has("name", "Totoro"),__.has("age", 2))); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@Test public void testOrForceIndexPartialIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey stringProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(stringProperty, getStringMapping()).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); g.V().or(__.has("name", "Totoro"),__.has("age", 2)).hasNext(); fail("should fail"); } catch (final JanusGraphException e){ assertTrue(e.getMessage().contains("Could not find a suitable index to answer graph query and graph scans are disabled")); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
/** * Tests indexing boolean */ @Test public void testBooleanIndexing() { final PropertyKey name = makeKey("visible", Boolean.class); mgmt.buildIndex("booleanIndex", Vertex.class). addKey(name).buildMixedIndex(INDEX); finishSchema(); clopen(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("visible", true); final JanusGraphVertex v2 = graph.addVertex(); v2.property("visible", false); assertCount(2, graph.vertices()); assertEquals(v1, getOnlyVertex(graph.query().has("visible", true))); assertEquals(v2, getOnlyVertex(graph.query().has("visible", false))); assertEquals(v2, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, true))); assertEquals(v1, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, false))); clopen();//Flush the index assertCount(2, graph.vertices()); assertEquals(v1, getOnlyVertex(graph.query().has("visible", true))); assertEquals(v2, getOnlyVertex(graph.query().has("visible", false))); assertEquals(v2, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, true))); assertEquals(v1, getOnlyVertex(graph.query().has("visible", Cmp.NOT_EQUAL, false))); }
@Test public void testOrForceIndexMixedAndCompositeIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("nameidx", Vertex.class).addKey(nameProperty, getStringMapping()).buildMixedIndex(INDEX); management.buildIndex("ageridx", Vertex.class).addKey(ageProperty).buildCompositeIndex(); management.buildIndex("lengthidx", Vertex.class).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@Test public void shouldAwaitMultipleStatuses() throws InterruptedException, ExecutionException { final PropertyKey key1 = makeKey("key1", String.class); final JanusGraphIndex index = mgmt.buildIndex("randomMixedIndex", Vertex.class).addKey(key1).buildMixedIndex(INDEX); if (index.getIndexStatus(key1) == SchemaStatus.INSTALLED) { mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.REGISTER_INDEX).get(); mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get(); } else if (index.getIndexStatus(key1) == SchemaStatus.REGISTERED) { mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get(); } final PropertyKey key2 = makeKey("key2", String.class); mgmt.addIndexKey(index, key2); mgmt.commit(); //key1 now has status ENABLED, let's ensure we can watch for REGISTERED and ENABLED try { ManagementSystem.awaitGraphIndexStatus(graph, "randomMixedIndex").status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call(); } catch (final Exception e) { Assert.fail("Failed to awaitGraphIndexStatus on multiple statuses."); } }
/** * Tests indexing using _all virtual field */ @Test public void testWidcardQuery() { if (supportsWildcardQuery()) { final PropertyKey p1 = makeKey("p1", String.class); final PropertyKey p2 = makeKey("p2", String.class); mgmt.buildIndex("mixedIndex", Vertex.class).addKey(p1).addKey(p2).buildMixedIndex(INDEX); finishSchema(); clopen(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("p1", "test1"); v1.property("p2", "test2"); clopen();//Flush the index assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test1\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test2\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); } }
@Test public void testOrForceIndexUniqueMixedIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = management.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(ageProperty).addKey(lengthProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); testOr(customGraph); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@Test public void testIndexQueryWithScore() throws InterruptedException { final PropertyKey textKey = mgmt.makePropertyKey("text").dataType(String.class).make(); mgmt.buildIndex("store1", Vertex.class).addKey(textKey).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); final JanusGraphVertex v2 = tx.addVertex(); final JanusGraphVertex v3 = tx.addVertex(); v1.property("text", "Hello Hello Hello Hello Hello Hello Hello Hello world"); v2.property("text", "Hello abab abab fsdfsd sfdfsd sdffs fsdsdf fdf fsdfsd aera fsad abab abab fsdfsd sfdf"); v3.property("text", "Hello Hello world world"); tx.commit(); final Set<Double> scores = graph.indexQuery("store1", "v.text:(Hello)").vertexStream() .map(JanusGraphIndexQuery.Result::getScore) .collect(Collectors.toSet()); Assert.assertEquals(3, scores.size()); }
@Test // this tests a case when there as AND with a single CONTAINS condition inside AND(name:(was here)) // which (in case of Solr) spans multiple conditions such as AND(AND(name:was, name:here)) // so we need to make sure that we don't apply AND twice. public void testContainsWithMultipleValues() throws Exception { final PropertyKey name = makeKey("name", String.class); mgmt.buildIndex("store1", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "hercules was here"); tx.commit(); final JanusGraphVertex r = Iterables.get(graph.query().has("name", Text.CONTAINS, "hercules here").vertices(), 0); Assert.assertEquals(r.property("name").value(), "hercules was here"); }
@Test public void testOrPartialIndex() { final PropertyKey nameProperty = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey lengthProperty = mgmt.makePropertyKey("length").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex("otheridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(lengthProperty).buildMixedIndex(INDEX); finishSchema(); clopen(); testOr(graph); }