/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.search.function;
import java.io.IOException;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache.DocTerms;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.SolrException;
/**
* Use a field value and find the Document Frequency within another field.
*
* @since solr 4.0
*/
public class JoinDocFreqValueSource extends FieldCacheSource {
public static final String NAME = "joindf";
protected final String qfield;
public JoinDocFreqValueSource(String field, String qfield) {
super(field);
this.qfield = qfield;
}
public String description() {
return NAME + "(" + field +":("+qfield+"))";
}
public DocValues getValues(Map context, final IndexReader reader) throws IOException
{
final DocTerms terms = cache.getTerms(reader, field, true );
return new DocValues() {
public int intVal(int doc)
{
try {
BytesRef ref = new BytesRef();
terms.getTerm(doc, ref);
int v = reader.docFreq( qfield, ref );
//System.out.println( NAME+"["+ref.utf8ToString()+"="+v+"]" );
return v;
}
catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "caught exception in function "+description()+" : doc="+doc, e);
}
}
public float floatVal(int doc) {
return (float)intVal(doc);
}
public long longVal(int doc) {
return (long)intVal(doc);
}
public double doubleVal(int doc) {
return (double)intVal(doc);
}
public String strVal(int doc) {
return intVal(doc) + "";
}
public String toString(int doc) {
return description() + '=' + intVal(doc);
}
};
}
public boolean equals(Object o) {
if (o.getClass() != JoinDocFreqValueSource.class) return false;
JoinDocFreqValueSource other = (JoinDocFreqValueSource)o;
if( !qfield.equals( other.qfield ) ) return false;
return super.equals(other);
}
public int hashCode() {
return qfield.hashCode() + super.hashCode();
};
}