forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ApproximateRangeQuery and ApproximateQuery (opensearch-proj…
…ect#13788) 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]>
- Loading branch information
Showing
20 changed files
with
1,622 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
rest-api-spec/src/main/resources/rest-api-spec/test/search/370_approximate_range.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
+ ", approximateIndexQuery=" | ||
+ approximateIndexQuery.toString(field) | ||
+ ", dvQuery=" | ||
+ indexOrDocValuesQuery.getRandomAccessQuery().toString(field) | ||
+ ")"; | ||
} | ||
|
||
@Override | ||
public void visit(QueryVisitor visitor) { | ||
indexOrDocValuesQuery.visit(visitor); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (sameClassAs(obj) == false) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int h = classHash(); | ||
h = 31 * h + indexOrDocValuesQuery.getIndexQuery().hashCode(); | ||
h = 31 * h + indexOrDocValuesQuery.getRandomAccessQuery().hashCode(); | ||
return h; | ||
} | ||
} |
Oops, something went wrong.