org.apache.lucene.search.ScoreMode Java Examples

The following examples show how to use org.apache.lucene.search.ScoreMode. 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 File: ArrayLengthQuery.java    From crate with 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 #2
Source File: FuzzyCompletionQuery.java    From lucene-solr with 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 #3
Source File: TestSpans.java    From lucene-solr with 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 #4
Source File: RecoverySourcePruneMergePolicy.java    From crate with 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 #5
Source File: DerivedExpressionQuery.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: ScoringMatch.java    From lucene-solr with 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 #7
Source File: TestPayloadSpans.java    From lucene-solr with 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 #8
Source File: TestFieldMaskingSpanQuery.java    From lucene-solr with 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 #9
Source File: TestSelectiveWeightCreation.java    From lucene-solr with 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 File: NumberRangeFacetsTest.java    From lucene-solr with 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 #11
Source File: CompositeVerifyQuery.java    From lucene-solr with 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 #12
Source File: TestFieldMaskingSpanQuery.java    From lucene-solr with 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 #13
Source File: GenericFunctionQueryTest.java    From crate with 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 #14
Source File: AbstractPrefixTreeQuery.java    From lucene-solr with 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 #15
Source File: TestPayloadTermQuery.java    From lucene-solr with 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 #16
Source File: TestFunctionScoreQuery.java    From lucene-solr with 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 #17
Source File: TestNearSpansOrdered.java    From lucene-solr with 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 #18
Source File: TestNearSpansOrdered.java    From lucene-solr with 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 #19
Source File: TestNearSpansOrdered.java    From lucene-solr with 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));
}
 
Example #20
Source File: DocValuesAggregates.java    From crate with 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 #21
Source File: ContextQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final CompletionWeight innerWeight = ((CompletionWeight) innerQuery.createWeight(searcher, scoreMode, boost));
  final Automaton innerAutomaton = innerWeight.getAutomaton();

  // If the inner automaton matches nothing, then we return an empty weight to avoid
  // traversing all contexts during scoring.
  if (innerAutomaton.getNumStates() == 0) {
    return new CompletionWeight(this, innerAutomaton);
  }

  // if separators are preserved the fst contains a SEP_LABEL
  // behind each gap. To have a matching automaton, we need to
  // include the SEP_LABEL in the query as well
  Automaton optionalSepLabel = Operations.optional(Automata.makeChar(ConcatenateGraphFilter.SEP_LABEL));
  Automaton prefixAutomaton = Operations.concatenate(optionalSepLabel, innerAutomaton);
  Automaton contextsAutomaton = Operations.concatenate(toContextAutomaton(contexts, matchAllContexts), prefixAutomaton);
  contextsAutomaton = Operations.determinize(contextsAutomaton, Operations.DEFAULT_MAX_DETERMINIZED_STATES);

  final Map<IntsRef, Float> contextMap = new HashMap<>(contexts.size());
  final TreeSet<Integer> contextLengths = new TreeSet<>();
  for (Map.Entry<IntsRef, ContextMetaData> entry : contexts.entrySet()) {
    ContextMetaData contextMetaData = entry.getValue();
    contextMap.put(entry.getKey(), contextMetaData.boost);
    contextLengths.add(entry.getKey().length);
  }
  int[] contextLengthArray = new int[contextLengths.size()];
  final Iterator<Integer> iterator = contextLengths.descendingIterator();
  for (int i = 0; iterator.hasNext(); i++) {
    contextLengthArray[i] = iterator.next();
  }
  return new ContextCompletionWeight(this, contextsAutomaton, innerWeight, contextMap, contextLengthArray);
}
 
