Skip to content

Commit 13ab4ec

Browse files
mingshlmsfroh
andauthored
Introduce Template query (opensearch-project#16818)
Introduce template query that holds the content of query which can contain placeholders and can be filled by the variables from PipelineProcessingContext produced by search processors. This allows query rewrite by the search processors. --------- Signed-off-by: Mingshi Liu <[email protected]> Co-authored-by: Michael Froh <[email protected]>
1 parent 94dfe6d commit 13ab4ec

21 files changed

+1333
-111
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3535
- Added a new `time` field to replace the deprecated `getTime` field in `GetStats`. ([#17009](https://github.com/opensearch-project/OpenSearch/pull/17009))
3636
- Improve flat_object field parsing performance by reducing two passes to a single pass ([#16297](https://github.com/opensearch-project/OpenSearch/pull/16297))
3737
- Improve performance of the bitmap filtering([#16936](https://github.com/opensearch-project/OpenSearch/pull/16936/))
38+
- Introduce Template query ([#16818](https://github.com/opensearch-project/OpenSearch/pull/16818))
3839

3940
### Dependencies
4041
- Bump `com.google.cloud:google-cloud-core-http` from 2.23.0 to 2.47.0 ([#16504](https://github.com/opensearch-project/OpenSearch/pull/16504))

server/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ dependencies {
7070
api project(":libs:opensearch-telemetry")
7171
api project(":libs:opensearch-task-commons")
7272

73-
7473
compileOnly project(':libs:opensearch-plugin-classloader')
7574
testRuntimeOnly project(':libs:opensearch-plugin-classloader')
7675

server/src/main/java/org/opensearch/action/search/TransportSearchAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ private void executeRequest(
476476
} else {
477477
Rewriteable.rewriteAndFetch(
478478
sr.source(),
479-
searchService.getRewriteContext(timeProvider::getAbsoluteStartMillis),
479+
searchService.getRewriteContext(timeProvider::getAbsoluteStartMillis, searchRequest),
480480
rewriteListener
481481
);
482482
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.index.query;
10+
11+
import org.opensearch.client.Client;
12+
import org.opensearch.common.util.concurrent.CountDown;
13+
import org.opensearch.core.action.ActionListener;
14+
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
15+
import org.opensearch.core.xcontent.NamedXContentRegistry;
16+
import org.opensearch.core.xcontent.XContentParser;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.function.BiConsumer;
21+
import java.util.function.LongSupplier;
22+
23+
/**
24+
* BaseQueryRewriteContext is a base implementation of the QueryRewriteContext interface.
25+
* It provides core functionality for query rewriting operations in OpenSearch.
26+
*
27+
* This class manages the context for query rewriting, including handling of asynchronous actions,
28+
* access to content registries, and time-related operations.
29+
*/
30+
public class BaseQueryRewriteContext implements QueryRewriteContext {
31+
private final NamedXContentRegistry xContentRegistry;
32+
private final NamedWriteableRegistry writeableRegistry;
33+
protected final Client client;
34+
protected final LongSupplier nowInMillis;
35+
private final List<BiConsumer<Client, ActionListener<?>>> asyncActions = new ArrayList<>();
36+
private final boolean validate;
37+
38+
public BaseQueryRewriteContext(
39+
NamedXContentRegistry xContentRegistry,
40+
NamedWriteableRegistry writeableRegistry,
41+
Client client,
42+
LongSupplier nowInMillis
43+
) {
44+
this(xContentRegistry, writeableRegistry, client, nowInMillis, false);
45+
}
46+
47+
public BaseQueryRewriteContext(
48+
NamedXContentRegistry xContentRegistry,
49+
NamedWriteableRegistry writeableRegistry,
50+
Client client,
51+
LongSupplier nowInMillis,
52+
boolean validate
53+
) {
54+
55+
this.xContentRegistry = xContentRegistry;
56+
this.writeableRegistry = writeableRegistry;
57+
this.client = client;
58+
this.nowInMillis = nowInMillis;
59+
this.validate = validate;
60+
}
61+
62+
/**
63+
* The registry used to build new {@link XContentParser}s. Contains registered named parsers needed to parse the query.
64+
*/
65+
public NamedXContentRegistry getXContentRegistry() {
66+
return xContentRegistry;
67+
}
68+
69+
/**
70+
* Returns the time in milliseconds that is shared across all resources involved. Even across shards and nodes.
71+
*/
72+
public long nowInMillis() {
73+
return nowInMillis.getAsLong();
74+
}
75+
76+
public NamedWriteableRegistry getWriteableRegistry() {
77+
return writeableRegistry;
78+
}
79+
80+
/**
81+
* Returns an instance of {@link QueryShardContext} if available of null otherwise
82+
*/
83+
public QueryShardContext convertToShardContext() {
84+
return null;
85+
}
86+
87+
/**
88+
* Registers an async action that must be executed before the next rewrite round in order to make progress.
89+
* This should be used if a rewriteabel needs to fetch some external resources in order to be executed ie. a document
90+
* from an index.
91+
*/
92+
public void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncAction) {
93+
asyncActions.add(asyncAction);
94+
}
95+
96+
/**
97+
* Returns <code>true</code> if there are any registered async actions.
98+
*/
99+
public boolean hasAsyncActions() {
100+
return asyncActions.isEmpty() == false;
101+
}
102+
103+
/**
104+
* Executes all registered async actions and notifies the listener once it's done. The value that is passed to the listener is always
105+
* <code>null</code>. The list of registered actions is cleared once this method returns.
106+
*/
107+
public void executeAsyncActions(ActionListener listener) {
108+
if (asyncActions.isEmpty()) {
109+
listener.onResponse(null);
110+
return;
111+
}
112+
113+
CountDown countDown = new CountDown(asyncActions.size());
114+
ActionListener<?> internalListener = new ActionListener() {
115+
@Override
116+
public void onResponse(Object o) {
117+
if (countDown.countDown()) {
118+
listener.onResponse(null);
119+
}
120+
}
121+
122+
@Override
123+
public void onFailure(Exception e) {
124+
if (countDown.fastForward()) {
125+
listener.onFailure(e);
126+
}
127+
}
128+
};
129+
// make a copy to prevent concurrent modification exception
130+
List<BiConsumer<Client, ActionListener<?>>> biConsumers = new ArrayList<>(asyncActions);
131+
asyncActions.clear();
132+
for (BiConsumer<Client, ActionListener<?>> action : biConsumers) {
133+
action.accept(client, internalListener);
134+
}
135+
}
136+
137+
public boolean validate() {
138+
return validate;
139+
}
140+
}

server/src/main/java/org/opensearch/index/query/QueryBuilders.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.io.IOException;
5151
import java.util.Collection;
5252
import java.util.List;
53+
import java.util.Map;
5354

5455
/**
5556
* Utility class to create search queries.
@@ -780,4 +781,13 @@ public static GeoShapeQueryBuilder geoDisjointQuery(String name, String indexedS
780781
public static ExistsQueryBuilder existsQuery(String name) {
781782
return new ExistsQueryBuilder(name);
782783
}
784+
785+
/**
786+
* A query that contains a template with holder that should be resolved by search processors
787+
*
788+
* @param content The content of the template
789+
*/
790+
public static TemplateQueryBuilder templateQuery(Map<String, Object> content) {
791+
return new TemplateQueryBuilder(content);
792+
}
783793
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.index.query;
10+
11+
import org.opensearch.client.Client;
12+
import org.opensearch.common.annotation.PublicApi;
13+
import org.opensearch.core.action.ActionListener;
14+
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
15+
import org.opensearch.core.xcontent.NamedXContentRegistry;
16+
import org.opensearch.search.pipeline.PipelinedRequest;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.function.BiConsumer;
21+
22+
/**
23+
* The QueryCoordinatorContext class implements the QueryRewriteContext interface and provides
24+
* additional functionality for coordinating query rewriting in OpenSearch.
25+
*
26+
* This class acts as a wrapper around a QueryRewriteContext instance and a PipelinedRequest,
27+
* allowing access to both rewrite context methods and pass over search request information.
28+
*
29+
* @since 2.19.0
30+
*/
31+
@PublicApi(since = "2.19.0")
32+
public class QueryCoordinatorContext implements QueryRewriteContext {
33+
private final QueryRewriteContext rewriteContext;
34+
private final PipelinedRequest searchRequest;
35+
36+
public QueryCoordinatorContext(QueryRewriteContext rewriteContext, PipelinedRequest searchRequest) {
37+
this.rewriteContext = rewriteContext;
38+
this.searchRequest = searchRequest;
39+
}
40+
41+
@Override
42+
public NamedXContentRegistry getXContentRegistry() {
43+
return rewriteContext.getXContentRegistry();
44+
}
45+
46+
@Override
47+
public long nowInMillis() {
48+
return rewriteContext.nowInMillis();
49+
}
50+
51+
@Override
52+
public NamedWriteableRegistry getWriteableRegistry() {
53+
return rewriteContext.getWriteableRegistry();
54+
}
55+
56+
@Override
57+
public QueryShardContext convertToShardContext() {
58+
return rewriteContext.convertToShardContext();
59+
}
60+
61+
@Override
62+
public void registerAsyncAction(BiConsumer<Client, ActionListener<?>> asyncAction) {
63+
rewriteContext.registerAsyncAction(asyncAction);
64+
}
65+
66+
@Override
67+
public boolean hasAsyncActions() {
68+
return rewriteContext.hasAsyncActions();
69+
}
70+
71+
@Override
72+
public void executeAsyncActions(ActionListener listener) {
73+
rewriteContext.executeAsyncActions(listener);
74+
}
75+
76+
@Override
77+
public boolean validate() {
78+
return rewriteContext.validate();
79+
}
80+
81+
@Override
82+
public QueryCoordinatorContext convertToCoordinatorContext() {
83+
return this;
84+
}
85+
86+
public Map<String, Object> getContextVariables() {
87+
88+
// Read from pipeline context
89+
Map<String, Object> contextVariables = new HashMap<>(searchRequest.getPipelineProcessingContext().getAttributes());
90+
91+
return contextVariables;
92+
}
93+
}

0 commit comments

Comments
 (0)