Search in sources :

Example 6 with IndexQueryConstraints

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

the class FulltextIndexProgressorTest method mustExhaustIteratorWhenLimitIsGreaterThanIterator.

@Test
void mustExhaustIteratorWhenLimitIsGreaterThanIterator() {
    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().limit(5);
    StubEntityValueClient client = new StubEntityValueClient();
    FulltextIndexProgressor progressor = new FulltextIndexProgressor(iterator, client, constraints);
    boolean keepGoing;
    do {
        keepGoing = progressor.next();
    } while (keepGoing);
    assertThat(client.entityIds).containsExactly(1L, 2L, 3L, 4L);
    assertThat(client.scores).containsExactly(1.0f, 2.0f, 3.0f, 4.0f);
}
Also used : IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) Test(org.junit.jupiter.api.Test)

Example 7 with IndexQueryConstraints

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

the class FulltextProcedures method queryFulltextForRelationships.

@SystemProcedure
@Description("Query the given full-text index. Returns the matching relationships, 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.queryRelationships", mode = READ)
public Stream<RelationshipOutput> queryFulltextForRelationships(@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 != RELATIONSHIP) {
        throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for relationships.");
    }
    RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
    IndexReadSession indexReadSession = tx.dataRead().indexReadSession(indexReference);
    IndexQueryConstraints constraints = queryConstraints(options);
    tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
    Spliterator<RelationshipOutput> spliterator = new SpliteratorAdaptor<>() {

        @Override
        public boolean tryAdvance(Consumer<? super RelationshipOutput> action) {
            while (cursor.next()) {
                long relationshipReference = cursor.relationshipReference();
                float score = cursor.score();
                RelationshipOutput relationshipOutput = RelationshipOutput.forExistingEntityOrNull(transaction, relationshipReference, score);
                if (relationshipOutput != null) {
                    action.accept(relationshipOutput);
                    return true;
                }
            }
            cursor.close();
            return false;
        }
    };
    return StreamSupport.stream(spliterator, false).onClose(cursor::close);
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) 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 8 with IndexQueryConstraints

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

the class FulltextIndexProviderTest method queryingWithIndexProgressorMustProvideScore.

@Test
void queryingWithIndexProgressorMustProvideScore() throws Exception {
    long nodeId = createTheThirdNode();
    IndexDescriptor index;
    index = createIndex(new int[] { labelIdHej, labelIdHa, labelIdHe }, new int[] { propIdHej, propIdHa, propIdHe, propIdHo });
    await(index);
    List<String> acceptedEntities = new ArrayList<>();
    try (KernelTransactionImplementation ktx = getKernelTransaction()) {
        NodeValueIndexCursor cursor = new ExtendedNodeValueIndexCursorAdapter() {

            private long nodeReference;

            private IndexProgressor progressor;

            @Override
            public long nodeReference() {
                return nodeReference;
            }

            @Override
            public boolean next() {
                return progressor.next();
            }

            @Override
            public void initialize(IndexDescriptor descriptor, IndexProgressor progressor, PropertyIndexQuery[] query, IndexQueryConstraints constraints, boolean indexIncludesTransactionState) {
                this.progressor = progressor;
            }

            @Override
            public boolean acceptEntity(long reference, float score, Value... values) {
                this.nodeReference = reference;
                assertFalse(Float.isNaN(score), "score should not be NaN");
                assertThat(score).as("score must be positive").isGreaterThan(0.0f);
                acceptedEntities.add("reference = " + reference + ", score = " + score + ", " + Arrays.toString(values));
                return true;
            }
        };
        Read read = ktx.dataRead();
        IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
        read.nodeIndexSeek(indexSession, cursor, unconstrained(), fulltextSearch("hej:\"villa\""));
        int counter = 0;
        while (cursor.next()) {
            assertThat(cursor.nodeReference()).isEqualTo(nodeId);
            counter++;
        }
        assertThat(counter).isEqualTo(1);
        assertThat(acceptedEntities.size()).isEqualTo(1);
        acceptedEntities.clear();
    }
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) ArrayList(java.util.ArrayList) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) ExtendedNodeValueIndexCursorAdapter(org.neo4j.kernel.impl.newapi.ExtendedNodeValueIndexCursorAdapter) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Read(org.neo4j.internal.kernel.api.Read) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor) Value(org.neo4j.values.storable.Value) Test(org.junit.jupiter.api.Test)

Example 9 with IndexQueryConstraints

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

the class EntityValueIndexCursorTestBase method shouldPerformBooleanSearch.

@Test
void shouldPerformBooleanSearch() throws KernelException {
    // given
    boolean needsValues = indexParams.indexProvidesBooleanValues();
    IndexQueryConstraints constraints = unordered(needsValues);
    int prop = token.propertyKey(PROP_NAME);
    IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX_NAME));
    IndexValueCapability capability = index.reference().getCapability().valueCapability(ValueGroup.BOOLEAN.category());
    try (var cursor = entityParams.allocateEntityValueIndexCursor(tx, cursors)) {
        MutableLongSet uniqueIds = new LongHashSet();
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, false));
        // then
        assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, true));
        // then
        assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
    }
}
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 10 with IndexQueryConstraints

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

the class EntityValueIndexCursorTestBase method shouldPerformTextArraySearch.

@Test
void shouldPerformTextArraySearch() throws KernelException {
    // given
    boolean needsValues = indexParams.indexProvidesArrayValues();
    IndexQueryConstraints constraints = unordered(needsValues);
    int prop = token.propertyKey(PROP_NAME);
    IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX_NAME));
    IndexValueCapability capability = index.reference().getCapability().valueCapability(ValueGroup.TEXT_ARRAY.category());
    try (var cursor = entityParams.allocateEntityValueIndexCursor(tx, cursors)) {
        MutableLongSet uniqueIds = new LongHashSet();
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, new String[] { "first", "second", "third" }));
        // then
        assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
        // when
        entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, new String[] { "fourth", "fifth", "sixth", "seventh" }));
        // then
        assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
    }
}
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)

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