Search in sources :

Example 1 with PropertyKey

use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testContainsWithMultipleValues.

@Test
public // so we need to make sure that we don't apply AND twice.
void testContainsWithMultipleValues() {
    PropertyKey name = makeKey("name", String.class);
    mgmt.buildIndex("store1", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    mgmt.commit();
    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");
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 2 with PropertyKey

use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testDateIndexing.

/**
 * Tests indexing dates
 */
@Test
public void testDateIndexing() {
    PropertyKey name = makeKey("date", Date.class);
    mgmt.buildIndex("dateIndex", Vertex.class).addKey(name).buildMixedIndex(INDEX);
    finishSchema();
    clopen();
    JanusGraphVertex v1 = graph.addVertex();
    v1.property("date", new Date(1));
    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))));
    // Flush the index
    clopen();
    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))));
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Date(java.util.Date) Test(org.junit.Test)

Example 3 with PropertyKey

use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testIndexQueryWithScore.

@Test
public void testIndexQueryWithScore() {
    PropertyKey textKey = mgmt.makePropertyKey("text").dataType(String.class).make();
    mgmt.buildIndex("store1", Vertex.class).addKey(textKey).buildMixedIndex(INDEX);
    mgmt.commit();
    JanusGraphVertex v1 = tx.addVertex();
    JanusGraphVertex v2 = tx.addVertex();
    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());
}
Also used : JanusGraphIndexQuery(org.janusgraph.core.JanusGraphIndexQuery) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 4 with PropertyKey

use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testIndexUpdatesWithoutReindex.

@Test
public void testIndexUpdatesWithoutReindex() throws InterruptedException, ExecutionException {
    Object[] settings = new Object[] { option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250) };
    clopen(settings);
    final String defText = "Mountain rocks are great friends";
    final int defTime = 5;
    final double defHeight = 101.1;
    final String[] defPhones = new String[] { "1234", "5678" };
    // Creates types and index only two keys key
    mgmt.makePropertyKey("time").dataType(Integer.class).make();
    PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
    mgmt.makePropertyKey("height").dataType(Double.class).make();
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        mgmt.makePropertyKey("phone").dataType(String.class).cardinality(Cardinality.LIST).make();
    }
    mgmt.buildIndex("theIndex", Vertex.class).addKey(text, getTextMapping(), getFieldMap(text)).buildMixedIndex(INDEX);
    finishSchema();
    // Add initial data
    addVertex(defTime, defText, defHeight, defPhones);
    // Indexes should not yet be in use
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[] { false, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    }
    newTx();
    // Add another key to index ------------------------------------------------------
    finishSchema();
    PropertyKey time = mgmt.getPropertyKey("time");
    mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), time, getFieldMap(time));
    finishSchema();
    newTx();
    // Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    tx.commit();
    // Should not yet be able to enable since not yet registered
    assertNull(mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX));
    // This call is redundant and just here to make sure it doesn't mess anything up
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.REGISTER_INDEX).get();
    mgmt.commit();
    ManagementSystem.awaitGraphIndexStatus(graph, "theIndex").timeout(10L, ChronoUnit.SECONDS).call();
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX).get();
    finishSchema();
    // Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    // One more key should be indexed but only sees partial data
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 3, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 3, new boolean[] { false, true });
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { false, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { false, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 3, new boolean[] { false, true });
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 3, new boolean[] { false, true });
    }
    newTx();
    // Add another key to index ------------------------------------------------------
    finishSchema();
    PropertyKey height = mgmt.getPropertyKey("height");
    mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), height);
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        PropertyKey phone = mgmt.getPropertyKey("phone");
        mgmt.addIndexKey(mgmt.getGraphIndex("theIndex"), phone, new Parameter("mapping", Mapping.STRING));
    }
    finishSchema();
    // Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    tx.commit();
    mgmt.commit();
    ManagementUtil.awaitGraphIndexUpdate(graph, "theIndex", 10, ChronoUnit.SECONDS);
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
    finishSchema();
    JanusGraphIndex index = mgmt.getGraphIndex("theIndex");
    for (PropertyKey key : index.getFieldKeys()) {
        assertEquals(SchemaStatus.ENABLED, index.getIndexStatus(key));
    }
    // Add more data
    addVertex(defTime, defText, defHeight, defPhones);
    // One more key should be indexed but only sees partial data
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 4, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 2, new boolean[] { true, true }, "theIndex");
    }
    newTx();
    finishSchema();
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.REINDEX).get();
    mgmt.commit();
    finishSchema();
    // All the data should now be in the index
    clopen(settings);
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    if (indexFeatures.supportsCardinality(Cardinality.LIST)) {
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
        evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 5, new boolean[] { true, true }, "theIndex");
    }
    mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.DISABLE_INDEX).get();
    tx.commit();
    mgmt.commit();
    ManagementUtil.awaitGraphIndexUpdate(graph, "theIndex", 10, ChronoUnit.SECONDS);
    finishSchema();
    index = mgmt.getGraphIndex("theIndex");
    for (PropertyKey key : index.getFieldKeys()) {
        assertEquals(SchemaStatus.DISABLED, index.getIndexStatus(key));
    }
    newTx();
    // This now requires a full graph scan
    evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 5, new boolean[] { false, true });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Parameter(org.janusgraph.core.schema.Parameter) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 5 with PropertyKey

use of org.janusgraph.core.PropertyKey in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method testIndexing.

