SOLR-1932: add totaltermfreq to relevancy functions

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1142481 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2011-07-03 19:02:57 +00:00
parent b692d0cd6a
commit 3f5c0eca5b
1 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/**
* 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.lucene.queries.function.valuesource;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queries.function.DocValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.docvalues.LongDocValues;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.BytesRef;
import java.io.IOException;
import java.util.Map;
/**
* <code>TotalTermFreqValueSource</code> returns the total term freq (sum of term freqs across all docuyments).
* @lucene.internal
*/
public class TotalTermFreqValueSource extends ValueSource {
protected String field;
protected String indexedField;
protected String val;
protected BytesRef indexedBytes;
public TotalTermFreqValueSource(String field, String val, String indexedField, BytesRef indexedBytes) {
this.field = field;
this.val = val;
this.indexedField = indexedField;
this.indexedBytes = indexedBytes;
}
public String name() {
return "totaltermfreq";
}
@Override
public String description() {
return name() + '(' + field + ',' + val + ')';
}
@Override
public DocValues getValues(Map context, IndexReader.AtomicReaderContext readerContext) throws IOException {
return (DocValues)context.get(this);
}
@Override
public void createWeight(Map context, IndexSearcher searcher) throws IOException {
long totalTermFreq = 0;
for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
totalTermFreq += readerContext.reader.totalTermFreq(indexedField, indexedBytes);
}
final long ttf = totalTermFreq;
context.put(this, new LongDocValues(this) {
@Override
public long longVal(int doc) {
return ttf;
}
});
}
@Override
public int hashCode() {
return getClass().hashCode() + indexedField.hashCode()*29 + indexedBytes.hashCode();
}
@Override
public boolean equals(Object o) {
if (this.getClass() != o.getClass()) return false;
TotalTermFreqValueSource other = (TotalTermFreqValueSource)o;
return this.indexedField.equals(other.indexedField) && this.indexedBytes.equals(other.indexedBytes);
}
}