Skip to content

Commit

Permalink
Fix explain action on query rewrite (#17286)
Browse files Browse the repository at this point in the history
Signed-off-by: Fen Qin <[email protected]>
  • Loading branch information
fen-qin committed Feb 26, 2025
1 parent e397903 commit a5778cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- 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))
- Add HTTP/2 protocol support to HttpRequest.HttpVersion ([#17248](https://github.com/opensearch-project/OpenSearch/pull/17248))
- Fix missing bucket in terms aggregation with missing value ([#17418](https://github.com/opensearch-project/OpenSearch/pull/17418))
- Fix explain action on query rewrite ([#17286](https://github.com/opensearch-project/OpenSearch/pull/17286))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.index.query.TermsQueryBuilder;
import org.opensearch.indices.TermsLookup;
import org.opensearch.test.OpenSearchIntegTestCase;

import java.io.ByteArrayInputStream;
Expand All @@ -52,6 +54,7 @@
import java.util.Set;

import static java.util.Collections.singleton;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.index.query.QueryBuilders.queryStringQuery;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
Expand Down Expand Up @@ -305,4 +308,25 @@ public void testStreamExplain() throws Exception {
result = Lucene.readExplanation(esBuffer);
assertThat(exp.toString(), equalTo(result.toString()));
}

public void testQueryRewrite() {
client().admin()
.indices()
.prepareCreate("twitter")
.setMapping("user", "type=integer", "followers", "type=integer")
.setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put("index.number_of_routing_shards", 2))
.get();
client().prepareIndex("twitter").setId("1").setSource("followers", new int[] { 1, 2, 3 }).get();
refresh();

TermsQueryBuilder termsLookupQuery = QueryBuilders.termsLookupQuery("user", new TermsLookup("twitter", "1", "followers"));
ExplainResponse response = client().prepareExplain("twitter", "1").setQuery(termsLookupQuery).get();
assertNotNull(response);
assertTrue(response.isExists());
assertFalse(response.isMatch());
assertThat(response.getIndex(), equalTo("twitter"));
assertThat(response.getId(), equalTo("1"));
assertNotNull(response.getExplanation());
assertFalse(response.getExplanation().isMatch());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.opensearch.index.get.GetResult;
import org.opensearch.index.mapper.IdFieldMapper;
import org.opensearch.index.mapper.Uid;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.Rewriteable;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.search.SearchService;
import org.opensearch.search.internal.AliasFilter;
Expand Down Expand Up @@ -101,7 +103,20 @@ public TransportExplainAction(
@Override
protected void doExecute(Task task, ExplainRequest request, ActionListener<ExplainResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(task, request, listener);
// if there's no query we can't rewrite it
if (request.query() == null) {
super.doExecute(task, request, listener);
return;

Check warning on line 109 in server/src/main/java/org/opensearch/action/explain/TransportExplainAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/explain/TransportExplainAction.java#L108-L109

Added lines #L108 - L109 were not covered by tests
}
ActionListener<QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
request.query(rewrittenQuery);
super.doExecute(task, request, listener);
}, listener::onFailure);
Rewriteable.rewriteAndFetch(
request.query(),
searchService.getIndicesService().getRewriteContext(() -> request.nowInMillis),

Check warning on line 117 in server/src/main/java/org/opensearch/action/explain/TransportExplainAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/explain/TransportExplainAction.java#L111-L117

Added lines #L111 - L117 were not covered by tests
rewriteListener
);
}

@Override
Expand Down

0 comments on commit a5778cb

Please sign in to comment.