Skip to content

Commit

Permalink
Fix explain action on query rewrite
Browse files Browse the repository at this point in the history
Signed-off-by: Fen Qin <[email protected]>
  • Loading branch information
Fen Qin authored and fen-qin committed Feb 13, 2025
1 parent 38e4b33 commit a111f2f
Show file tree
Hide file tree
Showing 3 changed files with 33 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 @@ -65,6 +65,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Don't over-allocate in HeapBufferedAsyncEntityConsumer in order to consume the response ([#9993](https://github.com/opensearch-project/OpenSearch/pull/9993))
- 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 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,22 @@ 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();

Explanation explanation = response.getExplanation();
assertNotNull(explanation);
assertTrue(explanation.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 All @@ -65,6 +67,7 @@

import java.io.IOException;
import java.util.Set;
import java.util.function.LongSupplier;

/**
* Explain transport action. Computes the explain on the targeted shard.
Expand Down Expand Up @@ -101,7 +104,14 @@ public TransportExplainAction(
@Override
protected void doExecute(Task task, ExplainRequest request, ActionListener<ExplainResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(task, request, listener);
ActionListener<QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
request.query(rewrittenQuery);
super.doExecute(task, request, listener);
}, listener::onFailure);

assert request.query() != null;
LongSupplier timeProvider = () -> request.nowInMillis;
Rewriteable.rewriteAndFetch(request.query(), searchService.getIndicesService().getRewriteContext(timeProvider), rewriteListener);
}

@Override
Expand Down

0 comments on commit a111f2f

Please sign in to comment.