Java Code Examples for org.apache.lucene.search.ScoreMode

The following examples show how to use org.apache.lucene.search.ScoreMode. 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: AbstractPrefixTreeQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      DocIdSet docSet = getDocIdSet(context);
      if (docSet == null) {
        return null;
      }
      DocIdSetIterator disi = docSet.iterator();
      if (disi == null) {
        return null;
      }
      return new ConstantScoreScorer(this, score(), scoreMode, disi);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return true;
    }
  };
}
 
Example 2
Source Project: lucene-solr   Source File: CompositeVerifyQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final Weight indexQueryWeight = indexQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, boost);//scores aren't unsupported

  return new ConstantScoreWeight(this, boost) {

    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {

      final Scorer indexQueryScorer = indexQueryWeight.scorer(context);
      if (indexQueryScorer == null) {
        return null;
      }

      final TwoPhaseIterator predFuncValues = predicateValueSource.iterator(context, indexQueryScorer.iterator());
      return new ConstantScoreScorer(this, score(), scoreMode, predFuncValues);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return predicateValueSource.isCacheable(ctx);
    }

  };
}
 
Example 3
Source Project: lucene-solr   Source File: NumberRangeFacetsTest.java    License: Apache License 2.0 6 votes vote down vote up
private Bits searchForDocBits(Query query) throws IOException {
  FixedBitSet bitSet = new FixedBitSet(indexSearcher.getIndexReader().maxDoc());
  indexSearcher.search(query,
      new SimpleCollector() {
        int leafDocBase;
        @Override
        public void collect(int doc) throws IOException {
          bitSet.set(leafDocBase + doc);
        }

        @Override
        protected void doSetNextReader(LeafReaderContext context) throws IOException {
          leafDocBase = context.docBase;
        }

        @Override
        public ScoreMode scoreMode() {
          return ScoreMode.COMPLETE_NO_SCORES;
        }
      });
  return bitSet;
}
 
Example 4
Source Project: crate   Source File: ArrayLengthQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {
        @Override
        public boolean isCacheable(LeafReaderContext ctx) {
            return false;
        }

        @Override
        public Scorer scorer(LeafReaderContext context) {
            return new ConstantScoreScorer(
                this,
                0f,
                scoreMode,
                new NumTermsPerDocTwoPhaseIterator(context.reader(), numTermsPerDocFactory.apply(context), matches));
        }
    };
}
 
Example 5
Source Project: lucene-solr   Source File: TestSpans.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpanNearUnOrdered() throws Exception {
  //See http://www.gossamer-threads.com/lists/lucene/java-dev/52270 for discussion about this test
  SpanQuery senq = spanNearUnorderedQuery(field, 0, "u1", "u2");
  Spans spans = senq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(spans, 4, 1, 3);
  assertNext(spans, 5, 2, 4);
  assertNext(spans, 8, 2, 4);
  assertNext(spans, 9, 0, 2);
  assertNext(spans, 10, 0, 2);
  assertFinished(spans);

  senq = spanNearUnorderedQuery(1, senq, spanTermQuery(field, "u2")); 
  spans = senq.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(spans, 4, 0, 3);
  assertNext(spans, 4, 1, 3); // unordered spans can be subsets
  assertNext(spans, 5, 0, 4);
  assertNext(spans, 5, 2, 4);
  assertNext(spans, 8, 0, 4);
  assertNext(spans, 8, 2, 4);
  assertNext(spans, 9, 0, 2);
  assertNext(spans, 9, 0, 4);
  assertNext(spans, 10, 0, 2);
  assertFinished(spans);
}
 
