Skip to content

Commit b67d076

Browse files
authored
ResolvedIndices: Introduced special case for no index in analyze requests. (#19849)
The analyze action has one special property that distinguishes it from all other actions extending TransportSingleShardAction: it can be executed without an index. This is a bit surprising, as we do not have a shard, while being in a class called "SingleShardAction". For this, the TransportAnalyzeAction does use a number of special cases. Thus, we also need to add a special case for the index resolution here. Signed-off-by: Nils Bandener <[email protected]>
1 parent 6b5c08a commit b67d076

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

server/src/main/java/org/opensearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.opensearch.cluster.ClusterState;
4747
import org.opensearch.cluster.block.ClusterBlockException;
4848
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
49+
import org.opensearch.cluster.metadata.ResolvedIndices;
4950
import org.opensearch.cluster.routing.ShardsIterator;
5051
import org.opensearch.cluster.service.ClusterService;
5152
import org.opensearch.common.inject.Inject;
@@ -151,6 +152,19 @@ protected AnalyzeAction.Response shardOperation(AnalyzeAction.Request request, S
151152
return analyze(request, indicesService.getAnalysis(), indexService, maxTokenCount);
152153
}
153154

155+
@Override
156+
public ResolvedIndices resolveIndices(AnalyzeAction.Request request) {
157+
if (request.index() == null) {
158+
// The analyze action has one special property that distinguishes it from all other actions extending
159+
// TransportSingleShardAction: it can be executed without an index. This is a bit surprising, as we do
160+
// not have a shard, while being in a class called "SingleShardAction". For this, the TransportAnalyzeAction
161+
// does use a number of special cases. Thus, we also need to add a special case for the index resolution here.
162+
return ResolvedIndices.of(ResolvedIndices.Local.of());
163+
} else {
164+
return super.resolveIndices(request);
165+
}
166+
}
167+
154168
public static AnalyzeAction.Response analyze(
155169
AnalyzeAction.Request request,
156170
AnalysisRegistry analysisRegistry,

server/src/test/java/org/opensearch/action/admin/indices/TransportAnalyzeActionTests.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@
3939
import org.opensearch.Version;
4040
import org.opensearch.action.admin.indices.analyze.AnalyzeAction;
4141
import org.opensearch.action.admin.indices.analyze.TransportAnalyzeAction;
42+
import org.opensearch.action.support.ActionFilters;
43+
import org.opensearch.cluster.ClusterName;
44+
import org.opensearch.cluster.ClusterState;
4245
import org.opensearch.cluster.metadata.IndexMetadata;
46+
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
47+
import org.opensearch.cluster.metadata.Metadata;
48+
import org.opensearch.cluster.metadata.ResolvedIndices;
49+
import org.opensearch.cluster.service.ClusterService;
4350
import org.opensearch.common.UUIDs;
4451
import org.opensearch.common.settings.Settings;
52+
import org.opensearch.common.util.concurrent.ThreadContext;
4553
import org.opensearch.env.Environment;
4654
import org.opensearch.env.TestEnvironment;
4755
import org.opensearch.index.IndexService;
@@ -55,12 +63,15 @@
5563
import org.opensearch.index.analysis.PreConfiguredCharFilter;
5664
import org.opensearch.index.analysis.TokenFilterFactory;
5765
import org.opensearch.index.analysis.TokenizerFactory;
66+
import org.opensearch.indices.IndicesService;
5867
import org.opensearch.indices.analysis.AnalysisModule;
5968
import org.opensearch.indices.analysis.AnalysisModule.AnalysisProvider;
6069
import org.opensearch.indices.analysis.AnalysisModuleTests.AppendCharFilter;
6170
import org.opensearch.plugins.AnalysisPlugin;
6271
import org.opensearch.test.IndexSettingsModule;
6372
import org.opensearch.test.OpenSearchTestCase;
73+
import org.opensearch.threadpool.ThreadPool;
74+
import org.opensearch.transport.TransportService;
6475

6576
import java.io.IOException;
6677
import java.io.Reader;
@@ -599,4 +610,53 @@ public void testDeprecationWarnings() throws IOException {
599610
analyze = TransportAnalyzeAction.analyze(req, registry, mockIndexService(), maxTokenCount);
600611
assertEquals(1, analyze.getTokens().size());
601612
}
613+
614+
public void testResolveIndicesForNoIndex() {
615+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
616+
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(threadContext);
617+
618+
TransportAnalyzeAction action = new TransportAnalyzeAction(
619+
Settings.EMPTY,
620+
mock(ThreadPool.class),
621+
mock(ClusterService.class),
622+
mock(TransportService.class),
623+
mock(IndicesService.class),
624+
mock(ActionFilters.class),
625+
indexNameExpressionResolver
626+
);
627+
628+
AnalyzeAction.Request request = new AnalyzeAction.Request().text("the quick brown fox");
629+
assertEquals(ResolvedIndices.of(ResolvedIndices.Local.of()), action.resolveIndices(request));
630+
}
631+
632+
public void testResolveIndicesForIndex() {
633+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
634+
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(threadContext);
635+
Metadata.Builder mdBuilder = Metadata.builder()
636+
.put(
637+
IndexMetadata.builder("test_index")
638+
.settings(
639+
settings(Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
640+
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)
641+
)
642+
);
643+
644+
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
645+
646+
ClusterService clusterService = mock(ClusterService.class);
647+
when(clusterService.state()).thenReturn(clusterState);
648+
649+
TransportAnalyzeAction action = new TransportAnalyzeAction(
650+
Settings.EMPTY,
651+
mock(ThreadPool.class),
652+
clusterService,
653+
mock(TransportService.class),
654+
mock(IndicesService.class),
655+
mock(ActionFilters.class),
656+
indexNameExpressionResolver
657+
);
658+
659+
AnalyzeAction.Request request = new AnalyzeAction.Request("test_index").text("the quick brown fox");
660+
assertEquals(ResolvedIndices.of(ResolvedIndices.Local.of("test_index")), action.resolveIndices(request));
661+
}
602662
}

0 commit comments

Comments
 (0)