Index: src/java/org/apache/lucene/search/function/ValueSourceQuery.java =================================================================== --- src/java/org/apache/lucene/search/function/ValueSourceQuery.java (revision 568253) +++ src/java/org/apache/lucene/search/function/ValueSourceQuery.java (working copy) @@ -42,14 +42,34 @@ */ public class ValueSourceQuery extends Query { ValueSource valSrc; + DocValuesStats stats = null; + boolean computeStats = false; /** + * Create a value source query and do not compute statistics + * @param valSrc provides the values defines the function to be used for scoring + */ + public ValueSourceQuery(ValueSource valSrc) { + this(valSrc, false); + } + + /** * Create a value source query * @param valSrc provides the values defines the function to be used for scoring + * @param computeStats determine whether or not to compute statistics for this query */ - public ValueSourceQuery(ValueSource valSrc) { + public ValueSourceQuery(ValueSource valSrc, boolean computeStats) { this.valSrc=valSrc; + this.computeStats = computeStats; } + + /** Get DocValuesStats returned by this ValueSourceQuery. + * + * @return statistics for query or null if either the query has not been executed yet or statistics were not enabled. + */ + public DocValuesStats getStats() { + return stats; + } /*(non-Javadoc) @see org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) */ public Query rewrite(IndexReader reader) throws IOException { @@ -126,6 +146,9 @@ this.maxDoc = reader.maxDoc(); // this is when/where the values are first created. vals = valSrc.getValues(reader); + if (computeStats) { + stats = vals.getStats(); + } } /*(non-Javadoc) @see org.apache.lucene.search.Scorer#next() */ @@ -197,5 +220,26 @@ public int hashCode() { return (getClass().hashCode() + valSrc.hashCode()) ^ Float.floatToIntBits(getBoost()); } + + /** Get the ValueSource associated with this Query. + * + * @return the ValueSource + */ + public ValueSource getValueSource() { + return this.valSrc; + } + + /** Return whether or not statistics were computed for this query. + */ + public boolean isComputeStats() { + return computeStats; + } + /** Set whether or not statistics should be computed for this query. + * This has an effect only if called before the search is executed. + */ + public void setComputeStats(boolean computeStats) { + this.computeStats = computeStats; + } + } Index: src/java/org/apache/lucene/search/function/DocValuesStats.java =================================================================== --- src/java/org/apache/lucene/search/function/DocValuesStats.java (revision 0) +++ src/java/org/apache/lucene/search/function/DocValuesStats.java (revision 0) @@ -0,0 +1,80 @@ +package org.apache.lucene.search.function; + +/** + * 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. + */ + +/** Holds statistics for a set of DocValues. + */ +public class DocValuesStats { + + // --- some simple statistics on values + private float minVal; + private float maxVal; + private float avgVal; + private float sum; + private int nVals; + + public DocValuesStats(DocValues vals) { + nVals = vals.getNumberOfValues(); + minVal = Float.MAX_VALUE; + maxVal = 0; + sum = 0; + for (int i = 0; i < nVals; i++) { + float val = vals.floatVal(i); + sum += val; + minVal = Math.min(minVal, val); + maxVal = Math.max(maxVal, val); + } + avgVal = sum / nVals; + } + + /** + * The minimum of all values. + */ + public float getMin() { + return minVal; + } + + /** + * The maximum of all values. + */ + public float getMax() { + return maxVal; + } + + /** + * The average of all values. + */ + public float getAvg() { + return avgVal; + } + + /** + * The sum of all values. + */ + public float getSum() { + return sum; + } + + /** + * The number of values. + */ + public int getCount() { + return nVals; + } + +} Property changes on: src/java/org/apache/lucene/search/function/DocValuesStats.java ___________________________________________________________________ Name: svn:mime-type + text/plain Index: src/java/org/apache/lucene/search/function/DocValues.java =================================================================== --- src/java/org/apache/lucene/search/function/DocValues.java (revision 568253) +++ src/java/org/apache/lucene/search/function/DocValues.java (working copy) @@ -126,34 +126,15 @@ } // --- some simple statistics on values - private float minVal; - private float maxVal; - private float avgVal; - private boolean computed=false; - // compute optional values - private void compute () { - if (computed) { - return; - } - minVal = Float.MAX_VALUE; - maxVal = 0; - float sum = 0; - for (int i=0; itype param tells how to parse the field string values into a numeric score value. * @param field the numeric field to be used. * @param type the type of the field: either + * {@link Type#BYTE}, {@link Type#SHORT}, {@link Type#INT}, or {@link Type#FLOAT}. + * @param computeStats whether or not to compute statistics or the query, default is false + */ + public FieldScoreQuery(String field, Type type, boolean computeStats) { + super(getValueSource(field,type), computeStats); + } + + /** + * Create a FieldScoreQuery - a query that scores each document as the value of the numeric input field. + *

+ * The type param tells how to parse the field string values into a numeric score value. + * @param field the numeric field to be used. + * @param type the type of the field: either * {@link Type#BYTE}, {@link Type#SHORT}, {@link Type#INT}, or {@link Type#FLOAT}. */ public FieldScoreQuery(String field, Type type) { - super(getValueSource(field,type)); + this(field,type, false); } // create the appropriate (cached) field value source. Index: src/java/org/apache/lucene/search/function/FieldCacheSource.java =================================================================== --- src/java/org/apache/lucene/search/function/FieldCacheSource.java (revision 568253) +++ src/java/org/apache/lucene/search/function/FieldCacheSource.java (working copy) @@ -26,7 +26,7 @@ * Expert: A base class for ValueSource implementations that retrieve values for * a single field from the {@link org.apache.lucene.search.FieldCache FieldCache}. *

- * Fields used herein nust be indexed (doesn't matter if these fields are stored or not). + * Fields used herein must be indexed (doesn't matter if these fields are stored or not). *

* It is assumed that each such indexed field is untokenized, or at least has a single token in a document. * For documents with multiple tokens of the same field, behavior is undefined (It is likely that current @@ -89,6 +89,11 @@ field.hashCode() + cachedFieldSourceHashCode(); } + + /** Get the field used for values. */ + public String getField() { + return field; + } /** * Check if equals to another {@link FieldCacheSource}, already knowing that cache and field are equal.