Example 6
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 7
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 8
Source Project: crate   Source File: GenericFunctionQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_generic_function_query_cannot_be_cached_with_un_deterministic_functions_present() throws Exception {
    QueryTester.Builder builder = new QueryTester.Builder(
        createTempDir(),
        THREAD_POOL,
        clusterService,
        Version.CURRENT,
        "create table t (x int)"
    );
    builder.indexValues("x", 1, 2, 3);
    try (QueryTester tester = builder.build()) {
        var query = tester.toQuery("x = random()");
        var searcher = tester.searcher();
        var weight = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        assertThat(weight.isCacheable(searcher.getTopReaderContext().leaves().get(0)), is(false));
    }
}
 
Example 9
Source Project: lucene-solr   Source File: TestSelectiveWeightCreation.java    License: Apache License 2.0 6 votes vote down vote up
private LTRScoringQuery.ModelWeight performQuery(TopDocs hits,
    IndexSearcher searcher, int docid, LTRScoringQuery model) throws IOException,
    ModelException {
  final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()
      .leaves();
  final int n = ReaderUtil.subIndex(hits.scoreDocs[0].doc, leafContexts);
  final LeafReaderContext context = leafContexts.get(n);
  final int deBasedDoc = hits.scoreDocs[0].doc - context.docBase;

  final Weight weight = searcher.createWeight(searcher.rewrite(model), ScoreMode.COMPLETE, 1);
  final Scorer scorer = weight.scorer(context);

  // rerank using the field final-score
  scorer.iterator().advance(deBasedDoc);
  scorer.score();
  assertTrue(weight instanceof LTRScoringQuery.ModelWeight);
  final LTRScoringQuery.ModelWeight modelWeight = (LTRScoringQuery.ModelWeight) weight;
  return modelWeight;

}
 
Example 10
Source Project: lucene-solr   Source File: TestFieldMaskingSpanQuery.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpans2() throws Exception {
  assumeTrue("Broken scoring: LUCENE-3723",
      searcher.getSimilarity() instanceof TFIDFSimilarity);
  SpanQuery qA1 = new SpanTermQuery(new Term("gender", "female"));
  SpanQuery qA2 = new SpanTermQuery(new Term("first",  "james"));
  SpanQuery qA  = new SpanOrQuery(qA1, new FieldMaskingSpanQuery(qA2, "gender"));
  SpanQuery qB  = new SpanTermQuery(new Term("last",   "jones"));
  SpanQuery q   = new SpanNearQuery(new SpanQuery[]
    { new FieldMaskingSpanQuery(qA, "id"),
      new FieldMaskingSpanQuery(qB, "id") }, -1, false );
  check(q, new int[] { 0, 1, 2, 3 });

  Spans span = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(span, 0,0,1);
  assertNext(span, 1,1,2);
  assertNext(span, 2,0,1);
  assertNext(span, 2,2,3);
  assertNext(span, 3,0,1);
  assertFinished(span);
}
 
Example 11
Source Project: lucene-solr   Source File: FuzzyCompletionQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final Automaton originalAutomata;
  try (CompletionTokenStream stream = (CompletionTokenStream) analyzer.tokenStream(getField(), getTerm().text()) ) {
    originalAutomata = stream.toAutomaton(unicodeAware);
  }
  Set<IntsRef> refs = new HashSet<>();
  Automaton automaton = toLevenshteinAutomata(originalAutomata, refs);
  if (unicodeAware) {
    Automaton utf8automaton = new UTF32ToUTF8().convert(automaton);
    utf8automaton = Operations.determinize(utf8automaton, maxDeterminizedStates);
    automaton = utf8automaton;
  }
  // TODO Accumulating all refs is bad, because the resulting set may be very big.
  // TODO Better iterate over automaton again inside FuzzyCompletionWeight?
  return new FuzzyCompletionWeight(this, automaton, refs);
}
 
