Java Code Examples for org.apache.lucene.search.DoubleValues#doubleValue()

The following examples show how to use org.apache.lucene.search.DoubleValues#doubleValue() . 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: ValueSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public LongValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
  Map<Object, Object> context = new IdentityHashMap<>();
  ScoreAndDoc scorer = new ScoreAndDoc();
  context.put("scorer", scorer);
  final FunctionValues fv = in.getValues(context, ctx);
  return new LongValues() {

    @Override
    public long longValue() throws IOException {
      return fv.longVal(scorer.current);
    }

    @Override
    public boolean advanceExact(int doc) throws IOException {
      scorer.current = doc;
      if (scores != null && scores.advanceExact(doc))
        scorer.score = (float) scores.doubleValue();
      else
        scorer.score = 0;
      return fv.exists(doc);
    }
  };
}
 
Example 2
Source File: FunctionScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
  Scorer in = inner.scorer(context);
  if (in == null)
    return null;
  DoubleValues scores = valueSource.getValues(context, DoubleValuesSource.fromScorer(in));
  return new FilterScorer(in) {
    @Override
    public float score() throws IOException {
      if (scores.advanceExact(docID())) {
        double factor = scores.doubleValue();
        if (factor >= 0) {
          return (float) (factor * boost);
        }
      }
      // default: missing value, negative value or NaN
      return 0;
    }
    @Override
    public float getMaxScore(int upTo) throws IOException {
      return Float.POSITIVE_INFINITY;
    }
  };
}
 
Example 3
Source File: ExpressionValueSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static DoubleValues zeroWhenUnpositioned(DoubleValues in) {
  return new DoubleValues() {

    boolean positioned = false;

    @Override
    public double doubleValue() throws IOException {
      return positioned ? in.doubleValue() : 0;
    }

    @Override
    public boolean advanceExact(int doc) throws IOException {
      return positioned = in.advanceExact(doc);
    }
  };
}
 
Example 4
Source File: TaxonomyFacetSumValueSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void sumValues(List<MatchingDocs> matchingDocs, boolean keepScores, DoubleValuesSource valueSource) throws IOException {

    IntsRef scratch = new IntsRef();
    for(MatchingDocs hits : matchingDocs) {
      OrdinalsReader.OrdinalsSegmentReader ords = ordinalsReader.getReader(hits.context);
      DoubleValues scores = keepScores ? scores(hits) : null;
      DoubleValues functionValues = valueSource.getValues(hits.context, scores);
      DocIdSetIterator docs = hits.bits.iterator();
      
      int doc;
      while ((doc = docs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        ords.get(doc, scratch);
        if (functionValues.advanceExact(doc)) {
          float value = (float) functionValues.doubleValue();
          for (int i = 0; i < scratch.length; i++) {
            values[scratch.ints[i]] += value;
          }
        }
      }
    }

    rollup();
  }
 
Example 5
Source File: PointVectorStrategy.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 {
  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 6
Source File: ValueSource.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
  Map<Object, Object> context = new HashMap<>();
  ScoreAndDoc scorer = new ScoreAndDoc();
  context.put("scorer", scorer);
  context.put("searcher", searcher);
  FunctionValues fv = in.getValues(context, ctx);
  return new DoubleValues() {

    @Override
    public double doubleValue() throws IOException {
      return fv.doubleVal(scorer.current);
    }

    @Override
    public boolean advanceExact(int doc) throws IOException {
      scorer.current = doc;
      if (scores != null && scores.advanceExact(doc)) {
        scorer.score = (float) scores.doubleValue();
      }
      else
        scorer.score = 0;
      // ValueSource will return values even if exists() is false, generally a default
      // of some kind.  To preserve this behaviour with the iterator, we need to always
      // return 'true' here.
      return true;
    }
  };
}
 
Example 7
Source File: FunctionScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
  DoubleValues in = DoubleValues.withDefault(boost.getValues(ctx, scores), 1);
  return new DoubleValues() {
    @Override
    public double doubleValue() throws IOException {
      return scores.doubleValue() * in.doubleValue();
    }

    @Override
    public boolean advanceExact(int doc) throws IOException {
      return in.advanceExact(doc);
    }
  };
}
 
Example 8
Source File: JavascriptCompiler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * This method is unused, it is just here to make sure that the function signatures don't change.
 * If this method fails to compile, you also have to change the byte code generator to correctly
 * use the FunctionValues class.
 */
@SuppressWarnings({"unused", "null"})
private static void unusedTestCompile() throws IOException {
  DoubleValues f = null;
  double ret = f.doubleValue();
}