Search in sources :

Example 1 with SortedIndexReader

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

the class SchemaIndex method distinctTermsCount.

private Map<String, Integer> distinctTermsCount(@Name("label") String label, @Name("key") String key) {
    try {
        SortedIndexReader sortedIndexReader = getSortedIndexReader(label, key, 0, Sort.INDEXORDER);
        Fields fields = MultiFields.getFields(sortedIndexReader.getIndexSearcher().getIndexReader());
        Map<String, Integer> values = new HashMap<>();
        TermsEnum termsEnum;
        Terms terms = fields.terms("string");
        if (terms != null) {
            termsEnum = terms.iterator();
            while ((termsEnum.next()) != null) {
                values.put(termsEnum.term().utf8ToString(), termsEnum.docFreq());
            }
        }
        return values;
    } catch (Exception e) {
        throw new RuntimeException("Error collecting distinct terms of label: " + label + " and key: " + key, e);
    }
}
Also used : SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) Fields(org.apache.lucene.index.Fields) MultiFields(org.apache.lucene.index.MultiFields) Terms(org.apache.lucene.index.Terms) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) DuplicateSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.DuplicateSchemaRuleException) IOException(java.io.IOException) IndexNotApplicableKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 2 with SortedIndexReader

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

the class SchemaIndex method orderedByText.

@Procedure
@Description("apoc.index.orderedByText(label,key,operator,value,sort-relevance,limit) yield node - schema string search which keeps index order and adds limit, operator is 'STARTS WITH' or 'CONTAINS'")
public Stream<NodeResult> orderedByText(@Name("label") String label, @Name("key") String key, @Name("operator") String operator, @Name("value") String value, @Name("relevance") boolean relevance, @Name("limit") long limit) throws SchemaRuleNotFoundException, IndexNotFoundKernelException, DuplicateSchemaRuleException {
    SortedIndexReader sortedIndexReader = getSortedIndexReader(label, key, limit, getSort(value, value, relevance));
    PrimitiveLongIterator it = queryForString(sortedIndexReader, operator, value);
    return Util.toLongStream(it).mapToObj(id -> new NodeResult(db.getNodeById(id)));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) NodeResult(apoc.result.NodeResult) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 3 with SortedIndexReader

use of org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader 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

SortedIndexReader (org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader)3 Fields (org.apache.lucene.index.Fields)2 MultiFields (org.apache.lucene.index.MultiFields)2 Terms (org.apache.lucene.index.Terms)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 NodeResult (apoc.result.NodeResult)1 IOException (java.io.IOException)1 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)1 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)1 IndexNotApplicableKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException)1 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)1 DuplicateSchemaRuleException (org.neo4j.kernel.api.exceptions.schema.DuplicateSchemaRuleException)1 SchemaRuleNotFoundException (org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)1 SimpleIndexReader (org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader)1 Description (org.neo4j.procedure.Description)1 Procedure (org.neo4j.procedure.Procedure)1