Example 12
Source Project: crate   Source File: RecoverySourcePruneMergePolicy.java    License: Apache License 2.0 6 votes vote down vote up
static CodecReader wrapReader(String recoverySourceField, CodecReader reader, Supplier<Query> retainSourceQuerySupplier)
    throws IOException {
    NumericDocValues recoverySource = reader.getNumericDocValues(recoverySourceField);
    if (recoverySource == null || recoverySource.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) {
        return reader; // early terminate - nothing to do here since non of the docs has a recovery source anymore.
    }
    IndexSearcher s = new IndexSearcher(reader);
    s.setQueryCache(null);
    Weight weight = s.createWeight(s.rewrite(retainSourceQuerySupplier.get()), ScoreMode.COMPLETE_NO_SCORES, 1.0f);
    Scorer scorer = weight.scorer(reader.getContext());
    if (scorer != null) {
        BitSet recoverySourceToKeep = BitSet.of(scorer.iterator(), reader.maxDoc());
        // calculating the cardinality is significantly cheaper than skipping all bulk-merging we might do
        // if retentions are high we keep most of it
        if (recoverySourceToKeep.cardinality() == reader.maxDoc()) {
            return reader; // keep all source
        }
        return new SourcePruningFilterCodecReader(recoverySourceField, reader, recoverySourceToKeep);
    } else {
        return new SourcePruningFilterCodecReader(recoverySourceField, reader, null);
    }
}
 
Example 13
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
    if (!scoreMode.needsScores()) {
        // If scores are not needed simply return a constant score on all docs
        return new ConstantScoreWeight(this.query, boost) {
            @Override
            public boolean isCacheable(LeafReaderContext ctx) {
                return true;
            }

            @Override
            public Scorer scorer(LeafReaderContext context) throws IOException {
                return new ConstantScoreScorer(this, score(),
                    scoreMode, DocIdSetIterator.all(context.reader().maxDoc()));
            }
        };
    }

    return new FVWeight(this);
}
 
Example 14
Source Project: lucene-solr   Source File: ScoringMatch.java    License: Apache License 2.0 6 votes vote down vote up
public static final MatcherFactory<ScoringMatch> matchWithSimilarity(Similarity similarity) {
  return searcher -> {
    searcher.setSimilarity(similarity);
    return new CollectingMatcher<ScoringMatch>(searcher, ScoreMode.COMPLETE) {
      @Override
      protected ScoringMatch doMatch(String queryId, int doc, Scorable scorer) throws IOException {
        float score = scorer.score();
        if (score > 0)
          return new ScoringMatch(queryId, score);
        return null;
      }

      @Override
      public ScoringMatch resolve(ScoringMatch match1, ScoringMatch match2) {
        return new ScoringMatch(match1.getQueryId(), match1.getScore() + match2.getScore());
      }
    };
  };
}
 
Example 15
Source Project: lucene-solr   Source File: TestFieldMaskingSpanQuery.java    License: Apache License 2.0 6 votes vote down vote up
public void testSpans1() throws Exception {
  SpanQuery q1 = new SpanTermQuery(new Term("first", "sally"));
  SpanQuery q2 = new SpanTermQuery(new Term("first", "james"));
  SpanQuery qA = new SpanOrQuery(q1, q2);
  SpanQuery qB = new FieldMaskingSpanQuery(qA, "id");
                                          
  check(qA, new int[] { 0, 1, 2, 4 });
  check(qB, new int[] { 0, 1, 2, 4 });

  Spans spanA = qA.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  Spans spanB = qB.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  
  while (spanA.nextDoc() != Spans.NO_MORE_DOCS) {
    assertNotSame("spanB not still going", Spans.NO_MORE_DOCS, spanB.nextDoc());
    while (spanA.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
      assertEquals("spanB start position", spanA.startPosition(), spanB.nextStartPosition());
      assertEquals("spanB end position", spanA.endPosition(), spanB.endPosition());
    }
    assertEquals("spanB start position", Spans.NO_MORE_POSITIONS, spanB.nextStartPosition());
  }
  assertEquals("spanB end doc", Spans.NO_MORE_DOCS, spanB.nextDoc());
}
 
