Java 类org.apache.lucene.search.spans.SpanWeight 实例源码

项目:lucene-addons    文件:SQPTestBase.java   
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);
  float boost = getBoost(q);
  SpanWeight sw = sq.createWeight(searcher, false, boost);

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

  long i = 0;
  if (spans != null) {
    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
      while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
        i++;
      }
    }
  }
  return i;
}
项目:lucene-addons    文件:SQPTestBase.java   
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, false, getBoost(q));

  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());
      }
    }
  }
  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;
}
项目:lucene-addons    文件:TestSpanOnlyQueryParser.java   
private void testOffsetForSingleSpanMatch(SpanOnlyParser p, String s,
    int trueDocID, int trueSpanStart, int trueSpanEnd) throws Exception {
  SpanQuery q = (SpanQuery)p.parse(s);
  List<LeafReaderContext> ctxs = reader.leaves();
  assert (ctxs.size() == 1);
  LeafReaderContext ctx = ctxs.get(0);
  q = (SpanQuery) q.rewrite(ctx.reader());
  SpanWeight sw = q.createWeight(searcher, false, getBoost(q));

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

  int i = 0;
  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);
}