mirror of https://github.com/apache/lucene.git
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:
parent
9748b206f2
commit
06a3778905
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -64,7 +64,7 @@ public class TotalTermFreqValueSource extends ValueSource {
|
||||||
for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
|
for (IndexReader.AtomicReaderContext readerContext : searcher.getTopReaderContext().leaves()) {
|
||||||
totalTermFreq += readerContext.reader.totalTermFreq(indexedField, indexedBytes);
|
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) {
|
context.put(this, new LongDocValues(this) {
|
||||||
@Override
|
@Override
|
||||||
public long longVal(int doc) {
|
public long longVal(int doc) {
|
||||||
|
|
|
@ -547,6 +547,16 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {
|
||||||
return new TotalTermFreqValueSource(tinfo.field, tinfo.val, tinfo.indexedField, tinfo.indexedBytes);
|
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() {
|
addParser("idf", new ValueSourceParser() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -415,6 +415,9 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
|
||||||
assertU(adoc("id","6", "a_t","cow cow cow cow cow"));
|
assertU(adoc("id","6", "a_t","cow cow cow cow cow"));
|
||||||
assertU(commit());
|
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}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
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue