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 #17277

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
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();
ensureGreen("twitter");

client().prepareIndex("twitter").setId("1").setSource("user", "user1", "followers", new String[] { "user2", "user3" }).get();
client().prepareIndex("twitter").setId("2").setSource("user", "user2", "followers", new String[] { "user1" }).get();
refresh();

TermsQueryBuilder termsLookupQuery = QueryBuilders.termsLookupQuery("user", new TermsLookup("twitter", "2", "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,21 @@ public TransportExplainAction(
@Override
protected void doExecute(Task task, ExplainRequest request, ActionListener<ExplainResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(task, request, listener);
// super.doExecute(task, request, listener);
Copy link

Choose a reason for hiding this comment

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

I think you want to remove this line.

Copy link
Author

Choose a reason for hiding this comment

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

sure, will remove this line in next revision

LongSupplier timeProvider = () -> request.nowInMillis;
ActionListener<QueryBuilder> rewriteListener = ActionListener.wrap(rewrittenQuery -> {
Copy link

Choose a reason for hiding this comment

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

Why don't we simply switch from rewrite to rewriteAndFetch since now know this process can have async actions? Now basically we are doing rewriteAndFetch here and later I think we will still will do rewrite again.

Copy link
Author

Choose a reason for hiding this comment

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

here is Rewriteable javadoc - https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/8.17.1/org.elasticsearch.server/org/elasticsearch/index/query/Rewriteable.html

  • rewrite -
    Rewrites each element of the list until it doesn't change and returns a new list iff there is at least one element of the list that changed during it's rewrite.

  • rewriteAndFetch -
    Rewrites the given rewriteable and fetches pending async tasks for each round before rewriting again.

  • so we would need rewrite to be executed here

request.query(rewrittenQuery);
super.doExecute(task, request, listener);
}, listener::onFailure);
if (request.query() == null) {
rewriteListener.onResponse(request.query());
} else {
Rewriteable.rewriteAndFetch(
request.query(),
searchService.getIndicesService().getRewriteContext(timeProvider),
rewriteListener
);
}
}

@Override
Expand Down
Loading