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

下面列出了怎么用org.apache.lucene.search.spans.SpanWeight的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: lucene-solr   文件: PayloadScoreQuery.java
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
  PayloadSpanScorer scorer = (PayloadSpanScorer)scorer(context);
  if (scorer == null || scorer.iterator().advance(doc) != doc)
    return Explanation.noMatch("No match");

  scorer.score();  // force freq calculation
  Explanation payloadExpl = scorer.getPayloadExplanation();

  if (includeSpanScore) {
    SpanWeight innerWeight = ((PayloadSpanWeight) scorer.getWeight()).innerWeight;
    Explanation innerExpl = innerWeight.explain(context, doc);
    return Explanation.match(scorer.scoreCurrentDoc(), "PayloadSpanQuery, product of:", innerExpl, payloadExpl);
  }

  return scorer.getPayloadExplanation();
}
 
源代码2 项目: lucene-solr   文件: TestPayloadSpans.java
public void testSpanNot() throws Exception {
  SpanQuery[] clauses = new SpanQuery[2];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));
  SpanQuery spq = new SpanNearQuery(clauses, 5, true);
  SpanNotQuery snq = new SpanNotQuery(spq, new SpanTermQuery(new Term(PayloadHelper.FIELD, "two")));



  Directory directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
                                                   newIndexWriterConfig(new PayloadAnalyzer()).setSimilarity(similarity));

  Document doc = new Document();
  doc.add(newTextField(PayloadHelper.FIELD, "one two three one four three", Field.Store.YES));
  writer.addDocument(doc);
  IndexReader reader = getOnlyLeafReader(writer.getReader());
  writer.close();

  checkSpans(snq.createWeight(newSearcher(reader, false), ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(reader.leaves().get(0), SpanWeight.Postings.PAYLOADS), 1, new int[]{2});
  reader.close();
  directory.close();
}
 
源代码3 项目: lucene-solr   文件: TestPayloadTermQuery.java
public void test() throws IOException {
  SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term("field", "seventy")),
          new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  TopDocs hits = searcher.search(query, 100);
  assertTrue("hits is null and it shouldn't be", hits != null);
  assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100);

  //they should all have the exact same score, because they all contain seventy once, and we set
  //all the other similarity factors to be 1

  for (int i = 0; i < hits.scoreDocs.length; i++) {
    ScoreDoc doc = hits.scoreDocs[i];
    assertTrue(doc.score + " does not equal: " + 1, doc.score == 1);
  }
  CheckHits.checkExplanations(query, PayloadHelper.FIELD, searcher, true);
  Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  /*float score = hits.score(0);
  for (int i =1; i < hits.length(); i++)
  {
    assertTrue("scores are not equal and they should be", score == hits.score(i));
  }*/

}
 
