Skip to content

Commit

Permalink
Handled exception
Browse files Browse the repository at this point in the history
Signed-off-by: Owais Kazi <[email protected]>
  • Loading branch information
owaiskazi19 committed Apr 22, 2024
1 parent 8ff869f commit a4b65df
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.gateway.GatewayService;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexSettings;
import org.opensearch.index.analysis.AnalysisRegistry;
import org.opensearch.ingest.ConfigurationUtils;
Expand All @@ -64,6 +65,8 @@
/**
* The main entry point for search pipelines. Handles CRUD operations and exposes the API to execute search pipelines
* against requests and responses.
*
* @opensearch.internal
*/
public class SearchPipelineService implements ClusterStateApplier, ReportingService<SearchPipelineInfo> {

Expand Down Expand Up @@ -393,22 +396,26 @@ public PipelinedRequest resolvePipeline(SearchRequest searchRequest, IndexNameEx
// Named pipeline specified for the request
pipelineId = searchRequest.pipeline();
} else if (state != null && searchRequest.indices() != null && searchRequest.indices().length != 0) {
// Check for index default pipeline
Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, searchRequest);
for (Index index : concreteIndices) {
IndexMetadata indexMetadata = state.metadata().index(index);
if (indexMetadata != null) {
Settings indexSettings = indexMetadata.getSettings();
if (IndexSettings.DEFAULT_SEARCH_PIPELINE.exists(indexSettings)) {
String currentPipelineId = IndexSettings.DEFAULT_SEARCH_PIPELINE.get(indexSettings);
if (NOOP_PIPELINE_ID.equals(pipelineId)) {
pipelineId = currentPipelineId;
} else if (pipelineId.equals(currentPipelineId) == false) {
pipelineId = NOOP_PIPELINE_ID;
break;
try {
// Check for index default pipeline
Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, searchRequest);
for (Index index : concreteIndices) {
IndexMetadata indexMetadata = state.metadata().index(index);
if (indexMetadata != null) {
Settings indexSettings = indexMetadata.getSettings();
if (IndexSettings.DEFAULT_SEARCH_PIPELINE.exists(indexSettings)) {
String currentPipelineId = IndexSettings.DEFAULT_SEARCH_PIPELINE.get(indexSettings);
if (NOOP_PIPELINE_ID.equals(pipelineId)) {
pipelineId = currentPipelineId;
} else if (!pipelineId.equals(currentPipelineId)) {
pipelineId = NOOP_PIPELINE_ID;
break;
}
}
}
}
} catch (IndexNotFoundException e) {
logger.debug("Default pipeline not applied for {}", (Object) searchRequest.indices());
}
}
if (NOOP_PIPELINE_ID.equals(pipelineId) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1664,4 +1664,39 @@ public void testNoIndexResolveIndexDefaultPipeline() throws Exception {
assertEquals(5, pipelinedRequest.source().size());
}

public void testInvalidIndexResolveIndexDefaultPipeline() throws Exception {
SearchPipelineService service = createWithProcessors();

SearchPipelineMetadata metadata = new SearchPipelineMetadata(
Map.of(
"p1",
new PipelineConfiguration(
"p1",
new BytesArray("{\"request_processors\" : [ { \"scale_request_size\": { \"scale\" : 2 } } ] }"),
MediaTypeRegistry.JSON
)
)
);
Settings defaultPipelineSetting = Settings.builder()
.put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1)
.put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0)
.put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT)
.put(IndexSettings.DEFAULT_SEARCH_PIPELINE.getKey(), "p1")
.build();
IndexMetadata indexMetadata = new IndexMetadata.Builder("my_index").settings(defaultPipelineSetting).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
ClusterState previousState = clusterState;
clusterState = ClusterState.builder(clusterState)
.metadata(Metadata.builder().put(indexMetadata, false).putCustom(SearchPipelineMetadata.TYPE, metadata))
.build();

ClusterChangedEvent cce = new ClusterChangedEvent("", clusterState, previousState);
service.applyClusterState(cce);

SearchRequest searchRequest = new SearchRequest("xyz").source(SearchSourceBuilder.searchSource().size(5));
PipelinedRequest pipelinedRequest = syncTransformRequest(service.resolvePipeline(searchRequest, indexNameExpressionResolver));
assertEquals("_none", pipelinedRequest.getPipeline().getId());
assertEquals(5, pipelinedRequest.source().size());
}

}

0 comments on commit a4b65df

Please sign in to comment.