Search in sources :

Example 6 with IndexProgressor

use of org.neo4j.kernel.api.index.IndexProgressor in project neo4j by neo4j.

the class RelationshipIndexedRelationshipStoreScanTest method mockIdsReturnedFromTokenQueries.

private void mockIdsReturnedFromTokenQueries() {
    // Make token index reader return different ids for the different tokens
    doAnswer(invocation -> {
        IndexProgressor.EntityTokenClient client = invocation.getArgument(0);
        TokenPredicate token = invocation.getArgument(2);
        client.initialize(new IndexProgressor() {

            private final PrimitiveIterator.OfLong relationshipsWithType1 = Arrays.stream(new long[] { 1, 2, 4, 8 }).iterator();

            private final PrimitiveIterator.OfLong relationshipsWithType2 = Arrays.stream(new long[] { 2, 5, 6 }).iterator();

            @Override
            public boolean next() {
                PrimitiveIterator.OfLong relationshipsWithType = relationshipsWithType1;
                if (token.tokenId() == 2) {
                    relationshipsWithType = relationshipsWithType2;
                }
                if (relationshipsWithType.hasNext()) {
                    client.acceptEntity(relationshipsWithType.nextLong(), null);
                    return true;
                }
                return false;
            }

            @Override
            public void close() {
            }
        }, token.tokenId(), IndexOrder.NONE);
        return null;
    }).when(relationshipTypeScanReader).query(any(), any(), any(), any(), any());
}
Also used : PrimitiveIterator(java.util.PrimitiveIterator) TokenPredicate(org.neo4j.internal.kernel.api.TokenPredicate) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor)

Example 7 with IndexProgressor

use of org.neo4j.kernel.api.index.IndexProgressor 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 8 with IndexProgressor

use of org.neo4j.kernel.api.index.IndexProgressor in project neo4j by neo4j.

the class FulltextIndexReader method query.

@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... queries) throws IndexNotApplicableKernelException {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (PropertyIndexQuery indexQuery : queries) {
        if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.fulltextSearch) {
            PropertyIndexQuery.FulltextSearchPredicate fulltextSearch = (PropertyIndexQuery.FulltextSearchPredicate) indexQuery;
            try {
                queryBuilder.add(parseFulltextQuery(fulltextSearch.query()), BooleanClause.Occur.SHOULD);
            } catch (ParseException e) {
                throw new RuntimeException("Could not parse the given fulltext search query: '" + fulltextSearch.query() + "'.", e);
            }
        } else {
            // Not fulltext query
            assertNotComposite(queries);
            assertCypherCompatible();
            Query query;
            if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringContains) {
                PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) indexQuery;
                String searchTerm = QueryParser.escape(scp.contains().stringValue());
                Term term = new Term(propertyNames[0], "*" + searchTerm + "*");
                query = new WildcardQuery(term);
            } else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringSuffix) {
                PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) indexQuery;
                String searchTerm = QueryParser.escape(ssp.suffix().stringValue());
                Term term = new Term(propertyNames[0], "*" + searchTerm);
                query = new WildcardQuery(term);
            } else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringPrefix) {
                PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) indexQuery;
                String searchTerm = spp.prefix().stringValue();
                Term term = new Term(propertyNames[0], searchTerm);
                query = new LuceneDocumentStructure.PrefixMultiTermsQuery(term);
            } else if (indexQuery.getClass() == PropertyIndexQuery.ExactPredicate.class && indexQuery.valueGroup() == ValueGroup.TEXT) {
                PropertyIndexQuery.ExactPredicate exact = (PropertyIndexQuery.ExactPredicate) indexQuery;
                String searchTerm = ((TextValue) exact.value()).stringValue();
                Term term = new Term(propertyNames[0], searchTerm);
                query = new ConstantScoreQuery(new TermQuery(term));
            } else if (indexQuery.getClass() == PropertyIndexQuery.TextRangePredicate.class) {
                PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) indexQuery;
                query = newRangeSeekByStringQuery(propertyNames[0], sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
            } else {
                throw new IndexNotApplicableKernelException("A fulltext schema index cannot answer " + indexQuery.type() + " queries on " + indexQuery.valueCategory() + " values.");
            }
            queryBuilder.add(query, BooleanClause.Occur.MUST);
        }
    }
    Query query = queryBuilder.build();
    ValuesIterator itr = searchLucene(query, constraints, context, context.cursorContext(), context.memoryTracker());
    IndexProgressor progressor = new FulltextIndexProgressor(itr, client, constraints);
    client.initialize(index, progressor, queries, constraints, true);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) LuceneDocumentStructure(org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor) ParseException(org.apache.lucene.queryparser.classic.ParseException) ValuesIterator(org.neo4j.kernel.api.impl.index.collector.ValuesIterator)

Example 9 with IndexProgressor

use of org.neo4j.kernel.api.index.IndexProgressor in project neo4j by neo4j.

the class FusionIndexReaderTest method closeIteratorMustCloseAll.

// close iterator
@Test
void closeIteratorMustCloseAll() throws Exception {
    // given
    IndexProgressor[] progressors = new IndexProgressor[aliveReaders.length];
    for (int i = 0; i < aliveReaders.length; i++) {
        int slot = i;
        doAnswer(invocation -> {
            IndexProgressor.EntityValueClient client = invocation.getArgument(1);
            IndexProgressor progressor = mock(IndexProgressor.class);
            client.initialize(DESCRIPTOR, progressor, getIndexQueryArgument(invocation), invocation.getArgument(2), false);
            progressors[slot] = progressor;
            return null;
        }).when(aliveReaders[i]).query(any(), any(), any(), any());
    }
    // when
    try (NodeValueIterator iterator = new NodeValueIterator()) {
        fusionIndexReader.query(NULL_CONTEXT, iterator, unconstrained(), PropertyIndexQuery.exists(PROP_KEY));
    }
    // then
    for (IndexProgressor progressor : progressors) {
        verify(progressor).close();
    }
}
Also used : NodeValueIterator(org.neo4j.kernel.impl.index.schema.NodeValueIterator) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor) Test(org.junit.jupiter.api.Test)

Aggregations

IndexProgressor (org.neo4j.kernel.api.index.IndexProgressor)9 Test (org.junit.jupiter.api.Test)4 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 IndexReaderStub (org.neo4j.kernel.api.impl.index.IndexReaderStub)2 ArrayList (java.util.ArrayList)1 PrimitiveIterator (java.util.PrimitiveIterator)1 Term (org.apache.lucene.index.Term)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 Query (org.apache.lucene.search.Query)1 TermQuery (org.apache.lucene.search.TermQuery)1 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)1 WildcardQuery (org.apache.lucene.search.WildcardQuery)1 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)1 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)1 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)1 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)1