源代码4 项目: mtas   文件: CodecCollector.java
/**
 * Collect spans for occurences.
 *
 * @param occurences
 *          the occurences
 * @param prefixes
 *          the prefixes
 * @param field
 *          the field
 * @param searcher
 *          the searcher
 * @param lrc
 *          the lrc
 * @return the map
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
private static Map<GroupHit, Spans> collectSpansForOccurences(
    Set<GroupHit> occurences, Set<String> prefixes, String field,
    IndexSearcher searcher, LeafReaderContext lrc) throws IOException {
  Map<GroupHit, Spans> list = new HashMap<>();
  IndexReader reader = searcher.getIndexReader();
  final float boost = 0;
  for (GroupHit hit : occurences) {
    MtasSpanQuery queryHit = createQueryFromGroupHit(prefixes, field, hit);
    if (queryHit != null) {
      MtasSpanQuery queryHitRewritten = queryHit.rewrite(reader);
      SpanWeight weight = queryHitRewritten.createWeight(searcher, false,
          boost);
      Spans spans = weight.getSpans(lrc, SpanWeight.Postings.POSITIONS);
      if (spans != null) {
        list.put(hit, spans);
      }
    }
  }
  return list;
}
 
源代码5 项目: lucene-solr   文件: PayloadScoreQuery.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  SpanWeight innerWeight = wrappedQuery.createWeight(searcher, scoreMode, boost);
  if (!scoreMode.needsScores())
    return innerWeight;
  return new PayloadSpanWeight(searcher, innerWeight, boost);
}
 
源代码6 项目: lucene-solr   文件: TestPayloadSpans.java
public void testFirstClauseWithoutPayload() throws Exception {
  Spans spans;
  IndexSearcher searcher = getSearcher();

  SpanQuery[] clauses = new SpanQuery[3];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nopayload"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "qq"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ss"));

  SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 6, true);
  
  SpanQuery[] clauses2 = new SpanQuery[2];
   
  clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "pp"));
  clauses2[1] = spanNearQuery;

  SpanNearQuery snq = new SpanNearQuery(clauses2, 6, false);
  
  SpanQuery[] clauses3 = new SpanQuery[2];
   
  clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "np"));
  clauses3[1] = snq;

  SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);
  spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);

  assertTrue("spans is null and it shouldn't be", spans != null);
  checkSpans(spans, 1, new int[]{3});
  closeIndexReader.close();
  directory.close();
}
 
源代码7 项目: lucene-solr   文件: TestPayloadSpans.java
public void testHeavilyNestedSpanQuery() throws Exception {
  Spans spans;
  IndexSearcher searcher = getSearcher();

  SpanQuery[] clauses = new SpanQuery[3];
  clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "two"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "three"));

  SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, 5, true);
 
  clauses = new SpanQuery[3];
  clauses[0] = spanNearQuery; 
  clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "five"));
  clauses[2] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "six"));

  SpanNearQuery spanNearQuery2 = new SpanNearQuery(clauses, 6, true);
   
  SpanQuery[] clauses2 = new SpanQuery[2];
  clauses2[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "eleven"));
  clauses2[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "ten"));
  SpanNearQuery spanNearQuery3 = new SpanNearQuery(clauses2, 2, false);
  
  SpanQuery[] clauses3 = new SpanQuery[3];
  clauses3[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "nine"));
  clauses3[1] = spanNearQuery2;
  clauses3[2] = spanNearQuery3;
   
  SpanNearQuery nestedSpanNearQuery = new SpanNearQuery(clauses3, 6, false);

  spans = nestedSpanNearQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  checkSpans(spans, 2, new int[]{8, 8});
  closeIndexReader.close();
  directory.close();
}
 
源代码8 项目: lucene-solr   文件: TestPayloadSpans.java
public void testShrinkToAfterShortestMatch() throws IOException {
  Directory directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
                                                   newIndexWriterConfig(new TestPayloadAnalyzer()));

  Document doc = new Document();
  doc.add(new TextField("content", new StringReader("a b c d e f g h i j a k")));
  writer.addDocument(doc);

  IndexReader reader = writer.getReader();
  IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false);
  writer.close();

  SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
  SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
  SpanQuery[] sqs = { stq1, stq2 };
  SpanNearQuery snq = new SpanNearQuery(sqs, 1, true);
  VerifyingCollector collector = new VerifyingCollector();
  Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);

  TopDocs topDocs = is.search(snq, 1);
  Set<String> payloadSet = new HashSet<>();
  for (int i = 0; i < topDocs.scoreDocs.length; i++) {
    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
      while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
        collector.reset();
        spans.collect(collector);
        for (final BytesRef payload : collector.payloads) {
          payloadSet.add(Term.toString(payload));
        }
      }
    }
  }
  assertEquals(2, payloadSet.size());
  assertTrue(payloadSet.contains("a:Noise:10"));
  assertTrue(payloadSet.contains("k:Noise:11"));
  reader.close();
  directory.close();
}
 
源代码9 项目: lucene-solr   文件: TestPayloadSpans.java
public void testShrinkToAfterShortestMatch2() throws IOException {
  Directory directory = newDirectory();
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory,
                                                   newIndexWriterConfig(new TestPayloadAnalyzer()));

  Document doc = new Document();
  doc.add(new TextField("content", new StringReader("a b a d k f a h i k a k")));
  writer.addDocument(doc);
  IndexReader reader = writer.getReader();
  IndexSearcher is = newSearcher(getOnlyLeafReader(reader), false);
  writer.close();

  SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a"));
  SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k"));
  SpanQuery[] sqs = { stq1, stq2 };
  SpanNearQuery snq = new SpanNearQuery(sqs, 0, true);
  VerifyingCollector collector = new VerifyingCollector();
  Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);

  TopDocs topDocs = is.search(snq, 1);
  Set<String> payloadSet = new HashSet<>();
  for (int i = 0; i < topDocs.scoreDocs.length; i++) {
    while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
      while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
        collector.reset();
        spans.collect(collector);
        for (final BytesRef payload: collector.payloads) {
          payloadSet.add(Term.toString(payload));
        }
      }
    }
  }
  assertEquals(2, payloadSet.size());
  assertTrue(payloadSet.contains("a:Noise:10"));
  assertTrue(payloadSet.contains("k:Noise:11"));
  reader.close();
  directory.close();
}
 
源代码10 项目: lucene-solr   文件: TestPayloadTermQuery.java
public void testMultipleMatchesPerDoc() throws Exception {
  SpanQuery query = new PayloadScoreQuery(new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")),
          new MaxPayloadFunction(), PayloadDecoder.FLOAT_DECODER);
  TopDocs hits = searcher.search(query, 100);
  assertTrue("hits is null and it shouldn't be", hits != null);
  assertTrue("hits Size: " + hits.totalHits.value + " is not: " + 100, hits.totalHits.value == 100);

  //they should all have the exact same score, because they all contain seventy once, and we set
  //all the other similarity factors to be 1

  //System.out.println("Hash: " + seventyHash + " Twice Hash: " + 2*seventyHash);
  //there should be exactly 10 items that score a 4, all the rest should score a 2
  //The 10 items are: 70 + i*100 where i in [0-9]
  int numTens = 0;
  for (int i = 0; i < hits.scoreDocs.length; i++) {
    ScoreDoc doc = hits.scoreDocs[i];
    if (doc.doc % 10 == 0) {
      numTens++;
      assertTrue(doc.score + " does not equal: " + 4.0, doc.score == 4.0);
    } else {
      assertTrue(doc.score + " does not equal: " + 2, doc.score == 2);
    }
  }
  assertTrue(numTens + " does not equal: " + 10, numTens == 10);
  CheckHits.checkExplanations(query, "field", searcher, true);
  Spans spans = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertTrue("spans is null and it shouldn't be", spans != null);
  //should be two matches per document
  int count = 0;
  //100 hits times 2 matches per hit, we should have 200 in count
  while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
    while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
      count++;
    }
  }
  assertTrue(count + " does not equal: " + 200, count == 200);
}
 
源代码11 项目: mtas   文件: MtasSpanFollowedByQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanFollowedByQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanFollowedByQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码12 项目: mtas   文件: MtasSpanSequenceQuery.java
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  List<MtasSpanSequenceQueryWeight> subWeights = new ArrayList<>();
  SpanWeight ignoreWeight = null;
  for (MtasSpanSequenceItem item : items) {
    subWeights.add(new MtasSpanSequenceQueryWeight(
        item.getQuery().createWeight(searcher, false, boost), item.isOptional()));
  }
  if (ignoreQuery != null) {
    ignoreWeight = ignoreQuery.createWeight(searcher, false, boost);
  }
  return new SpanSequenceWeight(subWeights, ignoreWeight, maximumIgnoreLength,
      searcher, needsScores ? getTermContexts(subWeights) : null, boost);
}
 
源代码13 项目: mtas   文件: MtasSpanSequenceQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanSequenceQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanSequenceQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码14 项目: mtas   文件: MtasSpanEndQuery.java
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause))
      .createWeight(searcher, needsScores, boost);
  return new SpanTermWeight(spanWeight, searcher, boost);
}
 
源代码15 项目: mtas   文件: MtasSpanIntersectingQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanIntersectingQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanIntersectingQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码16 项目: mtas   文件: MtasSpanRecurrenceQuery.java
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight subWeight = query.createWeight(searcher, false, boost);
  SpanWeight ignoreWeight = null;
  if (ignoreQuery != null) {
    ignoreWeight = ignoreQuery.createWeight(searcher, false, boost);
  }
  return new SpanRecurrenceWeight(subWeight, ignoreWeight,
      maximumIgnoreLength, searcher,
      needsScores ? getTermContexts(subWeight) : null, boost);
}
 
源代码17 项目: mtas   文件: MtasSpanNotQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanNotQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanNotQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码18 项目: mtas   文件: MtasSpanPrecededByQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanPrecededByQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanPrecededByQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码19 项目: mtas   文件: MtasSpanFullyAlignedWithQuery.java
/**
 * Gets the term contexts.
 *
 * @param items the items
 * @return the term contexts
 */
protected Map<Term, TermContext> getTermContexts(
    List<MtasSpanFullyAlignedWithQueryWeight> items) {
  List<SpanWeight> weights = new ArrayList<>();
  for (MtasSpanFullyAlignedWithQueryWeight item : items) {
    weights.add(item.spanWeight);
  }
  return getTermContexts(weights);
}
 
源代码20 项目: mtas   文件: MtasExpandSpanQuery.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost)
    throws IOException {
  SpanWeight subWeight = query.createWeight(searcher, needsScores, boost);
  if (maximumLeft == 0 && maximumRight == 0) {
    return subWeight;
  } else {
    return new MtasExpandWeight(subWeight, searcher, needsScores, boost);
  }
}
 
源代码21 项目: mtas   文件: MtasMaximumExpandSpanQuery.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost)
    throws IOException {
  SpanWeight subWeight = query.createWeight(searcher, needsScores, boost);
  if (maximumLeft == 0 && maximumRight == 0) {
    return subWeight;
  } else {
    return new MtasMaximumExpandWeight(subWeight, searcher, needsScores, boost);
  }
}
 
源代码22 项目: mtas   文件: MtasSpanUniquePositionQuery.java
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight subWeight = clause.createWeight(searcher, false, boost);
  return new SpanUniquePositionWeight(subWeight, searcher,
      needsScores ? getTermContexts(subWeight) : null, boost);
}
 
