Java Code Examples for org.apache.lucene.search.spans.SpanWeight

The following examples show how to use org.apache.lucene.search.spans.SpanWeight. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: PayloadScoreQuery.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source Project: lucene-solr   Source File: TestPayloadSpans.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 3
Source Project: lucene-solr   Source File: TestPayloadTermQuery.java    License: Apache License 2.0 6 votes vote down vote up
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));
  }*/

}
 
Example 4
Source Project: mtas   Source File: CodecCollector.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source Project: lucene-solr   Source File: PayloadScoreQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source Project: lucene-solr   Source File: TestPayloadSpans.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source Project: lucene-solr   Source File: TestPayloadSpans.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source Project: lucene-solr   Source File: TestPayloadSpans.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source Project: lucene-solr   Source File: TestPayloadSpans.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 10
Source Project: lucene-solr   Source File: TestPayloadTermQuery.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source Project: mtas   Source File: MtasSpanFollowedByQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 12
Source Project: mtas   Source File: MtasSpanSequenceQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 13
Source Project: mtas   Source File: MtasSpanSequenceQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source Project: mtas   Source File: MtasSpanEndQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source Project: mtas   Source File: MtasSpanIntersectingQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source Project: mtas   Source File: MtasSpanRecurrenceQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 17
Source Project: mtas   Source File: MtasSpanNotQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source Project: mtas   Source File: MtasSpanPrecededByQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source Project: mtas   Source File: MtasSpanFullyAlignedWithQuery.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source Project: mtas   Source File: MtasExpandSpanQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 21
Source Project: mtas   Source File: MtasMaximumExpandSpanQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 22
Source Project: mtas   Source File: MtasSpanUniquePositionQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 23
Source Project: mtas   Source File: MtasExtendedSpanTermQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 24
@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);
}
 
Example 25
Source Project: mtas   Source File: MtasSpanStartQuery.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 26
Source Project: inception   Source File: MtasDocumentIndex.java    License: Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 27
Source Project: inception   Source File: MtasUimaParserLuceneTest.java    License: Apache License 2.0 4 votes vote down vote up
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()) {
                    // }
                }
            }
        }
    }
}
 
Example 28
Source Project: lucene-solr   Source File: TestUnifiedHighlighterMTQ.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return originalQuery.createWeight(searcher, scoreMode, boost);
}
 
Example 29
Source Project: lucene-solr   Source File: SpanPayloadCheckQuery.java    License: Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 30
Source Project: lucene-solr   Source File: SpanPayloadCheckQuery.java    License: Apache License 2.0 4 votes vote down vote up
public SpanPayloadCheckWeight(IndexSearcher searcher, Map<Term, TermStates> termStates, SpanWeight matchWeight, float boost) throws IOException {
  super(SpanPayloadCheckQuery.this, searcher, termStates, boost);
  this.matchWeight = matchWeight;
}