
com.thematchbox.river.rest.RestUpdateIndexAction 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 RestUpdateIndexAction extends BaseRestHandler {
private MatchBoxIndexerManager indexerManager;
@Inject
public RestUpdateIndexAction(Settings settings, RestController controller, Client client, MatchBoxIndexerManager indexerManager) {
super(settings, controller, client);
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxUpdateIndex", this);
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxUpdateIndex/{index}", this);
controller.registerHandler(RestRequest.Method.POST, "/_matchBoxUpdateIndex/{index}/{type}", this);
this.indexerManager = indexerManager;
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
logger.debug("RestUpdateIndexAction.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) {
addRequest(indexResponseList, matchingKey, ActionType.INCREMENTAL_UPDATE);
addRequest(indexResponseList, matchingKey, ActionType.INCREMENTAL_DELETE);
}
}
} else {
addRequest(indexResponseList, new IndexKey(index, type), ActionType.INCREMENTAL_UPDATE);
addRequest(indexResponseList, new IndexKey(index, type), ActionType.INCREMENTAL_DELETE);
}
try {
XContentBuilder builder = JsonXContent.contentBuilder();
RestHelper.appendFeedBackList(builder, indexResponseList);
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
} catch (IOException e) {
failed(channel, e);
}
}
private void addRequest(List indexResponseList, IndexKey matchingKey, ActionType actionType) {
IndexJob indexerRequest = new IndexJob(actionType, matchingKey);
boolean added = indexerManager.addRequest(indexerRequest);
indexResponseList.add(new IndexResponse(indexerRequest, added));
}
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