源代码23 项目: mtas   文件: MtasExtendedSpanTermQuery.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, float boost)
    throws IOException {
  final TermContext context;
  final IndexReaderContext topContext = searcher.getTopReaderContext();
  if (termContext == null) {
    context = TermContext.build(topContext, localTerm);
  } else {
    context = termContext;
  }
  return new SpanTermWeight(context, searcher,
      needsScores ? Collections.singletonMap(localTerm, context) : null, boost);
}
 
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight subWeight = subQuery.createWeight(searcher, needsScores, boost);
  return new MtasDisabledTwoPhaseIteratorWeight(subWeight, searcher,
      needsScores, boost);
}
 
源代码25 项目: mtas   文件: MtasSpanStartQuery.java
@Override
public MtasSpanWeight createWeight(IndexSearcher searcher,
    boolean needsScores, float boost) throws IOException {
  SpanWeight spanWeight = ((SpanQuery) searcher.rewrite(clause))
      .createWeight(searcher, needsScores, boost);
  return new SpanTermWeight(spanWeight, searcher, boost);
}
 
源代码26 项目: inception   文件: MtasDocumentIndex.java
private long doCountResults(IndexSearcher searcher,
    SearchQueryRequest aRequest, MtasSpanQuery q) throws IOException
{
    ListIterator<LeafReaderContext> leafReaderContextIterator = searcher.getIndexReader()
            .leaves().listIterator();

    Map<Long, Long> annotatableDocuments = listAnnotatableDocuments(aRequest.getProject(),
        aRequest.getUser());

    final float boost = 0;
    SpanWeight spanweight = q.rewrite(searcher.getIndexReader()).createWeight(searcher, false,
            boost);

    long numResults = 0;

    while (leafReaderContextIterator.hasNext()) {
        LeafReaderContext leafReaderContext = leafReaderContextIterator.next();
        try {
            Spans spans = spanweight.getSpans(leafReaderContext, SpanWeight.Postings.POSITIONS);
            SegmentReader segmentReader = (SegmentReader) leafReaderContext.reader();
            if (spans != null) {
                while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
                    if (segmentReader.numDocs() == segmentReader.maxDoc()
                            || segmentReader.getLiveDocs().get(spans.docID())) {
                        Document document = segmentReader.document(spans.docID());

                        // Retrieve user
                        String user = document.get(FIELD_USER);

                        // Retrieve source and annotation document ids
                        String rawSourceDocumentId = document.get(FIELD_SOURCE_DOCUMENT_ID);
                        String rawAnnotationDocumentId = document
                                .get(FIELD_ANNOTATION_DOCUMENT_ID);
                        if (rawSourceDocumentId == null || rawAnnotationDocumentId == null) {
                            log.trace("Indexed document lacks source/annotation document IDs"
                                    + " - source: {}, annotation: {}", rawSourceDocumentId,
                                rawAnnotationDocumentId);
                            continue;

                        }
                        long sourceDocumentId = Long.valueOf(rawSourceDocumentId);
                        long annotationDocumentId = Long.valueOf(rawAnnotationDocumentId);

                        // If the query is limited to a given document, skip any results
                        // which are not in the given document
                        Optional<SourceDocument> limitedToDocument = aRequest
                                .getLimitedToDocument();
                        if (limitedToDocument.isPresent() && !Objects
                            .equals(limitedToDocument.get().getId(), sourceDocumentId)) {
                            log.trace("Query limited to document {}, skipping results for "
                                    + "document {}", limitedToDocument.get().getId(),
                                sourceDocumentId);
                            continue;
                        }

                        if (annotatableDocuments.containsKey(sourceDocumentId)
                            && annotationDocumentId == -1) {
                            // Exclude result if the retrieved document is a sourcedocument
                            // (that is, has annotationDocument = -1) AND it has a
                            // corresponding annotation document for this user
                            log.trace("Skipping results from indexed source document {} in" 
                                + "favor of results from the corresponding annotation "
                                + "document", sourceDocumentId);
                            continue;
                        }
                        else if (annotationDocumentId != -1 && !aRequest.getUser().getUsername()
                            .equals(user)) {
                            // Exclude result if the retrieved document is an annotation
                            // document (that is, annotationDocument != -1 and its username
                            // is different from the quering user
                            log.trace("Skipping results from annotation document for user {} "
                                    + "which does not match the requested user {}", user,
                                aRequest.getUser().getUsername());
                            continue;
                        }

                        while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                            numResults++;
                        }
                    }
                }
            }
        }
        catch (Exception e) {
            log.error("Unable to process query results", e);
            numResults = -1;
        }
    }
    return numResults;
}
 
