Example usage for org.apache.lucene.index LeafReader document

List of usage examples for org.apache.lucene.index LeafReader document

Introduction

In this page you can find the example usage for org.apache.lucene.index LeafReader document.

Prototype

public abstract void document(int docID, StoredFieldVisitor visitor) throws IOException;

Source Link

Document

Expert: visits the fields of a stored document, for custom processing/loading of each field.

Usage

From source file:io.crate.execution.engine.collect.collectors.LuceneBatchIterator.java

License:Apache License

private void onDoc(int doc, LeafReader reader) throws IOException {
    checkCircuitBreaker();/*from w  w  w  . ja  v a  2 s  .c  o m*/
    if (visitor.required()) {
        visitor.reset();
        reader.document(doc, visitor);
    }
    for (LuceneCollectorExpression expression : expressions) {
        expression.setNextDocId(doc);
    }
}

From source file:org.elasticsearch.index.percolator.PercolatorQueryCache.java

License:Apache License

QueriesLeaf loadQueries(LeafReaderContext context, IndexShard indexShard) throws IOException {
    Version indexVersionCreated = indexShard.indexSettings().getIndexVersionCreated();
    MapperService mapperService = indexShard.mapperService();
    LeafReader leafReader = context.reader();
    ShardId shardId = ShardUtils.extractShardId(leafReader);
    if (shardId == null) {
        throw new IllegalStateException("can't resolve shard id");
    }//from   w w w .j  a  v  a2 s. c o m
    if (indexSettings.getIndex().equals(shardId.getIndex()) == false) {
        // percolator cache insanity
        String message = "Trying to load queries for index " + shardId.getIndex() + " with cache of index "
                + indexSettings.getIndex();
        throw new IllegalStateException(message);
    }

    IntObjectHashMap<Query> queries = new IntObjectHashMap<>();
    boolean legacyLoading = indexVersionCreated.before(Version.V_5_0_0_alpha1);
    if (legacyLoading) {
        PostingsEnum postings = leafReader.postings(new Term(TypeFieldMapper.NAME, LEGACY_TYPE_NAME),
                PostingsEnum.NONE);
        if (postings != null) {
            LegacyQueryFieldVisitor visitor = new LegacyQueryFieldVisitor();
            for (int docId = postings.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = postings
                    .nextDoc()) {
                leafReader.document(docId, visitor);
                queries.put(docId, parseLegacyPercolatorDocument(docId, visitor.source));
                visitor.source = null; // reset
            }
        }
    } else {
        // Each type can have one percolator field mapper,
        // So for each type we check if there is a percolator field mapper
        // and parse all the queries for the documents of that type.
        IndexSearcher indexSearcher = new IndexSearcher(leafReader);
        for (DocumentMapper documentMapper : mapperService.docMappers(false)) {
            Weight queryWeight = indexSearcher.createNormalizedWeight(documentMapper.typeFilter(), false);
            for (FieldMapper fieldMapper : documentMapper.mappers()) {
                if (fieldMapper instanceof PercolatorFieldMapper) {
                    PercolatorFieldType fieldType = (PercolatorFieldType) fieldMapper.fieldType();
                    BinaryDocValues binaryDocValues = leafReader
                            .getBinaryDocValues(fieldType.getQueryBuilderFieldName());
                    if (binaryDocValues != null) {
                        // use the same leaf reader context the indexSearcher is using too:
                        Scorer scorer = queryWeight.scorer(leafReader.getContext());
                        if (scorer != null) {
                            DocIdSetIterator iterator = scorer.iterator();
                            for (int docId = iterator
                                    .nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = iterator
                                            .nextDoc()) {
                                BytesRef qbSource = binaryDocValues.get(docId);
                                if (qbSource.length > 0) {
                                    queries.put(docId, parseQueryBuilder(docId, qbSource));
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    leafReader.addCoreClosedListener(this);
    return new QueriesLeaf(shardId, queries);
}

From source file:org.elasticsearch.index.shard.IndexShardTestCase.java

License:Apache License

protected Set<Uid> getShardDocUIDs(final IndexShard shard) throws IOException {
    shard.refresh("get_uids");
    try (Engine.Searcher searcher = shard.acquireSearcher("test")) {
        Set<Uid> ids = new HashSet<>();
        for (LeafReaderContext leafContext : searcher.reader().leaves()) {
            LeafReader reader = leafContext.reader();
            Bits liveDocs = reader.getLiveDocs();
            for (int i = 0; i < reader.maxDoc(); i++) {
                if (liveDocs == null || liveDocs.get(i)) {
                    Document uuid = reader.document(i, Collections.singleton(UidFieldMapper.NAME));
                    ids.add(Uid.createUid(uuid.get(UidFieldMapper.NAME)));
                }// w  ww  .ja  v a  2 s  .  c om
            }
        }
        return ids;
    }
}

From source file:org.elasticsearch.percolator.PercolateQueryBuilder.java

License:Apache License

private static PercolateQuery.QueryStore createLegacyStore(QueryShardContext context,
        boolean mapUnmappedFieldsAsString) {
    return ctx -> {
        LeafReader leafReader = ctx.reader();
        return docId -> {
            LegacyQueryFieldVisitor visitor = new LegacyQueryFieldVisitor();
            leafReader.document(docId, visitor);
            if (visitor.source == null) {
                throw new IllegalStateException("No source found for document with docid [" + docId + "]");
            }// w  w w . j  av a  2s.c  o m

            try (XContentParser sourceParser = XContentHelper.createParser(visitor.source)) {
                String currentFieldName = null;
                XContentParser.Token token = sourceParser.nextToken(); // move the START_OBJECT
                if (token != XContentParser.Token.START_OBJECT) {
                    throw new ElasticsearchException(
                            "failed to parse query [" + docId + "], not starting with OBJECT");
                }
                while ((token = sourceParser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = sourceParser.currentName();
                    } else if (token == XContentParser.Token.START_OBJECT) {
                        if ("query".equals(currentFieldName)) {
                            return parseQuery(context, mapUnmappedFieldsAsString, sourceParser);
                        } else {
                            sourceParser.skipChildren();
                        }
                    } else if (token == XContentParser.Token.START_ARRAY) {
                        sourceParser.skipChildren();
                    }
                }
            }
            return null;
        };
    };
}

From source file:org.elasticsearch.percolator.PercolatorQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores);
    return new Weight(this) {
        @Override/*from   w  w  w .  j av  a2  s .  c  om*/
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                int result = scorer.iterator().advance(docId);
                if (result == docId) {
                    return Explanation.match(scorer.score(), "PercolatorQuery");
                }
            }
            return Explanation.noMatch("PercolatorQuery");
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return innerWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            innerWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = innerWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }

            final LeafReader leafReader = leafReaderContext.reader();
            return new Scorer(this) {

                @Override
                public DocIdSetIterator iterator() {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                }

                @Override
                public TwoPhaseIterator twoPhaseIterator() {
                    return new TwoPhaseIterator(approximation.iterator()) {
                        @Override
                        public boolean matches() throws IOException {
                            return matchDocId(approximation.docID(), leafReader);
                        }

                        @Override
                        public float matchCost() {
                            return MATCH_COST;
                        }
                    };
                }

                @Override
                public float score() throws IOException {
                    return approximation.score();
                }

                @Override
                public int freq() throws IOException {
                    return approximation.freq();
                }

                @Override
                public int docID() {
                    return approximation.docID();
                }

                boolean matchDocId(int docId, LeafReader leafReader) throws IOException {
                    SingleFieldsVisitor singleFieldsVisitor = new SingleFieldsVisitor(UidFieldMapper.NAME);
                    leafReader.document(docId, singleFieldsVisitor);
                    BytesRef percolatorQueryId = new BytesRef(singleFieldsVisitor.uid().id());
                    return matchQuery(percolatorQueryId);
                }
            };
        }
    };
}

From source file:org.geotoolkit.lucene.filter.LuceneOGCFilter.java

License:Open Source License

/**
 * {@inheritDoc }//from  ww  w.java 2s . co m
 */
@Override
public DocIdSet getDocIdSet(final LeafReaderContext ctx, final Bits b) throws IOException {

    boolean treeSearch = false;
    boolean reverse = false;
    boolean distanceFilter = false;
    final Set<String> treeMatching = new HashSet<>();
    if (tree != null) {
        /*
         * For distance buffer filter no envelope only mode
         */
        if (filter instanceof DistanceBufferOperator) {
            distanceFilter = true;
            reverse = filter instanceof Beyond;
            final DistanceBufferOperator sp = (DistanceBufferOperator) filter;
            if (sp.getExpression2() instanceof Literal) {
                try {
                    final Literal lit = (Literal) sp.getExpression2();
                    final GeneralEnvelope bound = getExtendedReprojectedEnvelope(lit.getValue(), tree.getCrs(),
                            sp.getDistanceUnits(), sp.getDistance());
                    final int[] resultID = tree.searchID(bound);
                    Arrays.sort(resultID);
                    treeMatching.clear();
                    TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                    for (int id : resultID) {
                        final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                        if (env != null) {
                            treeMatching.add(env.getId());
                        }
                    }
                    treeSearch = true;
                } catch (FactoryException ex) {
                    throw new IOException(ex);
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", sp.getExpression2());
            }

        } else if (filter instanceof BinarySpatialOperator) {
            final BinarySpatialOperator sp = (BinarySpatialOperator) filter;
            if (sp.getExpression2() instanceof Literal) {
                final Literal lit = (Literal) sp.getExpression2();
                final Envelope boundFilter = getReprojectedEnvelope(lit.getValue(), tree.getCrs());
                try {
                    if (filterType == SpatialFilterType.CROSSES || !envelopeOnly) {
                        if (filterType == SpatialFilterType.DISJOINT) {
                            reverse = true;
                        }
                        final int[] resultID = tree.searchID(boundFilter);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                        envelopeOnly = false;
                    } else {
                        final int[] resultID = TreeX.search(tree, boundFilter, filterType);
                        Arrays.sort(resultID);
                        final TreeElementMapper<NamedEnvelope> tem = tree.getTreeElementMapper();
                        treeMatching.clear();
                        for (int id : resultID) {
                            final NamedEnvelope env = tem.getObjectFromTreeIdentifier(id);
                            if (env != null) {
                                treeMatching.add(env.getId());
                            }
                        }
                        treeSearch = true;
                    }
                } catch (StoreIndexException ex) {
                    Throwable cause = ex.getCause();
                    if (cause instanceof IOException) {
                        throw (IOException) cause;
                    } else {
                        throw new IOException(ex);
                    }
                }
            } else {
                LOGGER.log(Level.WARNING, "Not a literal for spatial filter:{0}", sp.getExpression2());
            }
        } else {
            LOGGER.log(Level.WARNING, "not a spatial operator:{0}", filter.getClass().getName());
        }
    } else {
        LOGGER.finer("Null R-tree in spatial search");
    }

    final LeafReader reader = ctx.reader();
    final BitDocIdSet set = new BitDocIdSet(new FixedBitSet(reader.maxDoc()));
    final DocsEnum termDocs = reader.termDocsEnum(META_FIELD);
    int n = termDocs.nextDoc();
    while (n != DocsEnum.NO_MORE_DOCS) {
        final int docId = termDocs.docID();
        final Document doc = reader.document(docId, ID_FIELDS);
        final String id = doc.get(IDENTIFIER_FIELD_NAME);
        final boolean match = treeMatching.contains(id);
        if (treeSearch && reverse && !match) {
            set.bits().set(docId);

        } else if (!treeSearch || match) {
            if (envelopeOnly && !distanceFilter) {
                set.bits().set(docId);
            } else {
                final Document geoDoc = reader.document(docId, GEOMETRY_FIELDS);
                if (filter.evaluate(geoDoc)) {
                    set.bits().set(docId);
                }
            }
        }
        n = termDocs.nextDoc();
    }

    return set;
}

From source file:org.modeshape.jcr.index.lucene.query.ConstantScoreWeightQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    Set<String> fieldSet = Collections.singleton(field);
    // return a weight which uses a constant (1.0f) scorer...
    return new RandomAccessWeight(this) {
        @Override/*from   ww w  .j  a v  a  2s. co  m*/
        protected Bits getMatchingDocs(LeafReaderContext context) throws IOException {
            LeafReader leafReader = context.reader();
            Bits liveDocs = leafReader.getLiveDocs();
            // if liveDocs is null it means there are no deleted documents...
            int docsCount = liveDocs != null ? liveDocs.length() : leafReader.numDocs();
            FixedBitSet result = new FixedBitSet(leafReader.maxDoc());
            for (int i = 0; i < docsCount; i++) {
                if (liveDocs != null && !liveDocs.get(i)) {
                    continue;
                }
                Document document = leafReader.document(i, fieldSet);
                IndexableField[] fields = document.getFields(field);
                if (fields.length == 0) {
                    // the document doesn't have the field...
                    continue;
                }
                if (areValid(fields)) {
                    result.set(i);
                }
            }
            return result.cardinality() > 0 ? result : null;
        }
    };
}