diff --git a/lucene/MIGRATE.txt b/lucene/MIGRATE.txt
index c2666e06784..ffbdef459c5 100644
--- a/lucene/MIGRATE.txt
+++ b/lucene/MIGRATE.txt
@@ -364,3 +364,21 @@ LUCENE-1458, LUCENE-2111: Flexible Indexing
for applications that rely on Lucene's internal document ID
assigment. If so, you should instead use LogByteSize/DocMergePolicy
during indexing.
+
+* LUCENE-2883: Lucene's o.a.l.search.function ValueSource based functionality, was consolidated
+ into module/queries along with Solr's similar functionality. The following classes were moved:
+ - o.a.l.search.function.CustomScoreQuery -> o.a.l.queries.CustomScoreQuery
+ - o.a.l.search.function.CustomScoreProvider -> o.a.l.queries.CustomScoreProvider
+ - o.a.l.search.function.NumericIndexDocValueSource -> o.a.l.queries.function.valuesource.NumericIndexDocValueSource
+ The following lists the replacement classes for those removed:
+ - o.a.l.search.function.ByteFieldSource -> o.a.l.queries.function.valuesource.ByteFieldSource
+ - o.a.l.search.function.DocValues -> o.a.l.queries.function.DocValues
+ - o.a.l.search.function.FieldCacheSource -> o.a.l.queries.function.valuesource.FieldCacheSource
+ - o.a.l.search.function.FieldScoreQuery ->o.a.l.queries.function.FunctionQuery
+ - o.a.l.search.function.FloatFieldSource -> o.a.l.queries.function.valuesource.FloatFieldSource
+ - o.a.l.search.function.IntFieldSource -> o.a.l.queries.function.valuesource.IntFieldSource
+ - o.a.l.search.function.OrdFieldSource -> o.a.l.queries.function.valuesource.OrdFieldSource
+ - o.a.l.search.function.ReverseOrdFieldSource -> o.a.l.queries.function.valuesource.ReverseOrdFieldSource
+ - o.a.l.search.function.ShortFieldSource -> o.a.l.queries.function.valuesource.ShortFieldSource
+ - o.a.l.search.function.ValueSource -> o.a.l.queries.function.ValueSource
+ - o.a.l.search.function.ValueSourceQuery -> o.a.l.queries.function.FunctionQuery
diff --git a/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java b/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java
index 62af470b573..999dafa9d48 100644
--- a/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java
+++ b/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java
@@ -35,8 +35,6 @@ import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
-import org.apache.lucene.search.function.FieldScoreQuery;
-import org.apache.lucene.search.function.FieldScoreQuery.Type;
import org.apache.lucene.spatial.DistanceUtils;
import org.apache.lucene.spatial.geohash.GeoHashUtils;
import org.apache.lucene.spatial.geometry.DistanceUnits;
diff --git a/lucene/src/java/org/apache/lucene/search/function/ByteFieldSource.java b/lucene/src/java/org/apache/lucene/search/function/ByteFieldSource.java
deleted file mode 100644
index 2d9775f3124..00000000000
--- a/lucene/src/java/org/apache/lucene/search/function/ByteFieldSource.java
+++ /dev/null
@@ -1,115 +0,0 @@
-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.
- */
-
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.search.FieldCache;
-import org.apache.lucene.search.function.DocValues;
-
-import java.io.IOException;
-
-/**
- * Expert: obtains single byte field values from the
- * {@link org.apache.lucene.search.FieldCache FieldCache}
- * using getBytes()
and makes those values
- * available as other numeric types, casting as needed.
- *
- * @lucene.experimental
- *
- * @see org.apache.lucene.search.function.FieldCacheSource for requirements
- * on the field.
- *
- *
NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ -public class ByteFieldSource extends FieldCacheSource { - private FieldCache.ByteParser parser; - - /** - * Create a cached byte field source with default string-to-byte parser. - */ - public ByteFieldSource(String field) { - this(field, null); - } - - /** - * Create a cached byte field source with a specific string-to-byte parser. - */ - public ByteFieldSource(String field, FieldCache.ByteParser parser) { - super(field); - this.parser = parser; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "byte(" + super.description() + ')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException { - final byte[] arr = cache.getBytes(reader, field, parser); - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */ - @Override - public int intVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + intVal(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return arr; - } - }; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */ - @Override - public boolean cachedFieldSourceEquals(FieldCacheSource o) { - if (o.getClass() != ByteFieldSource.class) { - return false; - } - ByteFieldSource other = (ByteFieldSource)o; - return this.parser==null ? - other.parser==null : - this.parser.getClass() == other.parser.getClass(); - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */ - @Override - public int cachedFieldSourceHashCode() { - return parser==null ? - Byte.class.hashCode() : parser.getClass().hashCode(); - } - -} diff --git a/lucene/src/java/org/apache/lucene/search/function/DocValues.java b/lucene/src/java/org/apache/lucene/search/function/DocValues.java deleted file mode 100755 index 5b7db51aa94..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/DocValues.java +++ /dev/null @@ -1,187 +0,0 @@ -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. - */ - -import org.apache.lucene.search.Explanation; - -/** - * Expert: represents field values as different types. - * Normally created via a - * {@link org.apache.lucene.search.function.ValueSource ValueSuorce} - * for a particular field and reader. - * - * @lucene.experimental - * - * - */ -public abstract class DocValues { - /* - * DocValues is distinct from ValueSource because - * there needs to be an object created at query evaluation time that - * is not referenced by the query itself because: - * - Query objects should be MT safe - * - For caching, Query objects are often used as keys... you don't - * want the Query carrying around big objects - */ - - /** - * Return doc value as a float. - *Mandatory: every DocValues implementation must implement at least this method. - * @param doc document whose float value is requested. - */ - public abstract float floatVal(int doc); - - /** - * Return doc value as an int. - *
Optional: DocValues implementation can (but don't have to) override this method. - * @param doc document whose int value is requested. - */ - public int intVal(int doc) { - return (int) floatVal(doc); - } - - /** - * Return doc value as a long. - *
Optional: DocValues implementation can (but don't have to) override this method. - * @param doc document whose long value is requested. - */ - public long longVal(int doc) { - return (long) floatVal(doc); - } - - /** - * Return doc value as a double. - *
Optional: DocValues implementation can (but don't have to) override this method. - * @param doc document whose double value is requested. - */ - public double doubleVal(int doc) { - return floatVal(doc); - } - - /** - * Return doc value as a string. - *
Optional: DocValues implementation can (but don't have to) override this method. - * @param doc document whose string value is requested. - */ - public String strVal(int doc) { - return Float.toString(floatVal(doc)); - } - - /** - * Return a string representation of a doc value, as required for Explanations. - */ - public abstract String toString(int doc); - - /** - * Explain the scoring value for the input doc. - */ - public Explanation explain(int doc) { - return new Explanation(floatVal(doc), toString(doc)); - } - - /** - * Expert: for test purposes only, return the inner array of values, or null if not applicable. - *
- * Allows tests to verify that loaded values are: - *
Float.NaN
if this
- * DocValues instance does not contain any value.
- * - * This operation is optional - *
- * - * @return the minimum of all values orFloat.NaN
if this
- * DocValues instance does not contain any value.
- */
- public float getMinValue() {
- compute();
- return minVal;
- }
-
- /**
- * Returns the maximum of all values or Float.NaN
if this
- * DocValues instance does not contain any value.
- * - * This operation is optional - *
- * - * @return the maximum of all values orFloat.NaN
if this
- * DocValues instance does not contain any value.
- */
- public float getMaxValue() {
- compute();
- return maxVal;
- }
-
- /**
- * Returns the average of all values or Float.NaN
if this
- * DocValues instance does not contain any value. *
- * - * This operation is optional - *
- * - * @return the average of all values orFloat.NaN
if this
- * DocValues instance does not contain any value
- */
- public float getAverageValue() {
- compute();
- return avgVal;
- }
-
-}
diff --git a/lucene/src/java/org/apache/lucene/search/function/FieldCacheSource.java b/lucene/src/java/org/apache/lucene/search/function/FieldCacheSource.java
deleted file mode 100644
index c079ebddb81..00000000000
--- a/lucene/src/java/org/apache/lucene/search/function/FieldCacheSource.java
+++ /dev/null
@@ -1,109 +0,0 @@
-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.
- */
-
-import java.io.IOException;
-
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexReader.AtomicReaderContext;
-import org.apache.lucene.search.FieldCache;
-
-/**
- * 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 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 - * code would use the value of one of these tokens, but this is not guaranteed). - *
- * Document with no tokens in this field are assigned the Zero
value.
- *
- * @lucene.experimental
- *
- *
NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ -public abstract class FieldCacheSource extends ValueSource { - private String field; - - /** - * Create a cached field source for the input field. - */ - public FieldCacheSource(String field) { - this.field=field; - } - - /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */ - @Override - public final DocValues getValues(AtomicReaderContext context) throws IOException { - return getCachedFieldValues(FieldCache.DEFAULT, field, context.reader); - } - - /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return field; - } - - /** - * Return cached DocValues for input field and reader. - * @param cache FieldCache so that values of a field are loaded once per reader (RAM allowing) - * @param field Field for which values are required. - * @see ValueSource - */ - public abstract DocValues getCachedFieldValues(FieldCache cache, String field, IndexReader reader) throws IOException; - - /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */ - @Override - public final boolean equals(Object o) { - if (!(o instanceof FieldCacheSource)) { - return false; - } - FieldCacheSource other = (FieldCacheSource) o; - return - this.field.equals(other.field) && - cachedFieldSourceEquals(other); - } - - /*(non-Javadoc) @see java.lang.Object#hashCode() */ - @Override - public final int hashCode() { - return - field.hashCode() + - cachedFieldSourceHashCode(); - } - - /** - * Check if equals to another {@link FieldCacheSource}, already knowing that cache and field are equal. - * @see Object#equals(java.lang.Object) - */ - public abstract boolean cachedFieldSourceEquals(FieldCacheSource other); - - /** - * Return a hash code of a {@link FieldCacheSource}, without the hash-codes of the field - * and the cache (those are taken care of elsewhere). - * @see Object#hashCode() - */ - public abstract int cachedFieldSourceHashCode(); -} diff --git a/lucene/src/java/org/apache/lucene/search/function/FieldScoreQuery.java b/lucene/src/java/org/apache/lucene/search/function/FieldScoreQuery.java deleted file mode 100755 index cd71c15b6a9..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/FieldScoreQuery.java +++ /dev/null @@ -1,125 +0,0 @@ -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. - */ - -/** - * A query that scores each document as the value of the numeric input field. - *- * The query matches all documents, and scores each document according to the numeric - * value of that field. - *
- * It is assumed, and expected, that: - *
- * Combining this query in a FunctionQuery allows much freedom in affecting document scores. - * Note, that with this freedom comes responsibility: it is more than likely that the - * default Lucene scoring is superior in quality to scoring modified as explained here. - * However, in some cases, and certainly for research experiments, this capability may turn useful. - *
- * When constructing this query, select the appropriate type. That type should match the data stored in the - * field. So in fact the "right" type should be selected before indexing. Type selection - * has effect on the RAM usage: - *
- * Caching: - * Values for the numeric field are loaded once and cached in memory for further use with the same IndexReader. - * To take advantage of this, it is extremely important to reuse index-readers or index-searchers, - * otherwise, for instance if for each query a new index reader is opened, large penalties would be - * paid for loading the field values into memory over and over again! - * - * @lucene.experimental - */ -public class FieldScoreQuery extends ValueSourceQuery { - - /** - * Type of score field, indicating how field values are interpreted/parsed. - *
- * The type selected at search search time should match the data stored in the field. - * Different types have different RAM requirements: - *
- * 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));
- }
-
- // create the appropriate (cached) field value source.
- private static ValueSource getValueSource(String field, Type type) {
- if (type == Type.BYTE) {
- return new ByteFieldSource(field);
- }
- if (type == Type.SHORT) {
- return new ShortFieldSource(field);
- }
- if (type == Type.INT) {
- return new IntFieldSource(field);
- }
- if (type == Type.FLOAT) {
- return new FloatFieldSource(field);
- }
- throw new IllegalArgumentException(type+" is not a known Field Score Query Type!");
- }
-
-}
diff --git a/lucene/src/java/org/apache/lucene/search/function/FloatFieldSource.java b/lucene/src/java/org/apache/lucene/search/function/FloatFieldSource.java
deleted file mode 100644
index c702ca37ae7..00000000000
--- a/lucene/src/java/org/apache/lucene/search/function/FloatFieldSource.java
+++ /dev/null
@@ -1,109 +0,0 @@
-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.
- */
-
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.search.FieldCache;
-import org.apache.lucene.search.function.DocValues;
-
-import java.io.IOException;
-
-/**
- * Expert: obtains float field values from the
- * {@link org.apache.lucene.search.FieldCache FieldCache}
- * using getFloats()
and makes those values
- * available as other numeric types, casting as needed.
- *
- * @lucene.experimental
- *
- * @see org.apache.lucene.search.function.FieldCacheSource for requirements
- * on the field.
- *
- *
NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ -public class FloatFieldSource extends FieldCacheSource { - private FieldCache.FloatParser parser; - - /** - * Create a cached float field source with default string-to-float parser. - */ - public FloatFieldSource(String field) { - this(field, null); - } - - /** - * Create a cached float field source with a specific string-to-float parser. - */ - public FloatFieldSource(String field, FieldCache.FloatParser parser) { - super(field); - this.parser = parser; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "float(" + super.description() + ')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException { - final float[] arr = cache.getFloats(reader, field, parser); - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return arr; - } - }; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */ - @Override - public boolean cachedFieldSourceEquals(FieldCacheSource o) { - if (o.getClass() != FloatFieldSource.class) { - return false; - } - FloatFieldSource other = (FloatFieldSource)o; - return this.parser==null ? - other.parser==null : - this.parser.getClass() == other.parser.getClass(); - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */ - @Override - public int cachedFieldSourceHashCode() { - return parser==null ? - Float.class.hashCode() : parser.getClass().hashCode(); - } -} \ No newline at end of file diff --git a/lucene/src/java/org/apache/lucene/search/function/IntFieldSource.java b/lucene/src/java/org/apache/lucene/search/function/IntFieldSource.java deleted file mode 100755 index 685eb15f45a..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/IntFieldSource.java +++ /dev/null @@ -1,115 +0,0 @@ -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. - */ - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.function.DocValues; - -import java.io.IOException; - -/** - * Expert: obtains int field values from the - * {@link org.apache.lucene.search.FieldCache FieldCache} - * usinggetInts()
and makes those values
- * available as other numeric types, casting as needed.
- *
- * @lucene.experimental
- *
- * @see org.apache.lucene.search.function.FieldCacheSource for requirements
- * on the field.
- *
- * NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ -public class IntFieldSource extends FieldCacheSource { - private FieldCache.IntParser parser; - - /** - * Create a cached int field source with default string-to-int parser. - */ - public IntFieldSource(String field) { - this(field, null); - } - - /** - * Create a cached int field source with a specific string-to-int parser. - */ - public IntFieldSource(String field, FieldCache.IntParser parser) { - super(field); - this.parser = parser; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "int(" + super.description() + ')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException { - final int[] arr = cache.getInts(reader, field, parser); - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */ - @Override - public int intVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + intVal(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return arr; - } - }; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */ - @Override - public boolean cachedFieldSourceEquals(FieldCacheSource o) { - if (o.getClass() != IntFieldSource.class) { - return false; - } - IntFieldSource other = (IntFieldSource)o; - return this.parser==null ? - other.parser==null : - this.parser.getClass() == other.parser.getClass(); - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */ - @Override - public int cachedFieldSourceHashCode() { - return parser==null ? - Integer.class.hashCode() : parser.getClass().hashCode(); - } - -} diff --git a/lucene/src/java/org/apache/lucene/search/function/MultiValueSource.java b/lucene/src/java/org/apache/lucene/search/function/MultiValueSource.java deleted file mode 100644 index b3ec7681ad1..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/MultiValueSource.java +++ /dev/null @@ -1,137 +0,0 @@ -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. - */ - -import java.io.IOException; - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexReader.AtomicReaderContext; -import org.apache.lucene.index.IndexReader.AtomicReaderContext; -import org.apache.lucene.index.IndexReader.ReaderContext; -import org.apache.lucene.search.Explanation; -import org.apache.lucene.util.ReaderUtil; - -/** This class wraps another ValueSource, but protects - * against accidental double RAM usage in FieldCache when - * a composite reader is passed to {@link #getValues}. - * - *NOTE: this class adds a CPU penalty to every - * lookup, as it must resolve the incoming document to the - * right sub-reader using a binary search.
- * - * @deprecated (4.0) This class is temporary, to ease the - * migration to segment-based searching. Please change your - * code to not pass composite readers to these APIs. */ - -@Deprecated -public final class MultiValueSource extends ValueSource { - - final ValueSource other; - public MultiValueSource(ValueSource other) { - this.other = other; - } - - @Override - public DocValues getValues(AtomicReaderContext context) throws IOException { - // Already an atomic reader -- just delegate - return other.getValues(context); - } - - @Override - public DocValues getValues(ReaderContext context) throws IOException { - if (context.isAtomic) { - return getValues((AtomicReaderContext) context); - } - return new MultiDocValues(ReaderUtil.leaves(context)); - } - - @Override - public String description() { - return other.description(); - } - - @Override - public boolean equals(Object o) { - if (o instanceof MultiValueSource) { - return ((MultiValueSource) o).other.equals(other); - } else { - return false; - } - } - - @Override - public int hashCode() { - return 31 * other.hashCode(); - } - - private final class MultiDocValues extends DocValues { - - final DocValues[] docValues; - final AtomicReaderContext[] leaves; - - MultiDocValues(AtomicReaderContext[] leaves) throws IOException { - this.leaves = leaves; - docValues = new DocValues[leaves.length]; - for(int i=0;i
- * Example:
- *
If there were only three field values: "apple","banana","pear"
- *
then ord("apple")=1, ord("banana")=2, ord("pear")=3
- *
- * WARNING: - * ord() depends on the position in an index and can thus change - * when other documents are inserted or deleted, - * or if a MultiSearcher is used. - * - * @lucene.experimental - * - *
NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ - -public class OrdFieldSource extends ValueSource { - protected String field; - - /** - * Constructor for a certain field. - * @param field field whose values order is used. - */ - public OrdFieldSource(String field) { - this.field = field; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "ord(" + field + ')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getValues(AtomicReaderContext context) throws IOException { - final DocTermsIndex termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader, field); - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return termsIndex.getOrd(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */ - @Override - public String strVal(int doc) { - // the string value of the ordinal, not the string itself - return Integer.toString(termsIndex.getOrd(doc)); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + intVal(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return termsIndex; - } - }; - } - - /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */ - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (o == null) return false; - if (o.getClass() != OrdFieldSource.class) return false; - OrdFieldSource other = (OrdFieldSource)o; - return this.field.equals(other.field); - } - - private static final int hcode = OrdFieldSource.class.hashCode(); - - /*(non-Javadoc) @see java.lang.Object#hashCode() */ - @Override - public int hashCode() { - return hcode + field.hashCode(); - } -} diff --git a/lucene/src/java/org/apache/lucene/search/function/ReverseOrdFieldSource.java b/lucene/src/java/org/apache/lucene/search/function/ReverseOrdFieldSource.java deleted file mode 100644 index bb01ca4a4b5..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/ReverseOrdFieldSource.java +++ /dev/null @@ -1,124 +0,0 @@ -/** - * 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.search.function; - -import org.apache.lucene.index.IndexReader.AtomicReaderContext; -import org.apache.lucene.search.FieldCache; - -import java.io.IOException; - -/** - * Expert: obtains the ordinal of the field value from the default Lucene - * {@link org.apache.lucene.search.FieldCache FieldCache} using getStringIndex() - * and reverses the order. - *- * The native lucene index order is used to assign an ordinal value for each field value. - *
- * Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
- *
- * Example of reverse ordinal (rord):
- *
If there were only three field values: "apple","banana","pear"
- *
then rord("apple")=3, rord("banana")=2, ord("pear")=1
- *
- * WARNING: - * rord() depends on the position in an index and can thus change - * when other documents are inserted or deleted, - * or if a MultiSearcher is used. - * - * @lucene.experimental - * - *
NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ - -public class ReverseOrdFieldSource extends ValueSource { - public String field; - - /** - * Contructor for a certain field. - * @param field field whose values reverse order is used. - */ - public ReverseOrdFieldSource(String field) { - this.field = field; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "rord("+field+')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getValues(AtomicReaderContext context) throws IOException { - final FieldCache.DocTermsIndex termsIndex = FieldCache.DEFAULT.getTermsIndex(context.reader, field); - - final int end = termsIndex.numOrd(); - - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return (end - termsIndex.getOrd(doc)); - } - /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */ - @Override - public int intVal(int doc) { - return end - termsIndex.getOrd(doc); - } - /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */ - @Override - public String strVal(int doc) { - // the string value of the ordinal, not the string itself - return Integer.toString(intVal(doc)); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + strVal(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return termsIndex; - } - }; - } - - /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */ - @Override - public boolean equals(Object o) { - if (o == this) return true; - if (o == null) return false; - if (o.getClass() != ReverseOrdFieldSource.class) return false; - ReverseOrdFieldSource other = (ReverseOrdFieldSource)o; - return this.field.equals(other.field); - } - - private static final int hcode = ReverseOrdFieldSource.class.hashCode(); - - /*(non-Javadoc) @see java.lang.Object#hashCode() */ - @Override - public int hashCode() { - return hcode + field.hashCode(); - } -} diff --git a/lucene/src/java/org/apache/lucene/search/function/ShortFieldSource.java b/lucene/src/java/org/apache/lucene/search/function/ShortFieldSource.java deleted file mode 100644 index 444a9156a01..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/ShortFieldSource.java +++ /dev/null @@ -1,115 +0,0 @@ -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. - */ - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.function.DocValues; - -import java.io.IOException; - -/** - * Expert: obtains short field values from the - * {@link org.apache.lucene.search.FieldCache FieldCache} - * usinggetShorts()
and makes those values
- * available as other numeric types, casting as needed.
- *
- * @lucene.experimental
- *
- * @see org.apache.lucene.search.function.FieldCacheSource for requirements
- * on the field.
- *
- * NOTE: with the switch in 2.9 to segment-based - * searching, if {@link #getValues} is invoked with a - * composite (multi-segment) reader, this can easily cause - * double RAM usage for the values in the FieldCache. It's - * best to switch your application to pass only atomic - * (single segment) readers to this API.
- */ -public class ShortFieldSource extends FieldCacheSource { - private FieldCache.ShortParser parser; - - /** - * Create a cached short field source with default string-to-short parser. - */ - public ShortFieldSource(String field) { - this(field, null); - } - - /** - * Create a cached short field source with a specific string-to-short parser. - */ - public ShortFieldSource(String field, FieldCache.ShortParser parser) { - super(field); - this.parser = parser; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ - @Override - public String description() { - return "short(" + super.description() + ')'; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */ - @Override - public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException { - final short[] arr = cache.getShorts(reader, field, parser); - return new DocValues() { - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ - @Override - public float floatVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */ - @Override - public int intVal(int doc) { - return arr[doc]; - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ - @Override - public String toString(int doc) { - return description() + '=' + intVal(doc); - } - /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ - @Override - Object getInnerArray() { - return arr; - } - }; - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */ - @Override - public boolean cachedFieldSourceEquals(FieldCacheSource o) { - if (o.getClass() != ShortFieldSource.class) { - return false; - } - ShortFieldSource other = (ShortFieldSource)o; - return this.parser==null ? - other.parser==null : - this.parser.getClass() == other.parser.getClass(); - } - - /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */ - @Override - public int cachedFieldSourceHashCode() { - return parser==null ? - Short.class.hashCode() : parser.getClass().hashCode(); - } - -} diff --git a/lucene/src/java/org/apache/lucene/search/function/ValueSource.java b/lucene/src/java/org/apache/lucene/search/function/ValueSource.java deleted file mode 100755 index 58485aeee65..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/ValueSource.java +++ /dev/null @@ -1,86 +0,0 @@ -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. - */ - -import org.apache.lucene.index.IndexReader.AtomicReaderContext; -import org.apache.lucene.index.IndexReader.CompositeReaderContext; -import org.apache.lucene.index.IndexReader.ReaderContext; -import org.apache.lucene.search.function.DocValues; - -import java.io.IOException; - -/** - * Expert: source of values for basic function queries. - *At its default/simplest form, values - one per doc - are used as the score of that doc. - *
Values are instantiated as - * {@link org.apache.lucene.search.function.DocValues DocValues} for a particular reader. - *
ValueSource implementations differ in RAM requirements: it would always be a factor - * of the number of documents, but for each document the number of bytes can be 1, 2, 4, or 8. - * - * @lucene.experimental - * - * - */ -public abstract class ValueSource { - - /** - * Return the DocValues used by the function query. - * @param context the IndexReader used to read these values. - * If any caching is involved, that caching would also be IndexReader based. - * @throws IOException for any error. - */ - public abstract DocValues getValues(AtomicReaderContext context) throws IOException; - - /** - * Return the DocValues used by the function query. - * @deprecated (4.0) This method is temporary, to ease the migration to segment-based - * searching. Please change your code to not pass {@link CompositeReaderContext} to these - * APIs. Use {@link #getValues(IndexReader.AtomicReaderContext)} instead - */ - @Deprecated - public DocValues getValues(ReaderContext context) throws IOException { - return getValues((AtomicReaderContext) context); - } - - - /** - * description of field, used in explain() - */ - public abstract String description(); - - /* (non-Javadoc) @see java.lang.Object#toString() */ - @Override - public String toString() { - return description(); - } - - /** - * Needed for possible caching of query results - used by {@link ValueSourceQuery#equals(Object)}. - * @see Object#equals(Object) - */ - @Override - public abstract boolean equals(Object o); - - /** - * Needed for possible caching of query results - used by {@link ValueSourceQuery#hashCode()}. - * @see Object#hashCode() - */ - @Override - public abstract int hashCode(); - -} diff --git a/lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java b/lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java deleted file mode 100644 index 4f26ee01dbf..00000000000 --- a/lucene/src/java/org/apache/lucene/search/function/ValueSourceQuery.java +++ /dev/null @@ -1,205 +0,0 @@ -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. - */ - -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.*; -import org.apache.lucene.index.IndexReader.AtomicReaderContext; -import org.apache.lucene.index.Term; -import org.apache.lucene.util.ToStringUtils; -import org.apache.lucene.util.Bits; - -import java.io.IOException; -import java.util.Set; - -/** - * Expert: A Query that sets the scores of document to the - * values obtained from a {@link org.apache.lucene.search.function.ValueSource ValueSource}. - *
- * This query provides a score for each and every undeleted document in the index. - *
- * The value source can be based on a (cached) value of an indexed field, but it - * can also be based on an external source, e.g. values read from an external database. - *
- * Score is set as: Score(doc,query) = query.getBoost()2 * valueSource(doc).
- *
- * @lucene.experimental
- */
-public class ValueSourceQuery extends Query {
- ValueSource valSrc;
-
- /**
- * Create a value source query
- * @param valSrc provides the values defines the function to be used for scoring
- */
- public ValueSourceQuery(ValueSource valSrc) {
- this.valSrc=valSrc;
- }
-
- /*(non-Javadoc) @see org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) */
- @Override
- public Query rewrite(IndexReader reader) throws IOException {
- return this;
- }
-
- /*(non-Javadoc) @see org.apache.lucene.search.Query#extractTerms(Set) */
- @Override
- public void extractTerms(Set
- Note: code snippets here should work, but they were never really compiled... so,
- tests sources under TestCustomScoreQuery, TestFieldScoreQuery and TestOrdValues
- may also be useful.
-
- Indexing:
-
- Search:
-
-
- Dividing the original score of each document by a square root of its docid
- (just to demonstrate what it takes to manipulate scores this way)
-
- For more informative debug info on the custom query, also override the name() method:
-
- Taking the square root of the original score and multiplying it by a "short field driven score", ie, the
- short value that was indexed for the scored doc in a certain field:
-
- * Tests here create an index with a few documents, each having
- * an int value indexed field and a float value indexed field.
- * The values of these fields are later used for scoring.
- *
- * The rank tests use Hits to verify that docs are ordered (by score) as expected.
- *
- * The exact score tests use TopDocs top to verify the exact score.
- */
-public class TestFieldScoreQuery extends FunctionTestSetup {
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- createIndex(true);
- }
-
- /** Test that FieldScoreQuery of Type.BYTE returns docs in expected order. */
- @Test
- public void testRankByte () throws Exception {
- // INT field values are small enough to be parsed as byte
- doTestRank(INT_FIELD,FieldScoreQuery.Type.BYTE);
- }
-
- /** Test that FieldScoreQuery of Type.SHORT returns docs in expected order. */
- @Test
- public void testRankShort () throws Exception {
- // INT field values are small enough to be parsed as short
- doTestRank(INT_FIELD,FieldScoreQuery.Type.SHORT);
- }
-
- /** Test that FieldScoreQuery of Type.INT returns docs in expected order. */
- @Test
- public void testRankInt () throws Exception {
- doTestRank(INT_FIELD,FieldScoreQuery.Type.INT);
- }
-
- /** Test that FieldScoreQuery of Type.FLOAT returns docs in expected order. */
- @Test
- public void testRankFloat () throws Exception {
- // INT field can be parsed as float
- doTestRank(INT_FIELD,FieldScoreQuery.Type.FLOAT);
- // same values, but in flot format
- doTestRank(FLOAT_FIELD,FieldScoreQuery.Type.FLOAT);
- }
-
- // Test that FieldScoreQuery returns docs in expected order.
- private void doTestRank (String field, FieldScoreQuery.Type tp) throws Exception {
- IndexSearcher s = new IndexSearcher(dir, true);
- Query q = new FieldScoreQuery(field,tp);
- log("test: "+q);
- QueryUtils.check(random, q,s);
- ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
- assertEquals("All docs should be matched!",N_DOCS,h.length);
- String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test
- for (int i=0; i
+ * The rank tests use Hits to verify that docs are ordered (by score) as expected.
+ *
+ * The exact score tests use TopDocs top to verify the exact score.
+ */
+public class TestFieldScoreQuery extends FunctionTestSetup {
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ createIndex(true);
+ }
+
+ /** Test that FieldScoreQuery of Type.BYTE returns docs in expected order. */
+ @Test
+ public void testRankByte () throws Exception {
+ // INT field values are small enough to be parsed as byte
+ doTestRank(BYTE_VALUESOURCE);
+ }
+
+ /** Test that FieldScoreQuery of Type.SHORT returns docs in expected order. */
+ @Test
+ public void testRankShort () throws Exception {
+ // INT field values are small enough to be parsed as short
+ doTestRank(SHORT_VALUESOURCE);
+ }
+
+ /** Test that FieldScoreQuery of Type.INT returns docs in expected order. */
+ @Test
+ public void testRankInt () throws Exception {
+ doTestRank(INT_VALUESOURCE);
+ }
+
+ /** Test that FieldScoreQuery of Type.FLOAT returns docs in expected order. */
+ @Test
+ public void testRankFloat () throws Exception {
+ // INT field can be parsed as float
+ doTestRank(INT_AS_FLOAT_VALUESOURCE);
+ // same values, but in flot format
+ doTestRank(FLOAT_VALUESOURCE);
+ }
+
+ // Test that FieldScoreQuery returns docs in expected order.
+ private void doTestRank (ValueSource valueSource) throws Exception {
+ FunctionQuery functionQuery = new FunctionQuery(valueSource);
+ IndexSearcher s = new IndexSearcher(dir, true);
+ log("test: "+ functionQuery);
+ QueryUtils.check(random, functionQuery,s);
+ ScoreDoc[] h = s.search(functionQuery, null, 1000).scoreDocs;
+ assertEquals("All docs should be matched!",N_DOCS,h.length);
+ String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test
+ for (int i=0; io
is equal to this. */
- @Override
- public boolean equals(Object o) {
- if (this == o)
- return true;
- if (!super.equals(o))
- return false;
- if (getClass() != o.getClass()) {
- return false;
- }
- ValueSourceQuery other = (ValueSourceQuery)o;
- return this.getBoost() == other.getBoost()
- && this.valSrc.equals(other.valSrc);
- }
-
- /** Returns a hash code value for this object. */
- @Override
- public int hashCode() {
- return (getClass().hashCode() + valSrc.hashCode()) ^ Float.floatToIntBits(getBoost());
- }
-
-}
diff --git a/lucene/src/java/org/apache/lucene/search/function/package.html b/lucene/src/java/org/apache/lucene/search/function/package.html
deleted file mode 100755
index 5da5fe8dbc0..00000000000
--- a/lucene/src/java/org/apache/lucene/search/function/package.html
+++ /dev/null
@@ -1,191 +0,0 @@
-
-
-
- function
package provides tight control over documents scores.
-
-
-
-
-
-
-
-
-
-
-
- f = new Field("score", "7", Field.Store.NO, Field.Index.UN_TOKENIZED);
- f.setOmitNorms(true);
- d1.add(f);
-
-
- Query q = new FieldScoreQuery("score", FieldScoreQuery.Type.BYTE);
-
- Document d1 above would get a score of 7.
-
- Query q = queryParser.parse("my query text");
- CustomScoreQuery customQ = new CustomScoreQuery(q) {
- public float customScore(int doc, float subQueryScore, float valSrcScore) {
- return subQueryScore / Math.sqrt(docid);
- }
- };
-
-
- CustomScoreQuery customQ = new CustomScoreQuery(q) {
- public float customScore(int doc, float subQueryScore, float valSrcScore) {
- return subQueryScore / Math.sqrt(docid);
- }
- public String name() {
- return "1/sqrt(docid)";
- }
- };
-
-
- Query q = queryParser.parse("my query text");
- FieldScoreQuery qf = new FieldScoreQuery("shortScore", FieldScoreQuery.Type.SHORT);
- CustomScoreQuery customQ = new CustomScoreQuery(q,qf) {
- public float customScore(int doc, float subQueryScore, float valSrcScore) {
- return Math.sqrt(subQueryScore) * valSrcScore;
- }
- public String name() {
- return "shortVal*sqrt(score)";
- }
- };
-
-
-