Skip to content

Commit

Permalink
Fixing tests for number field
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
  • Loading branch information
harshavamsi committed Nov 15, 2023
1 parent 57ec29e commit e43d203
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ public String typeName() {

@Override
public Query termQuery(Object value, QueryShardContext context) {
failIfNotIndexed();
failIfNotIndexedAndNoDocValues();
long scaledValue = Math.round(scale(value));
Query query = NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue);
Query query = NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, hasDocValues());
if (boost() != 1f) {
query = new BoostQuery(query, boost());
}
Expand All @@ -210,7 +210,7 @@ public Query termQuery(Object value, QueryShardContext context) {

@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
failIfNotIndexed();
failIfNotIndexedAndNoDocValues();
List<Long> scaledValues = new ArrayList<>(values.size());
for (Object value : values) {
long scaledValue = Math.round(scale(value));
Expand All @@ -225,7 +225,7 @@ public Query termsQuery(List<?> values, QueryShardContext context) {

@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
failIfNotIndexed();
failIfNotIndexedAndNoDocValues();
Long lo = null;
if (lowerTerm != null) {
double dValue = scale(lowerTerm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
Expand All @@ -63,7 +64,9 @@ public void testTermQuery() {
);
double value = (randomDouble() * 2 - 1) * 10000;
long scaledValue = Math.round(value * ft.getScalingFactor());
assertEquals(LongPoint.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, null));
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery("scaled_float", scaledValue);
Query query = new IndexOrDocValuesQuery(LongPoint.newExactQuery("scaled_float", scaledValue), dvQuery);
assertEquals(query, ft.termQuery(value, null));
}

public void testTermsQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,8 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
public Query termQuery(String field, Object value, Boolean hasDocValues) {
float v = parse(value, false);
Query query = HalfFloatPoint.newExactQuery(field, v);
if(hasDocValues){
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(
field, HalfFloatPoint.halfFloatToSortableShort(v)
);
if (hasDocValues) {
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field, HalfFloatPoint.halfFloatToSortableShort(v));
query = new IndexOrDocValuesQuery(query, dvQuery);
}
return query;
Expand Down Expand Up @@ -318,10 +316,9 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
@Override
public Query termQuery(String field, Object value, Boolean hasDocValues) {
float v = parse(value, false);
Query query = FloatPoint.newExactQuery(field, v);
if(hasDocValues){
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field,
NumericUtils.floatToSortableInt(v));
Query query = FloatPoint.newExactQuery(field, v);
if (hasDocValues) {
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.floatToSortableInt(v));
query = new IndexOrDocValuesQuery(query, dvQuery);
}
return query;
Expand Down Expand Up @@ -422,9 +419,8 @@ public Double parse(XContentParser parser, boolean coerce) throws IOException {
public Query termQuery(String field, Object value, Boolean hasDocValues) {
double v = parse(value, false);
Query query = DoublePoint.newExactQuery(field, v);
if(hasDocValues){
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field,
NumericUtils.doubleToSortableLong(v));
if (hasDocValues) {
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.doubleToSortableLong(v));
query = new IndexOrDocValuesQuery(query, dvQuery);
}
return query;
Expand Down Expand Up @@ -663,7 +659,7 @@ public Query termQuery(String field, Object value, Boolean hasDocValues) {
}
int v = parse(value, true);
Query query = IntPoint.newExactQuery(field, v);
if(hasDocValues){
if (hasDocValues) {
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field, v);
query = new IndexOrDocValuesQuery(query, dvQuery);
}
Expand Down Expand Up @@ -782,7 +778,7 @@ public Query termQuery(String field, Object value, Boolean hasDocValues) {
}
long v = parse(value, true);
Query query = LongPoint.newExactQuery(field, v);
if(hasDocValues){
if (hasDocValues) {
Query dvQuery = SortedNumericDocValuesField.newSlowExactQuery(field, v);
query = new IndexOrDocValuesQuery(query, dvQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ public void testNegativeZero() {
NumberType.HALF_FLOAT.rangeQuery("field", null, +0f, true, false, false, MOCK_QSC)
);

assertFalse(NumberType.DOUBLE.termQuery("field", -0d).equals(NumberType.DOUBLE.termQuery("field", +0d)));
assertFalse(NumberType.FLOAT.termQuery("field", -0f).equals(NumberType.FLOAT.termQuery("field", +0f)));
assertFalse(NumberType.HALF_FLOAT.termQuery("field", -0f).equals(NumberType.HALF_FLOAT.termQuery("field", +0f)));
assertFalse(NumberType.DOUBLE.termQuery("field", -0d, true).equals(NumberType.DOUBLE.termQuery("field", +0d, true)));
assertFalse(NumberType.FLOAT.termQuery("field", -0f, true).equals(NumberType.FLOAT.termQuery("field", +0f, true)));
assertFalse(NumberType.HALF_FLOAT.termQuery("field", -0f, true).equals(NumberType.HALF_FLOAT.termQuery("field", +0f, true)));
}

// Make sure we construct the IndexOrDocValuesQuery objects with queries that match
Expand Down

0 comments on commit e43d203

Please sign in to comment.