Skip to content

Commit ceadf37

Browse files
committed
Fix explain action on query rewrite (opensearch-project#17286)
Signed-off-by: Fen Qin <[email protected]>
1 parent 8447737 commit ceadf37

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG-3.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7171
- Fix swapped field formats in nodes API where `total_indexing_buffer_in_bytes` and `total_indexing_buffer` values were reversed ([#17070](https://github.com/opensearch-project/OpenSearch/pull/17070))
7272
- Add HTTP/2 protocol support to HttpRequest.HttpVersion ([#17248](https://github.com/opensearch-project/OpenSearch/pull/17248))
7373
- Fix missing bucket in terms aggregation with missing value ([#17418](https://github.com/opensearch-project/OpenSearch/pull/17418))
74+
- Fix explain action on query rewrite ([#17286](https://github.com/opensearch-project/OpenSearch/pull/17286))
7475

7576
### Security
7677

server/src/internalClusterTest/java/org/opensearch/explain/ExplainActionIT.java

+24
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
4141
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
4242
import org.opensearch.index.query.QueryBuilders;
43+
import org.opensearch.index.query.TermsQueryBuilder;
44+
import org.opensearch.indices.TermsLookup;
4345
import org.opensearch.test.OpenSearchIntegTestCase;
4446

4547
import java.io.ByteArrayInputStream;
@@ -52,6 +54,7 @@
5254
import java.util.Set;
5355

5456
import static java.util.Collections.singleton;
57+
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
5558
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
5659
import static org.opensearch.index.query.QueryBuilders.queryStringQuery;
5760
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
@@ -305,4 +308,25 @@ public void testStreamExplain() throws Exception {
305308
result = Lucene.readExplanation(esBuffer);
306309
assertThat(exp.toString(), equalTo(result.toString()));
307310
}
311+
312+
public void testQueryRewrite() {
313+
client().admin()
314+
.indices()
315+
.prepareCreate("twitter")
316+
.setMapping("user", "type=integer", "followers", "type=integer")
317+
.setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put("index.number_of_routing_shards", 2))
318+
.get();
319+
client().prepareIndex("twitter").setId("1").setSource("followers", new int[] { 1, 2, 3 }).get();
320+
refresh();
321+
322+
TermsQueryBuilder termsLookupQuery = QueryBuilders.termsLookupQuery("user", new TermsLookup("twitter", "1", "followers"));
323+
ExplainResponse response = client().prepareExplain("twitter", "1").setQuery(termsLookupQuery).get();
324+
assertNotNull(response);
325+
assertTrue(response.isExists());
326+
assertFalse(response.isMatch());
327+
assertThat(response.getIndex(), equalTo("twitter"));
328+
assertThat(response.getId(), equalTo("1"));
329+
assertNotNull(response.getExplanation());
330+
assertFalse(response.getExplanation().isMatch());
331+
}
308332
}

server/src/main/java/org/opensearch/action/explain/TransportExplainAction.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import org.opensearch.index.get.GetResult;
5353
import org.opensearch.index.mapper.IdFieldMapper;
5454
import org.opensearch.index.mapper.Uid;
55+
import org.opensearch.index.query.QueryBuilder;
56+
import org.opensearch.index.query.Rewriteable;
5557
import org.opensearch.index.shard.IndexShard;
5658
import org.opensearch.search.SearchService;
5759
import org.opensearch.search.internal.AliasFilter;
@@ -65,6 +67,7 @@
6567

6668
import java.io.IOException;
6769
import java.util.Set;
70+
import java.util.function.LongSupplier;
6871

6972
/**
7073
* Explain transport action. Computes the explain on the targeted shard.
@@ -101,7 +104,14 @@ public TransportExplainAction(
101104
@Override
102105
protected void doExecute(Task task, ExplainRequest request, ActionListener<ExplainResponse> listener) {
103106
request.nowInMillis = System.currentTimeMillis();
104-
super.doExecute(task, request, listener);
107+
ActionListener<QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
108+
request.query(rewrittenQuery);
109+
super.doExecute(task, request, listener);
110+
}, listener::onFailure);
111+
112+
assert request.query() != null;
113+
LongSupplier timeProvider = () -> request.nowInMillis;
114+
Rewriteable.rewriteAndFetch(request.query(), searchService.getIndicesService().getRewriteContext(timeProvider), rewriteListener);
105115
}
106116

107117
@Override

0 commit comments

Comments
 (0)