SOLR-1932: add sumtotaltermfreq

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1143091 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2011-07-05 15:06:24 +00:00
parent 9748b206f2
commit 06a3778905
4 changed files with 101 additions and 1 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.Fields;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Terms;
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 SumTotalTermFreqValueSource extends ValueSource {
protected String indexedField;
public SumTotalTermFreqValueSource(String indexedField) {
this.indexedField = indexedField;
}
public String name() {
return "sumtotaltermfreq";
}
@Override
public String description() {
return name() + '(' + indexedField + ')';
}
@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 sumTotalTermFreq = 0;
for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
Fields fields = readerContext.reader.fields();
if (fields == null) continue;
Terms terms = fields.terms(indexedField);
if (terms == null) continue;
sumTotalTermFreq += terms.getSumTotalTermFreq();
}
final long ttf = Math.max(-1, sumTotalTermFreq); // we may have added up -1s if not supported
context.put(this, new LongDocValues(this) {
@Override
public long longVal(int doc) {
return ttf;
}
});
}
@Override
public int hashCode() {
return getClass().hashCode() + indexedField.hashCode();
}
@Override
public boolean equals(Object o) {
if (this.getClass() != o.getClass()) return false;
SumTotalTermFreqValueSource other = (SumTotalTermFreqValueSource)o;
return this.indexedField.equals(other.indexedField);
}
}

View File

@ -64,7 +64,7 @@ public class TotalTermFreqValueSource extends ValueSource {
for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
totalTermFreq += readerContext.reader.totalTermFreq(indexedField, indexedBytes);
}
final long ttf = totalTermFreq;
final long ttf = Math.max(-1, totalTermFreq); // we may have added up -1s if not supported
context.put(this, new LongDocValues(this) {
@Override
public long longVal(int doc) {

View File

@ -547,6 +547,16 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {
return new TotalTermFreqValueSource(tinfo.field, tinfo.val, tinfo.indexedField, tinfo.indexedBytes);
}
});
alias("totaltermfreq","ttf");
addParser("sumtotaltermfreq", new ValueSourceParser() {
@Override
public ValueSource parse(FunctionQParser fp) throws ParseException {
String field = fp.parseArg();
return new SumTotalTermFreqValueSource(field);
}
});
alias("sumtotaltermfreq","sttf");
addParser("idf", new ValueSourceParser() {
@Override

View File

@ -415,6 +415,9 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
assertU(adoc("id","6", "a_t","cow cow cow cow cow"));
assertU(commit());
assertQ(req("fl","*,score","q", "{!func}totaltermfreq('a_t','cow')", "fq","id:6"), "//float[@name='score']='7.0'");
assertQ(req("fl","*,score","q", "{!func}ttf(a_t,'cow')", "fq","id:6"), "//float[@name='score']='7.0'");
assertQ(req("fl","*,score","q", "{!func}sumtotaltermfreq('a_t')", "fq","id:6"), "//float[@name='score']='11.0'");
assertQ(req("fl","*,score","q", "{!func}sttf(a_t)", "fq","id:6"), "//float[@name='score']='11.0'");
}
@Test