Example usage for org.apache.lucene.search.spans TermSpans positionsCost

List of usage examples for org.apache.lucene.search.spans TermSpans positionsCost

Introduction

In this page you can find the example usage for org.apache.lucene.search.spans TermSpans positionsCost.

Prototype

float positionsCost

To view the source code for org.apache.lucene.search.spans TermSpans positionsCost.

Click Source Link

Usage

From source file:com.shaie.annots.MultiPositionSpanTermQuery.java

License:Apache License

@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    final IndexReaderContext topContext = searcher.getTopReaderContext();
    final TermContext context;
    if (termContext == null || termContext.wasBuiltFor(topContext) == false) {
        context = TermContext.build(topContext, term);
    } else {//from  w ww . ja v a  2 s.  c o m
        context = termContext;
    }
    final Map<Term, TermContext> terms = needsScores ? Collections.singletonMap(term, context) : null;
    return new SpanTermWeight(context, searcher, terms, boost) {
        @Override
        public Spans getSpans(LeafReaderContext context, Postings requiredPostings) throws IOException {
            final TermSpans spans = (TermSpans) super.getSpans(context,
                    requiredPostings.atLeast(Postings.PAYLOADS));
            if (spans == null) { // term is not present in that reader
                assert context.reader().docFreq(term) == 0 : "no term exists in reader term=" + term;
                return null;
            }
            return new Spans() {

                private final PositionSpansCollector payloadCollector = new PositionSpansCollector();
                private int end = -1;

                @Override
                public int advance(int target) throws IOException {
                    end = -1;
                    return spans.advance(target);
                }

                @Override
                public void collect(SpanCollector collector) throws IOException {
                    spans.collect(collector);
                }

                @Override
                public long cost() {
                    return spans.cost();
                }

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

                @Override
                public int endPosition() {
                    return end;
                }

                @Override
                public int nextDoc() throws IOException {
                    end = -1;
                    return spans.nextDoc();
                }

                @Override
                public int nextStartPosition() throws IOException {
                    final int pos = spans.nextStartPosition();
                    if (pos == NO_MORE_POSITIONS) {
                        end = NO_MORE_POSITIONS;
                        return NO_MORE_POSITIONS;
                    }
                    spans.collect(payloadCollector);
                    end = payloadCollector.payloadValue + pos;
                    return pos;
                }

                @Override
                public float positionsCost() {
                    return spans.positionsCost();
                }

                @Override
                public int startPosition() {
                    return spans.startPosition();
                }

                @Override
                public int width() {
                    return spans.width();
                }
            };
        }
    };
}