
com.thematchbox.river.rest.RestRebuildIndexAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AbstractRiver Show documentation
Show all versions of AbstractRiver Show documentation
This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.
package com.thematchbox.river.rest;
/* Copyright 2015 theMatchBox
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import com.thematchbox.river.actions.ActionType;
import com.thematchbox.river.actions.IndexJob;
import com.thematchbox.river.actions.IndexKey;
import com.thematchbox.river.indexers.MatchBoxIndexerManager;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RestRebuildIndexAction extends BaseRestHandler {
private MatchBoxIndexerManager indexerManager;
@Inject
public RestRebuildIndexAction(Settings settings, Client client, RestController controller, MatchBoxIndexerManager indexerManager) {
super(settings, controller, client);
this.indexerManager = indexerManager;
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxRebuildIndex", this);
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxRebuildIndex/{index}", this);
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxRebuildIndex/{index}/{type}", this);
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
logger.debug("RestRebuildIndexAction.handleRequest called");
final String index = request.hasParam("index") ? request.param("index") : null;
final String type = request.hasParam("type") ? request.param("type") : null;
List indexResponseList = new ArrayList<>();
if (index == null || type == null) {
List matchingKeys = indexerManager.getMatchingKeys(index, type);
if (!matchingKeys.isEmpty()) {
for (IndexKey matchingKey : matchingKeys) {
IndexJob indexerRequest = new IndexJob(ActionType.FULL_REBUILD, matchingKey);
boolean added = indexerManager.addRequest(indexerRequest);
indexResponseList.add(new IndexResponse(indexerRequest, added));
}
}
} else {
IndexJob indexerRequest = new IndexJob(ActionType.FULL_REBUILD, index, type);
boolean added = indexerManager.addRequest(indexerRequest);
indexResponseList.add(new IndexResponse(indexerRequest, added));
}
try {
XContentBuilder builder = JsonXContent.contentBuilder();
RestHelper.appendFeedBackList(builder, indexResponseList);
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
} catch (IOException e) {
failed(channel, e);
}
}
private void failed(RestChannel channel, Throwable e) {
try {
channel.sendResponse(new BytesRestResponse(channel, e));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy