-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This introduces a basic "approximation" framework that improves query performance by modifying the query in a way that should be functionally equivalent. To start, we can reduce the bounds of a range query in order to satisfy the `track_total_hits` value (which defaults to 10,000). --------- Signed-off-by: Harsha Vamsi Kalluri <[email protected]> Signed-off-by: Michael Froh <[email protected]> Co-authored-by: Michael Froh <[email protected]> (cherry picked from commit 2e9db40)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
"search with approximate range": | ||
- do: | ||
indices.create: | ||
index: test | ||
body: | ||
mappings: | ||
properties: | ||
date: | ||
type: date | ||
index: true | ||
doc_values: true | ||
|
||
- do: | ||
bulk: | ||
index: test | ||
refresh: true | ||
body: | ||
- '{"index": {"_index": "test", "_id": "1" }}' | ||
- '{ "date": "2018-10-29T12:12:12.987Z" }' | ||
- '{ "index": { "_index": "test", "_id": "2" }}' | ||
- '{ "date": "2020-10-29T12:12:12.987Z" }' | ||
- '{ "index": { "_index": "test", "_id": "3" } }' | ||
- '{ "date": "2024-10-29T12:12:12.987Z" }' | ||
|
||
- do: | ||
search: | ||
rest_total_hits_as_int: true | ||
index: test | ||
body: | ||
query: | ||
range: { | ||
date: { | ||
gte: "2018-10-29T12:12:12.987Z" | ||
}, | ||
} | ||
|
||
- match: { hits.total: 3 } | ||
|
||
- do: | ||
search: | ||
rest_total_hits_as_int: true | ||
index: test | ||
body: | ||
sort: [{ date: asc }] | ||
query: | ||
range: { | ||
date: { | ||
gte: "2018-10-29T12:12:12.987Z" | ||
}, | ||
} | ||
|
||
|
||
- match: { hits.total: 3 } | ||
- match: { hits.hits.0._id: "1" } | ||
|
||
- do: | ||
search: | ||
rest_total_hits_as_int: true | ||
index: test | ||
body: | ||
sort: [{ date: desc }] | ||
query: | ||
range: { | ||
date: { | ||
gte: "2018-10-29T12:12:12.987Z", | ||
lte: "2020-10-29T12:12:12.987Z" | ||
}, | ||
} | ||
|
||
- match: { hits.total: 2 } | ||
- match: { hits.hits.0._id: "2" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.approximate; | ||
|
||
import org.apache.lucene.search.IndexOrDocValuesQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.QueryVisitor; | ||
|
||
/** | ||
* A wrapper around {@link IndexOrDocValuesQuery} that can be used to run approximate queries. | ||
* It delegates to either {@link ApproximateQuery} or {@link IndexOrDocValuesQuery} based on whether the query can be approximated or not. | ||
* @see ApproximateQuery | ||
*/ | ||
public final class ApproximateIndexOrDocValuesQuery extends ApproximateScoreQuery { | ||
|
||
private final ApproximateQuery approximateIndexQuery; | ||
private final IndexOrDocValuesQuery indexOrDocValuesQuery; | ||
|
||
public ApproximateIndexOrDocValuesQuery(Query indexQuery, ApproximateQuery approximateIndexQuery, Query dvQuery) { | ||
super(new IndexOrDocValuesQuery(indexQuery, dvQuery), approximateIndexQuery); | ||
this.approximateIndexQuery = approximateIndexQuery; | ||
this.indexOrDocValuesQuery = new IndexOrDocValuesQuery(indexQuery, dvQuery); | ||
} | ||
|
||
@Override | ||
public String toString(String field) { | ||
return "ApproximateIndexOrDocValuesQuery(indexQuery=" | ||
+ indexOrDocValuesQuery.getIndexQuery().toString(field) | ||
Check warning on line 34 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L33-L34
|
||
+ ", approximateIndexQuery=" | ||
+ approximateIndexQuery.toString(field) | ||
Check warning on line 36 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L36
|
||
+ ", dvQuery=" | ||
+ indexOrDocValuesQuery.getRandomAccessQuery().toString(field) | ||
Check warning on line 38 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L38
|
||
+ ")"; | ||
} | ||
|
||
@Override | ||
public void visit(QueryVisitor visitor) { | ||
indexOrDocValuesQuery.visit(visitor); | ||
} | ||
Check warning on line 45 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L44-L45
|
||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (sameClassAs(obj) == false) { | ||
return false; | ||
Check warning on line 50 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L50
|
||
} | ||
return true; | ||
Check warning on line 52 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L52
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int h = classHash(); | ||
h = 31 * h + indexOrDocValuesQuery.getIndexQuery().hashCode(); | ||
h = 31 * h + indexOrDocValuesQuery.getRandomAccessQuery().hashCode(); | ||
return h; | ||
} | ||
Check warning on line 61 in server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java Codecov / codecov/patchserver/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java#L57-L61
|
||
} |