源代码27 项目: inception   文件: MtasUimaParserLuceneTest.java
private static void doQuery(IndexReader indexReader, String field, MtasSpanQuery q,
        List<String> prefixes)
    throws IOException
{
    ListIterator<LeafReaderContext> iterator = indexReader.leaves().listIterator();
    IndexSearcher searcher = new IndexSearcher(indexReader);
    final float boost = 0;
    SpanWeight spanweight = q.rewrite(indexReader).createWeight(searcher, false, boost);

    while (iterator.hasNext()) {
        System.out.println("#### new iteration ####");
        LeafReaderContext lrc = iterator.next();
        Spans spans = spanweight.getSpans(lrc, SpanWeight.Postings.POSITIONS);
        SegmentReader segmentReader = (SegmentReader) lrc.reader();
        Terms terms = segmentReader.terms(field);
        CodecInfo mtasCodecInfo = CodecInfo.getCodecInfoFromTerms(terms);
        if (spans != null) {
            while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
                if (segmentReader.numDocs() == segmentReader.maxDoc()
                        || segmentReader.getLiveDocs().get(spans.docID())) {
                    String idValue = segmentReader.document(spans.docID()).getField(FIELD_ID)
                            .stringValue();
                    System.out.println("********  New doc " + spans.docID() + "-" + idValue);
                    while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
                        System.out.println("------");
                        List<MtasTokenString> tokens = mtasCodecInfo
                                .getPrefixFilteredObjectsByPositions(field, spans.docID(),
                                        prefixes, spans.startPosition(),
                                        (spans.endPosition() - 1));
                        for (MtasTokenString token : tokens) {
                            System.out.print("docId: " + (lrc.docBase + spans.docID()) + ", ");
                            System.out.print(" position: " + token.getPositionStart()
                                    + (!Objects.equals(token.getPositionEnd(),
                                            token.getPositionStart())
                                                    ? "-" + token.getPositionEnd()
                                                    : ""));
                            System.out.print(" offset: " + token.getOffsetStart() + "-"
                                    + token.getOffsetEnd());
                            System.out.print(" mtasId: " + token.getId());
                            System.out.println(" " + token.getPrefix()
                                    + (token.getPostfix() != null ? ":" + token.getPostfix()
                                            : "")
                                    + ", ");
                        }
                        System.out.println("------");
                        List<MtasTreeHit<String>> hits = mtasCodecInfo
                                .getPositionedTermsByPrefixesAndPositionRange(field,
                                        spans.docID(), prefixes, spans.startPosition(),
                                        (spans.endPosition() - 1));
                        for (MtasTreeHit<String> hit : hits) {
                            System.out.print("docId: " + (lrc.docBase + spans.docID()) + ", ");
                            System.out.print("position: " + hit.startPosition
                                    + (hit.endPosition != hit.startPosition
                                            ? "-" + hit.endPosition
                                            : ""));
                            System.out.println(" " + CodecUtil.termPrefix(hit.data)
                                    + (CodecUtil.termValue(hit.data) != null
                                            ? ":" + CodecUtil.termValue(hit.data)
                                            : "")
                                    + ", ");
                        }
                    }
                    // if (prefixes != null && !prefixes.isEmpty()) {
                    // }
                }
            }
        }
    }
}
 
源代码28 项目: lucene-solr   文件: TestUnifiedHighlighterMTQ.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return originalQuery.createWeight(searcher, scoreMode, boost);
}
 
源代码29 项目: lucene-solr   文件: SpanPayloadCheckQuery.java
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  SpanWeight matchWeight = match.createWeight(searcher, scoreMode, boost);
  return new SpanPayloadCheckWeight(searcher, scoreMode.needsScores() ? getTermStates(matchWeight) : null, matchWeight, boost);
}
 
源代码30 项目: lucene-solr   文件: SpanPayloadCheckQuery.java
public SpanPayloadCheckWeight(IndexSearcher searcher, Map<Term, TermStates> termStates, SpanWeight matchWeight, float boost) throws IOException {
  super(SpanPayloadCheckQuery.this, searcher, termStates, boost);
  this.matchWeight = matchWeight;
}
 
 类所在包
 类方法
 同包方法