Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix explain action on query rewrite #17286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Style Nit) It's not clear to the casual observer that this is a set of two key/value pairs. While this is valid, there are other options to use a Map for setting these keys. This is a test class so it's really not a big deal.

.setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put("index.number_of_routing_shards", 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Style Nit) It's strange to see one setting as a constant and the other one not. I think you can use INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING here.

.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 @@
@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
Loading