org.codelibs.elasticsearch.client.action.HttpDeleteAction Maven / Gradle / Ivy
/*
* Copyright 2012-2019 CodeLibs Project and the Others.
*
* 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.
*/
package org.codelibs.elasticsearch.client.action;
import java.util.Locale;
import org.codelibs.curl.CurlRequest;
import org.codelibs.elasticsearch.client.HttpClient;
import org.codelibs.elasticsearch.client.util.UrlUtils;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteAction;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.VersionType;
public class HttpDeleteAction extends HttpAction {
protected final DeleteAction action;
public HttpDeleteAction(final HttpClient client, final DeleteAction action) {
super(client);
this.action = action;
}
public void execute(final DeleteRequest request, final ActionListener listener) {
getCurlRequest(request).execute(response -> {
try (final XContentParser parser = createParser(response)) {
final DeleteResponse deleteResponse = DeleteResponse.fromXContent(parser);
listener.onResponse(deleteResponse);
} catch (final Exception e) {
listener.onFailure(toElasticsearchException(response, e));
}
}, e -> unwrapElasticsearchException(listener, e));
}
protected CurlRequest getCurlRequest(final DeleteRequest request) {
// RestDeleteAction
final CurlRequest curlRequest = client.getCurlRequest(DELETE, "/_doc/" + UrlUtils.encode(request.id()), request.index());
if (request.timeout() != null) {
curlRequest.param("timeout", request.timeout().toString());
}
if (!RefreshPolicy.NONE.equals(request.getRefreshPolicy())) {
curlRequest.param("refresh", request.getRefreshPolicy().getValue());
}
if (!VersionType.INTERNAL.equals(request.versionType())) {
curlRequest.param("version_type", request.versionType().name().toLowerCase(Locale.ROOT));
}
curlRequest.param("if_seq_no", Long.toString(request.ifSeqNo()));
curlRequest.param("if_primary_term", Long.toString(request.ifPrimaryTerm()));
if (!ActiveShardCount.DEFAULT.equals(request.waitForActiveShards())) {
curlRequest.param("wait_for_active_shards", String.valueOf(getActiveShardsCountValue(request.waitForActiveShards())));
}
return curlRequest.param("routing", request.routing()).param("version", String.valueOf(request.version()));
}
}