Search in sources :

Example 6 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate in project janusgraph by JanusGraph.

the class IndexProviderTest method storeTest.

private void storeTest(String... stores) throws Exception {
    final Multimap<String, Object> doc1 = getDocument("Hello world", 1001, 5.2, Geoshape.point(48.0, 0.0), Geoshape.polygon(Arrays.asList(new double[][] { { -0.1, 47.9 }, { 0.1, 47.9 }, { 0.1, 48.1 }, { -0.1, 48.1 }, { -0.1, 47.9 } })), Arrays.asList("1", "2", "3"), Sets.newHashSet("1", "2"), Instant.ofEpochSecond(1), false);
    final Multimap<String, Object> doc2 = getDocument("Tomorrow is the world", 1010, 8.5, Geoshape.point(49.0, 1.0), Geoshape.line(Arrays.asList(new double[][] { { 0.9, 48.9 }, { 0.9, 49.1 }, { 1.1, 49.1 }, { 1.1, 48.9 } })), Arrays.asList("4", "5", "6"), Sets.newHashSet("4", "5"), Instant.ofEpochSecond(2), true);
    final Multimap<String, Object> doc3 = getDocument("Hello Bob, are you there?", -500, 10.1, Geoshape.point(47.0, 10.0), Geoshape.box(46.9, 9.9, 47.1, 10.1), Arrays.asList("7", "8", "9"), Sets.newHashSet("7", "8"), Instant.ofEpochSecond(3), false);
    for (final String store : stores) {
        initialize(store);
        add(store, "doc1", doc1, true);
        add(store, "doc2", doc2, true);
        add(store, "doc3", doc3, false);
    }
    final ImmutableList<IndexQuery.OrderEntry> orderTimeAsc = ImmutableList.of(new IndexQuery.OrderEntry(TIME, Order.ASC, Integer.class));
    final ImmutableList<IndexQuery.OrderEntry> orderWeightAsc = ImmutableList.of(new IndexQuery.OrderEntry(WEIGHT, Order.ASC, Double.class));
    final ImmutableList<IndexQuery.OrderEntry> orderTimeDesc = ImmutableList.of(new IndexQuery.OrderEntry(TIME, Order.DESC, Integer.class));
    final ImmutableList<IndexQuery.OrderEntry> orderWeightDesc = ImmutableList.of(new IndexQuery.OrderEntry(WEIGHT, Order.DESC, Double.class));
    final ImmutableList<IndexQuery.OrderEntry> jointOrder = ImmutableList.of(new IndexQuery.OrderEntry(WEIGHT, Order.DESC, Double.class), new IndexQuery.OrderEntry(TIME, Order.DESC, Integer.class));
    final ImmutableList<IndexQuery.OrderEntry> orderNameAsc = ImmutableList.of(new IndexQuery.OrderEntry(NAME, Order.ASC, String.class));
    final ImmutableList<IndexQuery.OrderEntry> orderNameDesc = ImmutableList.of(new IndexQuery.OrderEntry(NAME, Order.DESC, String.class));
    final ImmutableList<IndexQuery.OrderEntry> orderDateAsc = ImmutableList.of(new IndexQuery.OrderEntry(DATE, Order.ASC, Instant.class));
    final ImmutableList<IndexQuery.OrderEntry> orderDateDesc = ImmutableList.of(new IndexQuery.OrderEntry(DATE, Order.DESC, Instant.class));
    final ImmutableList<IndexQuery.OrderEntry> orderBooleanDesc = ImmutableList.of(new IndexQuery.OrderEntry(BOOLEAN, Order.DESC, Boolean.class));
    final ImmutableList<IndexQuery.OrderEntry> orderBooleanAsc = ImmutableList.of(new IndexQuery.OrderEntry(BOOLEAN, Order.ASC, Boolean.class));
    clopen();
    for (final String store : stores) {
        // Token
        List<String> result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        assertEquals(ImmutableSet.copyOf(result), tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "wOrLD"))).collect(Collectors.toSet()));
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "bob"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "worl"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "Tomorrow world"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "WorLD HELLO"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_FUZZY, "boby"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN, "A"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN, "z"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN, "world"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN_EQUAL, "A"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN_EQUAL, "z"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.GREATER_THAN_EQUAL, "world"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN, "A"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN, "z"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN, "world"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN_EQUAL, "A"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN_EQUAL, "z"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Cmp.LESS_THAN_EQUAL, "world"))).count());
        // Ordering
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderTimeDesc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new RawQuery(store, "text:\"world\"", orderTimeDesc, NO_PARAS)).map(RawQuery.Result::getResult).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderWeightDesc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new RawQuery(store, "text:\"world\"", orderWeightDesc, NO_PARAS)).map(RawQuery.Result::getResult).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderTimeAsc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new RawQuery(store, "text:\"world\"", orderTimeAsc, NO_PARAS)).map(RawQuery.Result::getResult).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderWeightAsc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new RawQuery(store, "text:\"world\"", orderWeightAsc, NO_PARAS)).map(RawQuery.Result::getResult).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), jointOrder)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderNameAsc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderNameDesc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderDateAsc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderDateDesc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderBooleanDesc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc2", "doc1"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"), orderBooleanAsc)).collect(Collectors.toList());
        assertEquals(ImmutableList.of("doc1", "doc2"), result);
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_PREFIX, "w"))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_PREFIX, "wOr"))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_PREFIX, "bobi"))).count());
        if (index.supports(new StandardKeyInformation(String.class, Cardinality.SINGLE), Text.CONTAINS_REGEX)) {
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_REGEX, "he[l]+(.*)"))).collect(Collectors.toList());
            assertEquals(ImmutableSet.of("doc1", "doc3"), ImmutableSet.copyOf(result));
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_REGEX, "[h]+e[l]+(.*)"))).collect(Collectors.toList());
            assertEquals(ImmutableSet.of("doc1", "doc3"), ImmutableSet.copyOf(result));
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_REGEX, "he[l]+"))).collect(Collectors.toList());
            assertTrue(result.isEmpty());
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS_REGEX, "e[l]+(.*)"))).collect(Collectors.toList());
            assertTrue(result.isEmpty());
        }
        for (final JanusGraphPredicate tp : new Text[] { Text.PREFIX, Text.REGEX }) {
            try {
                assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, tp, "tzubull"))).count());
                if (indexFeatures.supportsStringMapping(Mapping.TEXT))
                    fail();
            } catch (final IllegalArgumentException ignored) {
            }
        }
        // String
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.EQUAL, "Tomorrow is the world"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.EQUAL, "world"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.NOT_EQUAL, "bob"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.PREFIX, "Tomorrow"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.PREFIX, "wor"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.FUZZY, "Tomorow is the world"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN, "A"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN, "z"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN, "Hello world"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN_EQUAL, "A"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN_EQUAL, "z"))).count());
        assertEquals(2, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.GREATER_THAN_EQUAL, "Hello world"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN, "A"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN, "z"))).count());
        assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN, "Hello world"))).count());
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN_EQUAL, "A"))).count());
        assertEquals(3, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN_EQUAL, "z"))).count());
        assertEquals(2, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Cmp.LESS_THAN_EQUAL, "Hello world"))).count());
        try {
            tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Mockito.mock(Cmp.class), "value")));
            fail("should fail");
        } catch (final IllegalArgumentException ignored) {
        }
        for (final JanusGraphPredicate tp : new Text[] { Text.CONTAINS, Text.CONTAINS_PREFIX, Text.CONTAINS_REGEX }) {
            try {
                assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, tp, "tzubull"))).count());
                if (indexFeatures.supportsStringMapping(Mapping.STRING))
                    fail();
            } catch (final IllegalArgumentException ignored) {
            }
        }
        if (index.supports(new StandardKeyInformation(String.class, Cardinality.SINGLE), Text.REGEX)) {
            assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.REGEX, "Tomo[r]+ow is.*world"))).count());
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.REGEX, "Tomorrow"))).count());
        }
        if (index.supports(new StandardKeyInformation(String.class, Cardinality.SINGLE, Mapping.STRING.asParameter()), Text.REGEX)) {
            assertEquals(1, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.REGEX, "Tomo[r]+ow is.*world"))).count());
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(NAME, Text.REGEX, "Tomorrow"))).count());
        }
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "world"), PredicateCondition.of(TEXT, Text.CONTAINS, "hello")))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc1", result.get(0));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TIME, Cmp.EQUAL, -500))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001), PredicateCondition.of(TIME, Cmp.EQUAL, -500))))).collect(Collectors.toList());
        assertEquals(2, result.size());
        result = tx.queryStream(new IndexQuery(store, Not.of(PredicateCondition.of(TEXT, Text.CONTAINS, "world")))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TIME, Cmp.EQUAL, -500), Not.of(PredicateCondition.of(TEXT, Text.CONTAINS, "world"))))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, And.of(Or.of(PredicateCondition.of(TIME, Cmp.EQUAL, 1001), PredicateCondition.of(TIME, Cmp.EQUAL, -500)), PredicateCondition.of(TEXT, Text.CONTAINS, "world")))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc1", result.get(0));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "Bob"))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "Bob")))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "bob"))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc3", result.get(0));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "world"), PredicateCondition.of(WEIGHT, Cmp.GREATER_THAN, 6.0)))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc2", result.get(0));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.box(46.5, -0.5, 50.5, 10.5)))).collect(Collectors.toList());
        assertEquals(3, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2", "doc3"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 200.00)))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.box(46.5, -0.5, 50.5, 10.5)))).collect(Collectors.toList());
        assertEquals(3, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2", "doc3"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 200.00)))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.polygon(Arrays.asList(new double[][] { { -5.0, 47.0 }, { 5.0, 47.0 }, { 5.0, 50.0 }, { -5.0, 50.0 }, { -5.0, 47.0 } }))))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        if (index.supports(new StandardKeyInformation(Geoshape.class, Cardinality.SINGLE, Mapping.PREFIX_TREE.asParameter()), Geo.DISJOINT)) {
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.DISJOINT, Geoshape.box(46.5, -0.5, 50.5, 10.5)))).collect(Collectors.toList());
            assertEquals(0, result.size());
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.DISJOINT, Geoshape.circle(48.5, 0.5, 200.00)))).collect(Collectors.toList());
            assertEquals(1, result.size());
            assertEquals(ImmutableSet.of("doc3"), ImmutableSet.copyOf(result));
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.DISJOINT, Geoshape.polygon(Arrays.asList(new double[][] { { -5.0, 47.0 }, { 5.0, 47.0 }, { 5.0, 50.0 }, { -5.0, 50.0 }, { -5.0, 47.0 } }))))).collect(Collectors.toList());
            assertEquals(1, result.size());
            assertEquals(ImmutableSet.of("doc3"), ImmutableSet.copyOf(result));
        }
        if (indexFeatures.supportsGeoContains()) {
            result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.CONTAINS, Geoshape.point(47, 10)))).collect(Collectors.toList());
            assertEquals(1, result.size());
            assertEquals(ImmutableSet.of("doc3"), ImmutableSet.copyOf(result));
        }
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.INTERSECT, Geoshape.box(48, -1, 49, 2)))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.INTERSECT, Geoshape.circle(48.5, 0.5, 200.00)))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.INTERSECT, Geoshape.polygon(Arrays.asList(new double[][] { { -1.0, 48.0 }, { 2.0, 48.0 }, { 2.0, 49.0 }, { -1.0, 49.0 }, { -1.0, 48.0 } }))))).collect(Collectors.toList());
        assertEquals(2, result.size());
        assertEquals(ImmutableSet.of("doc1", "doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of("text", Text.CONTAINS, "tomorrow"), PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 200.00)), PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 200.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc2"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TIME, Cmp.GREATER_THAN_EQUAL, -1000), PredicateCondition.of(TIME, Cmp.LESS_THAN, 1010), PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 1000.00)), PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(48.5, 0.5, 1000.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc3"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(WEIGHT, Cmp.GREATER_THAN, 10.0)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc3"), ImmutableSet.copyOf(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of("blah", Cmp.GREATER_THAN, 10.0)))).collect(Collectors.toList());
        assertEquals(0, result.size());
        if (supportsLuceneStyleQueries()) {
            assertEquals(1, tx.queryStream(new RawQuery(store, "text:\"Hello Bob\"", NO_PARAS)).count());
            assertEquals(0, tx.queryStream(new RawQuery(store, "text:\"Hello Bob\"", NO_PARAS).setOffset(1)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "text:(world AND tomorrow)", NO_PARAS)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "text:(you there Hello Bob)", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "text:(you there Hello Bob)", NO_PARAS).setLimit(1)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "text:(you there Hello Bob)", NO_PARAS).setLimit(1).setOffset(1)).count());
            assertEquals(0, tx.queryStream(new RawQuery(store, "text:(you there Hello Bob)", NO_PARAS).setLimit(1).setOffset(2)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "text:\"world\"", NO_PARAS)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "time:[1000 TO 1020]", NO_PARAS)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "time:[1000 TO *]", NO_PARAS)).count());
            assertEquals(3, tx.queryStream(new RawQuery(store, "time:[* TO *]", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "weight:[5.1 TO 8.3]", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "weight:5.2", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "text:world AND time:1001", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "name:\"Hello world\"", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "boolean:true", NO_PARAS)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "boolean:false", NO_PARAS)).count());
            assertEquals(2, tx.queryStream(new RawQuery(store, "date:{1970-01-01T00:00:01Z TO 1970-01-01T00:00:03Z]", NO_PARAS)).count());
            assertEquals(3, tx.queryStream(new RawQuery(store, "date:[1970-01-01T00:00:01Z TO *]", NO_PARAS)).count());
            assertEquals(1, tx.queryStream(new RawQuery(store, "date:\"1970-01-01T00:00:02Z\"", NO_PARAS)).count());
        }
        if (index.supports(new StandardKeyInformation(String.class, Cardinality.LIST, Mapping.STRING.asParameter()), Cmp.EQUAL)) {
            assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "1"))).findFirst().get());
            assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "2"))).findFirst().get());
            assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "4"))).findFirst().get());
            assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "5"))).findFirst().get());
            assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "7"))).findFirst().get());
            assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "8"))).findFirst().get());
            assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "1"))).findFirst().get());
            assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "2"))).findFirst().get());
            assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "4"))).findFirst().get());
            assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "5"))).findFirst().get());
            assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "7"))).findFirst().get());
            assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "8"))).findFirst().get());
            remove(store, "doc1", ImmutableMultimap.of(PHONE_LIST, "1"), false);
            clopen();
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "1"))).count());
            assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "2"))).findFirst().get());
            remove(store, "doc2", ImmutableMultimap.of(PHONE_SET, "4"), false);
            clopen();
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "4"))).count());
            assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "5"))).findFirst().get());
        }
        assertEquals("doc1", tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.EQUAL, Instant.ofEpochSecond(1)))).findFirst().get());
        assertEquals("doc2", tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.EQUAL, Instant.ofEpochSecond(2)))).findFirst().get());
        assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.EQUAL, Instant.ofEpochSecond(3)))).findFirst().get());
        assertEquals("doc3", tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.GREATER_THAN, Instant.ofEpochSecond(2)))).findFirst().get());
        assertEquals(ImmutableSet.of("doc2", "doc3"), tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.GREATER_THAN_EQUAL, Instant.ofEpochSecond(2)))).collect(Collectors.toSet()));
        assertEquals(ImmutableSet.of("doc1"), tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.LESS_THAN, Instant.ofEpochSecond(2)))).collect(Collectors.toSet()));
        assertEquals(ImmutableSet.of("doc1", "doc2"), tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.LESS_THAN_EQUAL, Instant.ofEpochSecond(2)))).collect(Collectors.toSet()));
        assertEquals(ImmutableSet.of("doc1", "doc3"), tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.NOT_EQUAL, Instant.ofEpochSecond(2)))).collect(Collectors.toSet()));
        // Update some data
        add(store, "doc4", getDocument("It's all a big Bob", -100, 11.2, Geoshape.point(-48.0, 8.0), Geoshape.point(-48.0, 8.0), Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(4), false), true);
        remove(store, "doc2", doc2, true);
        remove(store, "doc3", ImmutableMultimap.of(WEIGHT, 10.1), false);
        add(store, "doc3", ImmutableMultimap.of(TIME, 2000, TEXT, "Bob owns the world"), false);
        remove(store, "doc1", ImmutableMultimap.of(TIME, 1001), false);
        add(store, "doc1", ImmutableMultimap.of(TIME, 1005, WEIGHT, 11.1, LOCATION, Geoshape.point(-48.0, 0.0), BOUNDARY, Geoshape.circle(-48.0, 0.0, 1.0)), false);
        final Geoshape multiPoint = Geoshape.geoshape(Geoshape.getShapeFactory().multiPoint().pointXY(60.0, 60.0).pointXY(120.0, 60.0).build());
        add(store, "doc5", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPoint, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400), false), true);
        final Geoshape multiLine = Geoshape.geoshape(Geoshape.getShapeFactory().multiLineString().add(Geoshape.getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)).add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build());
        add(store, "doc6", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiLine, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400), false), true);
        final Geoshape multiPolygon = Geoshape.geoshape(Geoshape.getShapeFactory().multiPolygon().add(Geoshape.getShapeFactory().polygon().pointXY(59.0, 59.0).pointXY(61.0, 59.0).pointXY(61.0, 61.0).pointXY(59.0, 61.0).pointXY(59.0, 59.0)).add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0).pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build());
        add(store, "doc7", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), multiPolygon, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400), false), true);
        final Geoshape geometryCollection = Geoshape.geoshape(Geoshape.getGeometryCollectionBuilder().add(Geoshape.getShapeFactory().pointXY(60.0, 60.0)).add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()).build());
        add(store, "doc8", getDocument("A Full Yes", -100, -11.2, Geoshape.point(48.0, 8.0), geometryCollection, Arrays.asList("10", "11", "12"), Sets.newHashSet("10", "11"), Instant.ofEpochSecond(400), false), true);
    }
    clopen();
    for (final String store : stores) {
        List<String> result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(TEXT, Text.CONTAINS, "world"))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc3"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "world"), PredicateCondition.of(WEIGHT, Cmp.GREATER_THAN, 6.0)))).collect(Collectors.toList());
        assertEquals(1, result.size());
        assertEquals("doc1", result.get(0));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 200.00)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 200.00)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "tomorrow"), PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 200.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of(), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TEXT, Text.CONTAINS, "tomorrow"), PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 200.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of(), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TIME, Cmp.GREATER_THAN_EQUAL, -1000), PredicateCondition.of(TIME, Cmp.LESS_THAN, 1010), PredicateCondition.of(LOCATION, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 1000.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc4"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(TIME, Cmp.GREATER_THAN_EQUAL, -1000), PredicateCondition.of(TIME, Cmp.LESS_THAN, 1010), PredicateCondition.of(BOUNDARY, Geo.WITHIN, Geoshape.circle(-48.5, 0.5, 1000.00))))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc4"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of(WEIGHT, Cmp.GREATER_THAN, 10.0)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc1", "doc4"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, And.of(PredicateCondition.of("blah", Cmp.GREATER_THAN, 10.0)))).collect(Collectors.toList());
        assertEquals(0, result.size());
        if (index.supports(new StandardKeyInformation(String.class, Cardinality.LIST, new Parameter<>("mapping", Mapping.STRING)), Cmp.EQUAL)) {
            for (int suffix = 4; suffix <= 8; suffix++) {
                final String suffixString = "doc" + suffix;
                assertTrue(tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "10"))).anyMatch(suffixString::equals));
                assertTrue(tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "11"))).anyMatch(suffixString::equals));
                assertTrue(tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "10"))).anyMatch(suffixString::equals));
                assertTrue(tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_SET, Cmp.EQUAL, "11"))).anyMatch(suffixString::equals));
            }
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "4"))).count());
            assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(PHONE_LIST, Cmp.EQUAL, "5"))).count());
        }
        assertEquals(0, tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.EQUAL, Instant.ofEpochSecond(2)))).count());
        assertEquals("doc4", tx.queryStream(new IndexQuery(store, PredicateCondition.of(DATE, Cmp.EQUAL, Instant.ofEpochSecond(4)))).findFirst().get());
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.INTERSECT, Geoshape.circle(59, 59, 200.00)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc5", "doc6", "doc7", "doc8"), Sets.newHashSet(result));
        result = tx.queryStream(new IndexQuery(store, PredicateCondition.of(BOUNDARY, Geo.INTERSECT, Geoshape.circle(59, 119, 200.00)))).collect(Collectors.toList());
        assertEquals(ImmutableSet.of("doc5", "doc6", "doc7", "doc8"), Sets.newHashSet(result));
    }
}
Also used : Instant(java.time.Instant) Cmp(org.janusgraph.core.attribute.Cmp) Geoshape(org.janusgraph.core.attribute.Geoshape) Text(org.janusgraph.core.attribute.Text) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Parameter(org.janusgraph.core.schema.Parameter)

Aggregations

JanusGraphPredicate (org.janusgraph.graphdb.query.JanusGraphPredicate)6 Instant (java.time.Instant)4 Cmp (org.janusgraph.core.attribute.Cmp)4 Geoshape (org.janusgraph.core.attribute.Geoshape)4 Date (java.util.Date)3 UUID (java.util.UUID)3 Geo (org.janusgraph.core.attribute.Geo)3 Mapping (org.janusgraph.core.schema.Mapping)3 And (org.janusgraph.graphdb.query.condition.And)3 Condition (org.janusgraph.graphdb.query.condition.Condition)3 Not (org.janusgraph.graphdb.query.condition.Not)3 Or (org.janusgraph.graphdb.query.condition.Or)3 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)3 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)1 Multimap (com.google.common.collect.Multimap)1