@Test
public void testIndexing() throws InterruptedException {
    PropertyKey text = makeKey("text", String.class);
    createExternalVertexIndex(text, INDEX);
    createExternalEdgeIndex(text, INDEX);
    PropertyKey name = makeKey("name", String.class);
    mgmt.addIndexKey(getExternalIndex(Vertex.class, INDEX), name, Parameter.of("mapping", Mapping.TEXT));
    mgmt.addIndexKey(getExternalIndex(Edge.class, INDEX), name, Parameter.of("mapping", Mapping.TEXT));
    PropertyKey location = makeKey("location", Geoshape.class);
    createExternalVertexIndex(location, INDEX);
    createExternalEdgeIndex(location, INDEX);
    PropertyKey boundary = makeKey("boundary", Geoshape.class);
    mgmt.addIndexKey(getExternalIndex(Vertex.class, INDEX), boundary, Parameter.of("mapping", Mapping.PREFIX_TREE), Parameter.of("index-geo-dist-error-pct", 0.0025));
    mgmt.addIndexKey(getExternalIndex(Edge.class, INDEX), boundary, Parameter.of("mapping", Mapping.PREFIX_TREE), Parameter.of("index-geo-dist-error-pct", 0.0025));
    PropertyKey time = makeKey("time", Long.class);
    createExternalVertexIndex(time, INDEX);
    createExternalEdgeIndex(time, INDEX);
    PropertyKey category = makeKey("category", Integer.class);
    mgmt.buildIndex("vcategory", Vertex.class).addKey(category).buildCompositeIndex();
    mgmt.buildIndex("ecategory", Edge.class).addKey(category).buildCompositeIndex();
    PropertyKey group = makeKey("group", Byte.class);
    createExternalVertexIndex(group, INDEX);
    createExternalEdgeIndex(group, INDEX);
    makeVertexIndexedKey("uid", Integer.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(time).signature(location, boundary).make();
    finishSchema();
    clopen();
    String[] words = { "world", "aurelius", "janusgraph", "graph" };
    int numCategories = 5;
    int numGroups = 10;
    double distance, offset;
    int numV = 100;
    final int originalNumV = numV;
    for (int i = 0; i < numV; i++) {
        JanusGraphVertex v = tx.addVertex();
        v.property(VertexProperty.Cardinality.single, "uid", i);
        v.property(VertexProperty.Cardinality.single, "category", i % numCategories);
        v.property(VertexProperty.Cardinality.single, "group", i % numGroups);
        v.property(VertexProperty.Cardinality.single, "text", "Vertex " + words[i % words.length]);
        v.property(VertexProperty.Cardinality.single, "name", words[i % words.length]);
        v.property(VertexProperty.Cardinality.single, "time", i);
        offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
        v.property(VertexProperty.Cardinality.single, "location", Geoshape.point(0.0 + offset, 0.0 + offset));
        if (i % 2 == 0) {
            v.property(VertexProperty.Cardinality.single, "boundary", Geoshape.line(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 } })));
        } else {
            v.property(VertexProperty.Cardinality.single, "boundary", Geoshape.polygon(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 }, { offset - 0.1, offset - 0.1 } })));
        }
        Edge e = v.addEdge("knows", getVertex("uid", Math.max(0, i - 1)));
        e.property("text", "Vertex " + words[i % words.length]);
        e.property("name", words[i % words.length]);
        e.property("time", i);
        e.property("category", i % numCategories);
        e.property("group", i % numGroups);
        e.property("location", Geoshape.point(0.0 + offset, 0.0 + offset));
        if (i % 2 == 0) {
            e.property("boundary", Geoshape.line(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 } })));
        } else {
            e.property("boundary", Geoshape.polygon(Arrays.asList(new double[][] { { offset - 0.1, offset - 0.1 }, { offset + 0.1, offset - 0.1 }, { offset + 0.1, offset + 0.1 }, { offset - 0.1, offset + 0.1 }, { offset - 0.1, offset - 0.1 } })));
        }
    }
    checkIndexingCounts(words, numV, originalNumV, true);
    // some indexing backends may guarantee only eventual consistency
    for (int retry = 0, status = 1; retry < RETRY_COUNT && status > 0; retry++) {
        clopen();
        try {
            checkIndexingCounts(words, numV, originalNumV, true);
            status = 0;
        } catch (AssertionError e) {
            if (retry >= RETRY_COUNT - 1)
                throw e;
            Thread.sleep(RETRY_INTERVAL);
        }
    }
    newTx();
    int numDelete = 12;
    for (int i = numV - numDelete; i < numV; i++) {
        getVertex("uid", i).remove();
    }
    numV -= numDelete;
    checkIndexingCounts(words, numV, originalNumV, false);
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Aggregations

PropertyKey (org.janusgraph.core.PropertyKey)84 Test (org.junit.Test)59 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)57 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)28 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)28 EdgeLabel (org.janusgraph.core.EdgeLabel)21 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Edge (org.apache.tinkerpop.gremlin.structure.Edge)14 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)13 VertexLabel (org.janusgraph.core.VertexLabel)13 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)11 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)9 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)9 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)8 JanusGraph (org.janusgraph.core.JanusGraph)8 JanusGraphException (org.janusgraph.core.JanusGraphException)8 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)8 Instant (java.time.Instant)6 ExecutionException (java.util.concurrent.ExecutionException)6