Example 16
Source Project: crate   Source File: DocValuesAggregates.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private static Iterable<Row> getRow(AtomicReference<Throwable> killed,
                                    Searcher searcher,
                                    Query query,
                                    List<DocValueAggregator> aggregators) throws IOException {
    IndexSearcher indexSearcher = searcher.searcher();
    Weight weight = indexSearcher.createWeight(indexSearcher.rewrite(query), ScoreMode.COMPLETE_NO_SCORES, 1f);
    List<LeafReaderContext> leaves = indexSearcher.getTopReaderContext().leaves();
    Object[] cells = new Object[aggregators.size()];
    for (int i = 0; i < aggregators.size(); i++) {
        cells[i] = aggregators.get(i).initialState();
    }
    for (var leaf : leaves) {
        Scorer scorer = weight.scorer(leaf);
        if (scorer == null) {
            continue;
        }
        for (int i = 0; i < aggregators.size(); i++) {
            aggregators.get(i).loadDocValues(leaf.reader());
        }
        DocIdSetIterator docs = scorer.iterator();
        Bits liveDocs = leaf.reader().getLiveDocs();
        for (int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = docs.nextDoc()) {
            if (liveDocs != null && !liveDocs.get(doc)) {
                continue;
            }
            Throwable killCause = killed.get();
            if (killCause != null) {
                Exceptions.rethrowUnchecked(killCause);
            }
            for (int i = 0; i < aggregators.size(); i++) {
                aggregators.get(i).apply(cells[i], doc);
            }
        }
    }
    for (int i = 0; i < aggregators.size(); i++) {
        cells[i] = aggregators.get(i).partialResult(cells[i]);
    }
    return List.of(new RowN(cells));
}
 
Example 17
Source Project: lucene-solr   Source File: PointVectorStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  Weight w = inner.createWeight(searcher, scoreMode, 1f);
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      Scorer in = w.scorer(context);
      if (in == null)
        return null;
      DoubleValues v = distanceSource.getValues(context, DoubleValuesSource.fromScorer(in));
      DocIdSetIterator approximation = in.iterator();
      TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
          return v.advanceExact(approximation.docID()) && v.doubleValue() <= limit;
        }

        @Override
        public float matchCost() {
          return 100;   // distance calculation can be heavy!
        }
      };
      return new ConstantScoreScorer(this, score(), scoreMode, twoPhase);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return distanceSource.isCacheable(ctx);
    }

  };
}
 
Example 18
Source Project: querqy   Source File: FieldBoostTermQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(final IndexSearcher searcher, final ScoreMode scoreMode, final float boost)
        throws IOException {
    final IndexReaderContext context = searcher.getTopReaderContext();
    final TermStates termState = TermStates.build(context, term, scoreMode.needsScores());
    // TODO: set boosts to 1f if needsScores is false?
    return new FieldBoostWeight(termState, boost, fieldBoost.getBoost(term.field(), searcher.getIndexReader()));
}
 
Example 19
Source Project: lucene-solr   Source File: TestNearSpansOrdered.java    License: Apache License 2.0 5 votes vote down vote up
public void testOrderedSpanIterationSameTerms1() throws Exception {
  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
      new SpanTermQuery(new Term(FIELD, "t1")), new SpanTermQuery(new Term(FIELD, "t2"))
  }, 1, true);
  Spans spans = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(spans,4,0,2);
  assertFinished(spans);
}
 
Example 20
Source Project: lucene-solr   Source File: FunctionMatchQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  DoubleValuesSource vs = source.rewrite(searcher);
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      DoubleValues values = vs.getValues(context, null);
      DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
      TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
          return values.advanceExact(approximation.docID()) && filter.test(values.doubleValue());
        }

        @Override
        public float matchCost() {
          return 100; // TODO maybe DoubleValuesSource should have a matchCost?
        }
      };
      return new ConstantScoreScorer(this, score(), scoreMode, twoPhase);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return source.isCacheable(ctx);
    }

  };
}
 
