-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Search Task API and Refactor search actions and handlers (#149)
Signed-off-by: Xun Zhang <xunzh@amazon.com>
- Loading branch information
1 parent
69195fd
commit afa4788
Showing
9 changed files
with
251 additions
and
137 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
common/src/main/java/org/opensearch/ml/common/transport/task/MLTaskSearchAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.opensearch.ml.common.transport.task; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.ml.common.transport.model.MLModelSearchAction; | ||
|
||
public class MLTaskSearchAction extends ActionType<SearchResponse> { | ||
// External Action which used for public facing RestAPIs. | ||
public static final String NAME = "cluster:admin/opensearch/ml/tasks/search"; | ||
public static final MLTaskSearchAction INSTANCE = new MLTaskSearchAction(); | ||
|
||
private MLTaskSearchAction() { | ||
super(NAME, SearchResponse::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
plugin/src/main/java/org/opensearch/ml/action/models/SearchTaskTransportAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.action.models; | ||
|
||
import org.opensearch.action.ActionListener; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.HandledTransportAction; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.ml.action.handler.MLSearchHandler; | ||
import org.opensearch.ml.common.transport.task.MLTaskSearchAction; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
public class SearchTaskTransportAction extends HandledTransportAction<SearchRequest, SearchResponse> { | ||
private MLSearchHandler mlSearchHandler; | ||
|
||
@Inject | ||
public SearchTaskTransportAction(TransportService transportService, ActionFilters actionFilters, MLSearchHandler mlSearchHandler) { | ||
super(MLTaskSearchAction.NAME, transportService, actionFilters, SearchRequest::new); | ||
this.mlSearchHandler = mlSearchHandler; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> actionListener) { | ||
mlSearchHandler.search(request, actionListener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
plugin/src/main/java/org/opensearch/ml/rest/AbstractMLSearchAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.opensearch.ml.rest; | ||
|
||
import static org.opensearch.common.xcontent.ToXContent.EMPTY_PARAMS; | ||
import static org.opensearch.ml.utils.RestActionUtils.getSourceContext; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.common.xcontent.ToXContentObject; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.RestResponse; | ||
import org.opensearch.rest.RestStatus; | ||
import org.opensearch.rest.action.RestResponseListener; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
|
||
public abstract class AbstractMLSearchAction<T extends ToXContentObject> extends BaseRestHandler { | ||
|
||
protected final List<String> urlPaths; | ||
protected final String index; | ||
protected final Class<T> clazz; | ||
protected final ActionType<SearchResponse> actionType; | ||
|
||
public AbstractMLSearchAction(List<String> urlPaths, String index, Class<T> clazz, ActionType<SearchResponse> actionType) { | ||
this.urlPaths = urlPaths; | ||
this.index = index; | ||
this.clazz = clazz; | ||
this.actionType = actionType; | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
searchSourceBuilder.parseXContent(request.contentOrSourceParamParser()); | ||
searchSourceBuilder.fetchSource(getSourceContext(request)); | ||
searchSourceBuilder.seqNoAndPrimaryTerm(true).version(true); | ||
SearchRequest searchRequest = new SearchRequest().source(searchSourceBuilder).indices(index); | ||
return channel -> client.execute(actionType, searchRequest, search(channel)); | ||
} | ||
|
||
protected RestResponseListener<SearchResponse> search(RestChannel channel) { | ||
return new RestResponseListener<SearchResponse>(channel) { | ||
@Override | ||
public RestResponse buildResponse(SearchResponse response) throws Exception { | ||
if (response.isTimedOut()) { | ||
return new BytesRestResponse(RestStatus.REQUEST_TIMEOUT, response.toString()); | ||
} | ||
return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), EMPTY_PARAMS)); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
List<Route> routes = new ArrayList<>(); | ||
for (String path : urlPaths) { | ||
routes.add(new Route(RestRequest.Method.POST, path)); | ||
routes.add(new Route(RestRequest.Method.GET, path)); | ||
} | ||
return routes; | ||
} | ||
} |
Oops, something went wrong.