Search in sources :

Example 41 with NodeValueIndexCursor

use of org.neo4j.internal.kernel.api.NodeValueIndexCursor 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 42 with NodeValueIndexCursor

use of org.neo4j.internal.kernel.api.NodeValueIndexCursor 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)

Aggregations

NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)42 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)25 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)20 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)20 Test (org.junit.jupiter.api.Test)18 Transaction (org.neo4j.graphdb.Transaction)16 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)16 ArrayList (java.util.ArrayList)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 TokenRead (org.neo4j.internal.kernel.api.TokenRead)7 Read (org.neo4j.internal.kernel.api.Read)6 Value (org.neo4j.values.storable.Value)6 KernelException (org.neo4j.exceptions.KernelException)3 Node (org.neo4j.graphdb.Node)3 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)2 Label (org.neo4j.graphdb.Label)2 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)2 NodeIndexCursor (org.neo4j.internal.kernel.api.NodeIndexCursor)2 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)2