Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Numeric exact match queries to use range queries internally #11209

Merged
merged 18 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Disable concurrent aggs for Diversified Sampler and Sampler aggs ([#11087](https://github.com/opensearch-project/OpenSearch/issues/11087))
- Made leader/follower check timeout setting dynamic ([#10528](https://github.com/opensearch-project/OpenSearch/pull/10528))
- Use iterative approach to evaluate Regex.simpleMatch ([#11060](https://github.com/opensearch-project/OpenSearch/pull/11060))
harshavamsi marked this conversation as resolved.
Show resolved Hide resolved
- Update NumberFieldMapper to use IndexOrDocValuesQuery ([#11209](https://github.
msfroh marked this conversation as resolved.
Show resolved Hide resolved
com/opensearch-project/OpenSearch/pull/11209))

### Deprecated

Expand Down
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 @@ -34,11 +34,13 @@

import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.SortedNumericDocValuesField;
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 +65,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 All @@ -75,7 +79,7 @@ public void testTermsQuery() {
long scaledValue1 = Math.round(value1 * ft.getScalingFactor());
double value2 = (randomDouble() * 2 - 1) * 10000;
long scaledValue2 = Math.round(value2 * ft.getScalingFactor());
assertEquals(LongPoint.newSetQuery("scaled_float", scaledValue1, scaledValue2), ft.termsQuery(Arrays.asList(value1, value2), null));
assertEquals(LongField.newSetQuery("scaled_float", scaledValue1, scaledValue2), ft.termsQuery(Arrays.asList(value1, value2), null));
}

public void testRangeQuery() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
setup:
- skip:
features: [ "headers" ]
---
"search on number fields with doc_values enabled":
- do:
indices.create:
index: test-iodvq
body:
mappings:
dynamic: false
properties:
byte:
type: byte
index: true
doc_values: true
harshavamsi marked this conversation as resolved.
Show resolved Hide resolved
double:
type: double
index: true
doc_values: true
float:
type: float
index: true
doc_values: true
half_float:
type: half_float
index: true
doc_values: true
integer:
type: integer
index: true
doc_values: true
long:
type: long
index: true
doc_values: true
short:
type: short
index: true
doc_values: true
unsigned_long:
type: unsigned_long
index: true
doc_values: true


- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 1
body:
byte: 1
double: 1.0
float: 1.0
half_float: 1.0
integer: 1
long: 1
short: 1
unsigned_long: 1
harshavamsi marked this conversation as resolved.
Show resolved Hide resolved

- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 2
body:
byte: 2
double: 1.0
float: 1.0
half_float: 1.0
integer: 1
long: 1
short: 1
unsigned_long: 1


- do:
headers:
Content-Type: application/json
index:
index: "test-iodvq"
id: 3
body:
byte: 3
double: 1.0
float: 1.0
half_float: 1.0
integer: 1
long: 1
short: 1
unsigned_long: 1


- do:
indices.refresh: {}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
byte: 1

- match: {hits.total: 1}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
double: 1.0

- match: {hits.total: 3}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
float: 1.0

- match: {hits.total: 3}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
integer: 1

- match: {hits.total: 3}

- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
term:
long: 1

- match: {hits.total: 3}


- do:
search:
rest_total_hits_as_int: true
index: test-iodvq
body:
query:
terms:
byte: [1, 2]

- match: {hits.total: 2}
Loading
Loading