Skip to content

Commit

Permalink
Fixes Numeric exact match queries to use range queries internally (#1…
Browse files Browse the repository at this point in the history
…1209) (#11747)

* Updating numeric term and terms queries to use IODVQ



* Addressing comments



* Fix formatting



* Fix changelog



* Addressing more comments + adding tests



* renaming yaml test



* Adding skip for bwc



* Adding new SortedUnsignedLongDocValuesSetQuery to allow for BitInteger Terms query



* Fixing some tests



* Remove duplicate skip



* Remove unused points declaration



* Change unsigned exact query to be consistent



* Use slowExactQuery from Unsigned Set Query



* Merging different yaml tests into a single test



* Updating test case for main



* Fix changelog



---------


(cherry picked from commit 178a7a0)

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 8dc3c0c commit 1ad7a5d
Show file tree
Hide file tree
Showing 11 changed files with 1,795 additions and 131 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Performance improvement for date histogram aggregations without sub-aggregations ([#11083](https://github.com/opensearch-project/OpenSearch/pull/11083))
- 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))
- Improved performance of numeric exact-match queries ([#11209](https://github.com/opensearch-project/OpenSearch/pull/11209))
- Change error message when per shard document limit is breached ([#11312](https://github.com/opensearch-project/OpenSearch/pull/11312))
- Improve boolean parsing performance ([#11308](https://github.com/opensearch-project/OpenSearch/pull/11308))
- Interpret byte array as primitive using VarHandles ([#11362](https://github.com/opensearch-project/OpenSearch/pull/11362))
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(), isSearchable());
if (boost() != 1f) {
query = new BoostQuery(query, boost());
}
Expand All @@ -210,13 +210,18 @@ 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));
scaledValues.add(scaledValue);
}
Query query = NumberFieldMapper.NumberType.LONG.termsQuery(name(), Collections.unmodifiableList(scaledValues));
Query query = NumberFieldMapper.NumberType.LONG.termsQuery(
name(),
Collections.unmodifiableList(scaledValues),
hasDocValues(),
isSearchable()
);
if (boost() != 1f) {
query = new BoostQuery(query, boost());
}
Expand All @@ -225,7 +230,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 All @@ -242,7 +247,7 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower
}
hi = Math.round(Math.floor(dValue));
}
Query query = NumberFieldMapper.NumberType.LONG.rangeQuery(name(), lo, hi, true, true, hasDocValues(), context);
Query query = NumberFieldMapper.NumberType.LONG.rangeQuery(name(), lo, hi, true, true, hasDocValues(), isSearchable(), context);
if (boost() != 1f) {
query = new BoostQuery(query, boost());
}
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 Expand Up @@ -112,7 +116,16 @@ public void testRangeQuery() throws IOException {
Double u = randomBoolean() ? null : (randomDouble() * 2 - 1) * 10000;
boolean includeLower = randomBoolean();
boolean includeUpper = randomBoolean();
Query doubleQ = NumberFieldMapper.NumberType.DOUBLE.rangeQuery("double", l, u, includeLower, includeUpper, false, MOCK_QSC);
Query doubleQ = NumberFieldMapper.NumberType.DOUBLE.rangeQuery(
"double",
l,
u,
includeLower,
includeUpper,
false,
true,
MOCK_QSC
);
Query scaledFloatQ = ft.rangeQuery(l, u, includeLower, includeUpper, MOCK_QSC);
assertEquals(searcher.count(doubleQ), searcher.count(scaledFloatQ));
}
Expand Down
Loading

0 comments on commit 1ad7a5d

Please sign in to comment.