Package com.tigertext.lucene.ext

Source Code of com.tigertext.lucene.ext.RedisValueSource

package com.tigertext.lucene.ext;

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.function.DocValues;
import org.apache.lucene.search.function.ValueSource;

/**
* {@link ValueSource} where values are taken from the execution of an Redis
* function
*
* @author Fernando Benavides <elbrujohalcon@inaka.net>
*/
public class RedisValueSource extends ValueSource {

  private static final long  serialVersionUID  = -8167987813348282785L;
  private RedisFilter      filter;
  private int          nextDocBase;

  /**
   * Default constructor
   *
   * @param filter
   *            Redis filter from which to get scores
   */
  public RedisValueSource(RedisFilter filter) {
    this.filter = filter;
  }

  @Override
  public String description() {
    return "Redis scored by " + this.filter;
  }

  @Override
  public DocValues getValues(IndexReader reader) throws IOException {
    final int docBase = nextDocBase;
    nextDocBase += reader.maxDoc();
    return new DocValues() {

      @Override
      public double doubleVal(int doc) {
        return filter.getScore(doc + docBase);
      }

      @Override
      public String toString(int doc) {
        return description() + ":" + filter.getScore(doc + docBase);
      }

      @Override
      public float floatVal(int doc) {
        return filter.getScore(doc + docBase).floatValue();
      }
    };
  }

  @Override
  public boolean equals(Object o) {
    if (o == this)
      return true;
    if (o == null)
      return false;
    if (o.getClass() != RedisValueSource.class)
      return false;
    RedisValueSource other = (RedisValueSource) o;
    return this.filter.equals(other.filter);
  }

  @Override
  public int hashCode() {
    return this.getClass().hashCode() + this.filter.hashCode();
  }
}
TOP

Related Classes of com.tigertext.lucene.ext.RedisValueSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.