Example usage for org.apache.lucene.search.spans SpanQuery createWeight

List of usage examples for org.apache.lucene.search.spans SpanQuery createWeight

Introduction

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

Prototype

@Override
    public abstract SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
            throws IOException;

Source Link

Usage

From source file:org.tallison.lucene.queryparser.spans.SQPTestBase.java

License:Apache License

long countSpans(String field, Query q) throws Exception {
    List<LeafReaderContext> ctxs = reader.leaves();

    assert (ctxs.size() == 1);
    LeafReaderContext leafReaderContext = ctxs.get(0);
    SpanQuery sq = convert(field, q);
    sq = (SpanQuery) sq.rewrite(reader);
    SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);

    final Spans spans = sw.getSpans(leafReaderContext, SpanWeight.Postings.POSITIONS);

    long i = 0;/*  ww w  . j av  a2s .  c o  m*/
    if (spans != null) {
        while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
            while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                i++;
            }
        }
    }
    return i;
}

From source file:org.tallison.lucene.queryparser.spans.SQPTestBase.java

License:Apache License

long countDocs(String field, Query q) throws Exception {
    BitSet docs = new BitSet();
    List<LeafReaderContext> ctxs = reader.leaves();
    assert (ctxs.size() == 1);
    LeafReaderContext leafReaderContext = ctxs.get(0);
    SpanQuery sq = convert(field, q);
    sq = (SpanQuery) sq.rewrite(reader);
    SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);

    final Spans spans = sw.getSpans(leafReaderContext, SpanWeight.Postings.POSITIONS);
    if (spans != null) {
        while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
            while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                docs.set(spans.docID());
            }/*from w ww.j a  v a 2s .c o  m*/
        }
    }
    long spanDocHits = docs.cardinality();
    // double check with a regular searcher and original query
    TotalHitCountCollector coll = new TotalHitCountCollector();
    searcher.search(q, coll);
    assertEquals(coll.getTotalHits(), spanDocHits);
    return spanDocHits;
}

From source file:org.tallison.lucene.queryparser.spans.TestSpanOnlyQueryParser.java

License:Apache License

private void testOffsetForSingleSpanMatch(SpanOnlyParser p, String s, int trueDocID, int trueSpanStart,
        int trueSpanEnd) throws Exception {
    SpanQuery sq = (SpanQuery) p.parse(s);
    List<LeafReaderContext> ctxs = reader.leaves();
    assert (ctxs.size() == 1);
    LeafReaderContext ctx = ctxs.get(0);
    sq = (SpanQuery) sq.rewrite(ctx.reader());
    SpanWeight sw = sq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);

    final Spans spans = sw.getSpans(ctx, SpanWeight.Postings.POSITIONS);

    int i = 0;/*from  ww  w  . j  ava  2  s. com*/
    int spanStart = -1;
    int spanEnd = -1;
    int docID = -1;

    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
        while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
            spanStart = spans.startPosition();
            spanEnd = spans.endPosition();
            docID = spans.docID();
            i++;
        }
    }
    assertEquals("should only be one matching span", 1, i);
    assertEquals("doc id", trueDocID, docID);
    assertEquals("span start", trueSpanStart, spanStart);
    assertEquals("span end", trueSpanEnd, spanEnd);
}

From source file:org.tallison.lucene.search.concordance.charoffsets.SpansCrawler.java

License:Apache License

public static void crawl(SpanQuery query, Query filter, IndexSearcher searcher, DocTokenOffsetsVisitor visitor)
        throws IOException, TargetTokenNotFoundException {

    query = (SpanQuery) query.rewrite(searcher.getIndexReader());

    SpanWeight w = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    if (filter == null) {
        for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {

            Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS);
            if (spans == null) {
                continue;
            }//from   w  w  w .java 2 s.  c  om
            boolean cont = visitLeafReader(ctx, spans, visitor);
            if (!cont) {
                break;
            }
        }
    } else {
        filter = searcher.rewrite(filter);
        Weight searcherWeight = searcher.createWeight(filter, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
            Scorer leafReaderContextScorer = searcherWeight.scorer(ctx);
            if (leafReaderContextScorer == null) {
                continue;
            }
            //Can we tell from the scorer that there were no hits?
            //in <= 5.x we could stop here if the filter query had no hits.

            Spans spans = w.getSpans(ctx, SpanWeight.Postings.POSITIONS);
            if (spans == null) {
                continue;
            }
            DocIdSetIterator filterItr = leafReaderContextScorer.iterator();

            if (filterItr == null || filterItr.equals(DocIdSetIterator.empty())) {
                continue;
            }
            boolean cont = visitLeafReader(ctx, spans, filterItr, visitor);
            if (!cont) {
                break;
            }
        }
    }
}