Example #22
Source File: LTRRescorer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(IndexSearcher searcher,
    Explanation firstPassExplanation, int docID) throws IOException {

  final List<LeafReaderContext> leafContexts = searcher.getTopReaderContext()
      .leaves();
  final int n = ReaderUtil.subIndex(docID, leafContexts);
  final LeafReaderContext context = leafContexts.get(n);
  final int deBasedDoc = docID - context.docBase;
  final Weight modelWeight = searcher.createWeight(searcher.rewrite(scoringQuery),
      ScoreMode.COMPLETE, 1);
  return modelWeight.explain(context, deBasedDoc);
}
 
Example #23
Source File: TestRangeFacetCounts.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final Weight in = this.in.createWeight(searcher, scoreMode, boost);
  return new FilterWeight(in) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      used.set(true);
      return in.scorer(context);
    }
  };
}
 
Example #24
Source File: InstrumentedIndexSearcher.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(Query query, ScoreMode scoreMode, float boost) throws IOException {
    QueryProfileBreakdown profile = profiler.getQueryBreakdown(query);
    Timer timer = profile.getTimer(QueryTimingType.CREATE_WEIGHT);
    timer.start();
    final Weight weight;
    try {
        weight = super.createWeight(query, scoreMode, boost);
    } finally {
        timer.stop();
        profiler.pollLastElement();
    }
    return new ProfileWeight(query, weight, profile);
}
 
Example #25
Source File: FunctionRangeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DelegatingCollector getFilterCollector(IndexSearcher searcher) {
  @SuppressWarnings({"rawtypes"})
  Map fcontext = ValueSource.newContext(searcher);
  Weight weight = rangeFilt.createWeight(searcher, ScoreMode.COMPLETE, 1);
  return new FunctionRangeCollector(fcontext, weight);
}
 
Example #26
Source File: SpanTermQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final TermStates context;
  final IndexReaderContext topContext = searcher.getTopReaderContext();
  if (termStates == null || termStates.wasBuiltFor(topContext) == false) {
    context = TermStates.build(topContext, term, scoreMode.needsScores());
  }
  else {
    context = termStates;
  }
  return new SpanTermWeight(context, searcher, scoreMode.needsScores() ? Collections.singletonMap(term, context) : null, boost);
}
 
Example #27
Source File: TestNearSpansOrdered.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testNearSpansNext() throws Exception {
  SpanNearQuery q = makeQuery();
  Spans span = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS);
  assertNext(span,0,0,3);
  assertNext(span,1,0,4);
  assertFinished(span);
}
 
Example #28
Source File: SpanWithinQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  SpanWeight bigWeight = big.createWeight(searcher, scoreMode, boost);
  SpanWeight littleWeight = little.createWeight(searcher, scoreMode, boost);
  return new SpanWithinWeight(searcher, scoreMode.needsScores() ? getTermStates(bigWeight, littleWeight) : null,
                                    bigWeight, littleWeight, boost);
}
 
Example #29
Source File: ShapeQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected Scorer getScorer(final LeafReader reader, final Weight weight, final float boost, final ScoreMode scoreMode) throws IOException {
  switch (query.getQueryRelation()) {
    case INTERSECTS: return getSparseScorer(reader, weight, boost, scoreMode);
    case WITHIN:
    case DISJOINT: return getDenseScorer(reader, weight, boost, scoreMode);
    case CONTAINS: return getContainsDenseScorer(reader, weight, boost, scoreMode);
    default: throw new IllegalArgumentException("Unsupported query type :[" + query.getQueryRelation() + "]");
  }
}
 
Example #30
Source File: GraphTermsQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public final Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(this, boost) {
    Filter filter;

    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      if (filter == null) {
        DocSet set = getDocSet(searcher);
        filter = set.getTopFilter();
      }

      // Although this set only includes live docs, other filters can be pushed down to queries.
      DocIdSet readerSet = filter.getDocIdSet(context, null);
      if (readerSet == null) {
        return null;
      }
      DocIdSetIterator readerSetIterator = readerSet.iterator();
      if (readerSetIterator == null) {
        return null;
      }
      return new ConstantScoreScorer(this, score(), scoreMode, readerSetIterator);
    }

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