Search in sources :

Example 11 with IndexQueryConstraints

use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.

the class EntityValueIndexCursorTestBase method shouldPerformNumericRangeSearch.

@Test
void shouldPerformNumericRangeSearch() throws Exception {
    // given
    boolean needsValues = indexParams.indexProvidesNumericValues();
    IndexQueryConstraints constraints = unordered(needsValues);
    int prop = token.propertyKey(PROP_NAME);
    IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX_NAME));
    IndexValueCapability numberCapability = index.reference().getCapability().valueCapability(ValueCategory.NUMBER);
    try (var cursor = entityParams.allocateEntityValueIndexCursor(tx, cursors)) {
        MutableLongSet uniqueIds = new LongHashSet();
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.range(prop, 5, true, 12, true));
        // then
        assertFoundEntitiesAndValue(cursor, uniqueIds, numberCapability, needsValues, num5, num6, num12a, num12b);
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.range(prop, 5, true, 12, false));
        // then
        assertFoundEntitiesAndValue(cursor, uniqueIds, numberCapability, needsValues, num5, num6);
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.range(prop, 5, false, 12, true));
        // then
        assertFoundEntitiesAndValue(cursor, uniqueIds, numberCapability, needsValues, num6, num12a, num12b);
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.range(prop, 5, false, 12, false));
        // then
        assertFoundEntitiesAndValue(cursor, uniqueIds, numberCapability, needsValues, num6);
    }
}
Also used : LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) IndexValueCapability(org.neo4j.internal.schema.IndexValueCapability) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 12 with IndexQueryConstraints

use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.

the class FulltextIndexProgressorTest method mustSkipAndLimitEntriesPerConstraints.

@Test
void mustSkipAndLimitEntriesPerConstraints() {
    StubValuesIterator iterator = new StubValuesIterator();
    iterator.add(1, 1.0f);
    iterator.add(2, 2.0f);
    iterator.add(3, 3.0f);
    iterator.add(4, 4.0f);
    IndexQueryConstraints constraints = unconstrained().skip(1).limit(2);
    StubEntityValueClient client = new StubEntityValueClient();
    FulltextIndexProgressor progressor = new FulltextIndexProgressor(iterator, client, constraints);
    boolean keepGoing;
    do {
        keepGoing = progressor.next();
    } while (keepGoing);
    assertThat(client.entityIds).containsExactly(2L, 3L);
    assertThat(client.scores).containsExactly(2.0f, 3.0f);
}
Also used : IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) Test(org.junit.jupiter.api.Test)

Example 13 with IndexQueryConstraints

use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.

the class FulltextProcedures method queryConstraints.

protected static IndexQueryConstraints queryConstraints(Map<String, Object> options) {
    IndexQueryConstraints constraints = unconstrained();
    Object skip;
    if ((skip = options.get("skip")) != null && skip instanceof Number) {
        constraints = constraints.skip(((Number) skip).longValue());
    }
    Object limit;
    if ((limit = options.get("limit")) != null && limit instanceof Number) {
        constraints = constraints.limit(((Number) limit).longValue());
    }
    return constraints;
}
Also used : IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints)

Example 14 with IndexQueryConstraints

use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.

the class FulltextProcedures method queryFulltextForNodes.

@SystemProcedure
@Description("Query the given full-text index. Returns the matching nodes, and their Lucene query score, ordered by score. " + "Valid keys for the options map are: 'skip' to skip the top N results; 'limit' to limit the number of results returned.")
@Procedure(name = "db.index.fulltext.queryNodes", mode = READ)
public Stream<NodeOutput> queryFulltextForNodes(@Name("indexName") String name, @Name("queryString") String query, @Name(value = "options", defaultValue = "{}") Map<String, Object> options) throws Exception {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    IndexDescriptor indexReference = getValidIndex(name);
    awaitOnline(indexReference);
    EntityType entityType = indexReference.schema().entityType();
    if (entityType != NODE) {
        throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for nodes.");
    }
    NodeValueIndexCursor cursor = tx.cursors().allocateNodeValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
    IndexReadSession indexSession = tx.dataRead().indexReadSession(indexReference);
    IndexQueryConstraints constraints = queryConstraints(options);
    tx.dataRead().nodeIndexSeek(indexSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
    Spliterator<NodeOutput> spliterator = new SpliteratorAdaptor<>() {

        @Override
        public boolean tryAdvance(Consumer<? super NodeOutput> action) {
            while (cursor.next()) {
                long nodeReference = cursor.nodeReference();
                float score = cursor.score();
                NodeOutput nodeOutput = NodeOutput.forExistingEntityOrNull(transaction, nodeReference, score);
                if (nodeOutput != null) {
                    action.accept(nodeOutput);
                    return true;
                }
            }
            cursor.close();
            return false;
        }
    };
    Stream<NodeOutput> stream = StreamSupport.stream(spliterator, false);
    return stream.onClose(cursor::close);
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EntityType(org.neo4j.common.EntityType) Consumer(java.util.function.Consumer) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 15 with IndexQueryConstraints

use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.

the class TokenIndexAccessorTest method assertReaderFindsExpected.

private static void assertReaderFindsExpected(TokenIndexReader reader, IndexOrder indexOrder, long tokenId, LongList expectedIds, ThrowingConsumer<TokenIndexReader, Exception> innerCalling) throws Exception {
    if (indexOrder.equals(IndexOrder.DESCENDING)) {
        expectedIds = expectedIds.toReversed();
    }
    try (CollectingEntityTokenClient collectingEntityTokenClient = new CollectingEntityTokenClient(tokenId)) {
        IndexQueryConstraints constraint = IndexQueryConstraints.constrained(indexOrder, false);
        TokenPredicate query = new TokenPredicate((int) tokenId);
        reader.query(collectingEntityTokenClient, constraint, query, NULL);
        // Then
        int count = 0;
        while (collectingEntityTokenClient.next()) {
            innerCalling.accept(reader);
            count++;
        }
        assertThat(count).isEqualTo(expectedIds.size());
        assertThat(collectingEntityTokenClient.actualIds).isEqualTo(expectedIds);
    }
}
Also used : TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints)

Aggregations

IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)15 Test (org.junit.jupiter.api.Test)11 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)9 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)6 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)6 IndexValueCapability (org.neo4j.internal.schema.IndexValueCapability)6 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)3 Consumer (java.util.function.Consumer)2 EntityType (org.neo4j.common.EntityType)2 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)2 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)2 Description (org.neo4j.procedure.Description)2 Procedure (org.neo4j.procedure.Procedure)2 ArrayList (java.util.ArrayList)1 Read (org.neo4j.internal.kernel.api.Read)1 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)1 TokenPredicate (org.neo4j.internal.kernel.api.TokenPredicate)1 TokenRead (org.neo4j.internal.kernel.api.TokenRead)1 IndexProgressor (org.neo4j.kernel.api.index.IndexProgressor)1 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)1