All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.sinri.keel.elasticsearch.document.ESDocumentMixin Maven / Gradle / Ivy

Go to download

A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.

There is a newer version: 3.2.18
Show newest version
package io.github.sinri.keel.elasticsearch.document;

import io.github.sinri.keel.elasticsearch.ESApiMixin;
import io.vertx.core.Future;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public interface ESDocumentMixin extends ESApiMixin {
    default Future documentCreate(String indexName, @Nullable String documentId, @Nullable ESApiQueries queries, JsonObject documentBody) {
        return Future.succeededFuture()
                .compose(v -> {
                    if (documentId == null) {
                        return callPost("/" + indexName + "/_doc/", queries, documentBody);
                    } else {
                        return callPost("/" + indexName + "/_create/" + documentId, queries, documentBody);
                    }
                })
                .compose(resp -> {
                    return Future.succeededFuture(new ESDocumentCreateResponse(resp));
                });
    }

    /**
     * @see Get API
     */
    default Future documentGet(String indexName, String documentId, @Nullable ESApiQueries queries) {
        return call(HttpMethod.GET, "/" + indexName + "/_doc/" + documentId, queries, null)
                .compose(resp -> {
                    return Future.succeededFuture(new ESDocumentGetResponse(resp));
                });
    }

    /**
     * @see Delete API
     */
    default Future documentDelete(String indexName, String documentId, @Nullable ESApiQueries queries) {
        return call(HttpMethod.DELETE, "/" + indexName + "/_doc/" + documentId, queries, null)
                .compose(resp -> {
                    return Future.succeededFuture(new ESDocumentDeleteResponse(resp));
                });
    }

    /**
     * @see Update API
     */
    default Future documentUpdate(String indexName, String documentId, @Nullable ESApiQueries queries, JsonObject requestBody) {
        return callPost("/" + indexName + "/_update/" + documentId, queries, requestBody)
                .compose(resp -> {
                    return Future.succeededFuture(new ESDocumentUpdateResponse(resp));
                });
    }

    /**
     * Performs multiple indexing or delete operations in a single API call.
     * This reduces overhead and can greatly increase indexing speed.
     *
     * @param target (Optional, string) Name of the data stream, index, or index alias to perform bulk actions on.
     * @see Bulk API
     * @since 3.1.10
     */
    default Future documentBulk(@Nullable String target, @Nullable ESApiQueries queries, @Nonnull List requestBody) {
        // POST /_bulk
        // POST //_bulk
        String endpoint = "/_bulk";
        if (target != null) {
            endpoint = "/" + target + endpoint;
        }
        StringBuilder body = new StringBuilder();
        requestBody.forEach(x -> {
            body.append(x.toString()).append("\n");
        });
        return call(HttpMethod.POST, endpoint, queries, body.toString())
                .compose(resp -> {
                    return Future.succeededFuture(new ESDocumentBulkResponse(resp));
                });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy