Skip to content

Commit

Permalink
Fix querying a data stream name in _index field. (#63183)
Browse files Browse the repository at this point in the history
Backport #63170 to 7.9 branch.

The _index field is a special field that allows using queries against the name of an index or alias.
Data stream names were not included, this pr fixes that by changing SearchIndexNameMatcher
(which used via IndexFieldMapper) to also include data streams.
  • Loading branch information
martijnvg committed Oct 2, 2020
1 parent c5a4b0e commit 1b423b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean test(String pattern) {

private boolean matchesIndex(String pattern) {
String[] concreteIndices = expressionResolver.concreteIndexNames(
clusterService.state(), IndicesOptions.lenientExpandOpen(), pattern);
clusterService.state(), IndicesOptions.lenientExpandOpen(), true, pattern);
for (String index : concreteIndices) {
if (Regex.simpleMatch(index, indexName)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.elasticsearch.datastreams;

import org.apache.lucene.search.TotalHits;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.DocWriteRequest;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESIntegTestCase;
Expand Down Expand Up @@ -1049,6 +1051,21 @@ public void testAutoCreateV1TemplateNoDataStream() {
assertThat(getIndexResponse.getSettings().get("logs-foobar").get(IndexMetadata.SETTING_NUMBER_OF_REPLICAS), equalTo("0"));
}

public void testQueryDataStreamNameInIndexField() throws Exception {
putComposableIndexTemplate("id1", List.of("metrics-*"));
CreateDataStreamAction.Request createDataStreamRequest = new CreateDataStreamAction.Request("metrics-foo");
client().execute(CreateDataStreamAction.INSTANCE, createDataStreamRequest).get();

indexDocs("metrics-foo", 1);
indexDocs("metrics-bar", 1);

SearchRequest searchRequest = new SearchRequest("*");
searchRequest.source().query(new TermQueryBuilder("_index", "metrics-foo"));
SearchResponse searchResponse = client().search(searchRequest).actionGet();
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
assertThat(searchResponse.getHits().getTotalHits().relation, equalTo(TotalHits.Relation.EQUAL_TO));
}

private static void verifyResolvability(String dataStream, ActionRequestBuilder<?, ?> requestBuilder, boolean fail) {
verifyResolvability(dataStream, requestBuilder, fail, 0);
}
Expand Down

0 comments on commit 1b423b1

Please sign in to comment.