Example 21
Source Project: lucene-solr   Source File: FunctionScoreQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  ScoreMode sm;
  if (scoreMode.needsScores() && source.needsScores()) {
    sm = ScoreMode.COMPLETE;
  } else {
    sm = ScoreMode.COMPLETE_NO_SCORES;
  }
  Weight inner = in.createWeight(searcher, sm, 1f);
  if (scoreMode.needsScores() == false)
    return inner;
  return new FunctionScoreWeight(this, inner, source.rewrite(searcher), boost);
}
 
Example 22
Source Project: lucene-solr   Source File: SolrIndexSearcherTest.java    License: Apache License 2.0 5 votes vote down vote up
public MockPostFilter(int maxDocsToCollect, int cost, ScoreMode scoreMode) {
  super(new Term("foo", "bar"));//The term won't really be used. just the collector
  assert cost > 100;
  this.cost = cost;
  this.maxDocsToCollect = maxDocsToCollect;
  this.scoreMode = scoreMode;
}
 
Example 23
Source Project: lucene-solr   Source File: TestFunctionScoreQuery.java    License: Apache License 2.0 5 votes vote down vote up
private void assertInnerScoreMode(ScoreMode expectedScoreMode, ScoreMode inputScoreMode, DoubleValuesSource valueSource) throws IOException {
  final AtomicReference<ScoreMode> scoreModeInWeight = new AtomicReference<ScoreMode>();
  Query innerQ = new TermQuery(new Term(TEXT_FIELD, "a")) {
    
    @Override
    public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
      scoreModeInWeight.set(scoreMode);
      return super.createWeight(searcher, scoreMode, boost);
    }
  };
  
  FunctionScoreQuery fq = new FunctionScoreQuery(innerQ, valueSource);
  fq.createWeight(searcher, inputScoreMode, 1f);
  assertEquals(expectedScoreMode, scoreModeInWeight.get());
}
 
Example 24
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
        throws IOException {
    IndexReaderContext context = searcher.getTopReaderContext();
    assert scoreMode.needsScores() : "Should not be used in filtering mode";
    return new PostingsExplorerWeight(this, this.term, TermStates.build(context, this.term,
            scoreMode.needsScores()),
            this.type);
}
 
Example 25
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 26
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 27
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 28
Source Project: lucene-solr   Source File: TestNearSpansOrdered.java    License: Apache License 2.0 5 votes vote down vote up
public void testOrderedSpanIterationSameTerms2() throws Exception {
  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
      new SpanTermQuery(new Term(FIELD, "t2")), new SpanTermQuery(new Term(FIELD, "t1"))
  }, 1, true);
  Spans spans = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(spans,4,1,4);
  assertNext(spans,4,2,4);
  assertFinished(spans);
}
 
Example 29
Source Project: lucene-solr   Source File: TestNearSpansOrdered.java    License: Apache License 2.0 5 votes vote down vote up
public void testOrderedSpanIteration() throws Exception {
  SpanNearQuery q = new SpanNearQuery(new SpanQuery[]{
      new SpanOrQuery(new SpanTermQuery(new Term(FIELD, "w1")), new SpanTermQuery(new Term(FIELD, "w2"))),
      new SpanTermQuery(new Term(FIELD, "w4"))
  }, 10, true);
  Spans spans = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(spans,0,0,4);
  assertNext(spans,0,1,4);
  assertFinished(spans);
}
 
Example 30
Source Project: lucene-solr   Source File: TestNearSpansOrdered.java    License: Apache License 2.0 5 votes vote down vote up
public void testNearSpansAdvanceTo0() throws Exception {
  SpanNearQuery q = makeQuery();
  Spans span = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertEquals(0, span.advance(0));
  assertEquals(0, span.nextStartPosition());
  assertEquals(s(0,0,3), s(span));
}