Search in sources :

Example 1 with SimpleIndexReader

use of org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemaIndex method getLuceneIndexReader.

private SimpleIndexReader getLuceneIndexReader(String label, String key) throws SchemaRuleNotFoundException, IndexNotFoundKernelException, DuplicateSchemaRuleException {
    try (KernelStatement stmt = (KernelStatement) tx.acquireStatement()) {
        ReadOperations reads = stmt.readOperations();
        LabelSchemaDescriptor labelSchemaDescriptor = new LabelSchemaDescriptor(reads.labelGetForName(label), reads.propertyKeyGetForName(key));
        IndexDescriptor descriptor = reads.indexGetForSchema(labelSchemaDescriptor);
        IndexReader indexReader = stmt.getStoreStatement().getIndexReader(descriptor);
        if (indexReader.getClass().getName().endsWith("FusionIndexReader")) {
            try {
                Field field = indexReader.getClass().getDeclaredField("luceneReader");
                field.setAccessible(true);
                return (SimpleIndexReader) field.get(indexReader);
            } catch (IllegalAccessException | NoSuchFieldException e) {
                throw new RuntimeException("Error accessing index reader", e);
            }
        }
        return (SimpleIndexReader) indexReader;
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Field(java.lang.reflect.Field) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) SimpleIndexReader(org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader) SimpleIndexReader(org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader) SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) LabelSchemaDescriptor(org.neo4j.kernel.api.schema.LabelSchemaDescriptor) IndexDescriptor(org.neo4j.kernel.api.schema.index.IndexDescriptor)

Example 2 with SimpleIndexReader

use of org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemaIndex method distinctTerms.

private List<Object> distinctTerms(@Name("label") String label, @Name("key") String key) throws SchemaRuleNotFoundException, IndexNotFoundKernelException, IOException, DuplicateSchemaRuleException {
    SimpleIndexReader reader = getLuceneIndexReader(label, key);
    SortedIndexReader sortedIndexReader = new SortedIndexReader(reader, 0, Sort.INDEXORDER);
    Set<Object> values = new LinkedHashSet<>(100);
    TermsEnum termsEnum;
    Fields fields = MultiFields.getFields(sortedIndexReader.getIndexSearcher().getIndexReader());
    Terms terms = fields.terms("string");
    if (terms != null) {
        termsEnum = terms.iterator();
        while ((termsEnum.next()) != null) {
            values.add(termsEnum.term().utf8ToString());
        }
    }
    return new ArrayList<>(values);
}
Also used : SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) SimpleIndexReader(org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader) Terms(org.apache.lucene.index.Terms) TermsEnum(org.apache.lucene.index.TermsEnum)

Aggregations

SimpleIndexReader (org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader)2 SortedIndexReader (org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader)2 Field (java.lang.reflect.Field)1 Fields (org.apache.lucene.index.Fields)1 MultiFields (org.apache.lucene.index.MultiFields)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 ReadOperations (org.neo4j.kernel.api.ReadOperations)1 LabelSchemaDescriptor (org.neo4j.kernel.api.schema.LabelSchemaDescriptor)1 IndexDescriptor (org.neo4j.kernel.api.schema.index.IndexDescriptor)1 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)1 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)1