Skip to content

Commit 2e4cc8c

Browse files
authored
Fix explain action on query rewrite (#17286) (#17286)
Signed-off-by: Fen Qin <mfenqin@amazon.com>
1 parent f6d6aa6 commit 2e4cc8c

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG-3.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7777
- 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))
7878
- Add HTTP/2 protocol support to HttpRequest.HttpVersion ([#17248](https://github.com/opensearch-project/OpenSearch/pull/17248))
7979
- Fix missing bucket in terms aggregation with missing value ([#17418](https://github.com/opensearch-project/OpenSearch/pull/17418))
80+
- Fix explain action on query rewrite ([#17286](https://github.com/opensearch-project/OpenSearch/pull/17286))
8081

8182
### Security
8283

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

+16-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;
@@ -101,7 +103,20 @@ public TransportExplainAction(
101103
@Override
102104
protected void doExecute(Task task, ExplainRequest request, ActionListener<ExplainResponse> listener) {
103105
request.nowInMillis = System.currentTimeMillis();
104-
super.doExecute(task, request, listener);
106+
// if there's no query we can't rewrite it
107+
if (request.query() == null) {
108+
super.doExecute(task, request, listener);
109+
return;
110+
}
111+
ActionListener<QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
112+
request.query(rewrittenQuery);
113+
super.doExecute(task, request, listener);
114+
}, listener::onFailure);
115+
Rewriteable.rewriteAndFetch(
116+
request.query(),
117+
searchService.getIndicesService().getRewriteContext(() -> request.nowInMillis),
118+
rewriteListener
119+
);
105120
}
106121

107122
@Override

0 commit comments

Comments
 (0)