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

org.elasticsearch.client.action.index.IndexRequestBuilder Maven / Gradle / Ivy

There is a newer version: 8.15.1
Show newest version
/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you 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.elasticsearch.client.action.index;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.replication.ReplicationType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.action.support.BaseRequestBuilder;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;

import javax.annotation.Nullable;
import java.util.Map;

/**
 * An index document action request builder.
 *
 * @author kimchy (shay.banon)
 */
public class IndexRequestBuilder extends BaseRequestBuilder {

    public IndexRequestBuilder(Client client, @Nullable String index) {
        super(client, new IndexRequest(index));
    }

    /**
     * Sets the index to index the document to.
     */
    public IndexRequestBuilder setIndex(String index) {
        request.index(index);
        return this;
    }

    /**
     * Sets the type to index the document to.
     */
    public IndexRequestBuilder setType(String type) {
        request.type(type);
        return this;
    }

    /**
     * Sets the id to index the document under. Optional, and if not set, one will be automatically
     * generated.
     */
    public IndexRequestBuilder setId(String id) {
        request.id(id);
        return this;
    }

    /**
     * Controls the shard routing of the request. Using this value to hash the shard
     * and not the id.
     */
    public IndexRequestBuilder setRouting(String routing) {
        request.routing(routing);
        return this;
    }

    /**
     * Index the Map as a JSON.
     *
     * @param source The map to index
     */
    public IndexRequestBuilder setSource(Map source) {
        request.source(source);
        return this;
    }

    /**
     * Index the Map as the provided content type.
     *
     * @param source The map to index
     */
    public IndexRequestBuilder setSource(Map source, XContentType contentType) {
        request.source(source, contentType);
        return this;
    }

    /**
     * Sets the document source to index.
     *
     * 

Note, its preferable to either set it using {@link #setSource(org.elasticsearch.common.xcontent.XContentBuilder)} * or using the {@link #setSource(byte[])}. */ public IndexRequestBuilder setSource(String source) { request.source(source); return this; } /** * Sets the content source to index. */ public IndexRequestBuilder setSource(XContentBuilder sourceBuilder) { request.source(sourceBuilder); return this; } /** * Sets the document to index in bytes form. */ public IndexRequestBuilder setSource(byte[] source) { request.source(source); return this; } /** * Sets the document to index in bytes form (assumed to be safe to be used from different * threads). * * @param source The source to index * @param offset The offset in the byte array * @param length The length of the data */ public IndexRequestBuilder setSource(byte[] source, int offset, int length) { request.source(source, offset, length); return this; } /** * Sets the document to index in bytes form. * * @param source The source to index * @param offset The offset in the byte array * @param length The length of the data * @param unsafe Is the byte array safe to be used form a different thread */ public IndexRequestBuilder setSource(byte[] source, int offset, int length, boolean unsafe) { request.source(source, offset, length, unsafe); return this; } /** * Constructs a simple document with a field and a value. */ public IndexRequestBuilder setSource(String field1, Object value1) { request.source(field1, value1); return this; } /** * Constructs a simple document with a field and value pairs. */ public IndexRequestBuilder setSource(String field1, Object value1, String field2, Object value2) { request.source(field1, value1, field2, value2); return this; } /** * Constructs a simple document with a field and value pairs. */ public IndexRequestBuilder setSource(String field1, Object value1, String field2, Object value2, String field3, Object value3) { request.source(field1, value1, field2, value2, field3, value3); return this; } /** * Constructs a simple document with a field and value pairs. */ public IndexRequestBuilder setSource(String field1, Object value1, String field2, Object value2, String field3, Object value3, String field4, Object value4) { request.source(field1, value1, field2, value2, field3, value3, field4, value4); return this; } /** * The content type that will be used to generate a document from user provided objects (like Map). */ public IndexRequestBuilder setContentType(XContentType contentType) { request.contentType(contentType); return this; } /** * A timeout to wait if the index operation can't be performed immediately. Defaults to 1m. */ public IndexRequestBuilder setTimeout(TimeValue timeout) { request.timeout(timeout); return this; } /** * A timeout to wait if the index operation can't be performed immediately. Defaults to 1m. */ public IndexRequestBuilder setTimeout(String timeout) { request.timeout(timeout); return this; } /** * Sets the type of operation to perform. */ public IndexRequestBuilder setOpType(IndexRequest.OpType opType) { request.opType(opType); return this; } /** * Sets a string representation of the {@link #setOpType(org.elasticsearch.action.index.IndexRequest.OpType)}. Can * be either "index" or "create". */ public IndexRequestBuilder setOpType(String opType) { request.opType(opType); return this; } /** * Set to true to force this index to use {@link org.elasticsearch.action.index.IndexRequest.OpType#CREATE}. */ public IndexRequestBuilder setCreate(boolean create) { request.create(create); return this; } /** * Should a refresh be executed post this index operation causing the operation to * be searchable. Note, heavy indexing should not set this to true. Defaults * to false. */ public IndexRequestBuilder setRefresh(boolean refresh) { request.refresh(refresh); return this; } /** * Set the replication type for this operation. */ public IndexRequestBuilder setReplicationType(ReplicationType replicationType) { request.replicationType(replicationType); return this; } /** * Sets the consistency level. Defaults to {@link org.elasticsearch.action.WriteConsistencyLevel#DEFAULT}. */ public IndexRequestBuilder setConsistencyLevel(WriteConsistencyLevel consistencyLevel) { request.consistencyLevel(consistencyLevel); return this; } /** * Set the replication type for this operation. */ public IndexRequestBuilder setReplicationType(String replicationType) { request.replicationType(replicationType); return this; } /** * Should the listener be called on a separate thread if needed. */ public IndexRequestBuilder setListenerThreaded(boolean listenerThreaded) { request.listenerThreaded(listenerThreaded); return this; } /** * Controls if the operation will be executed on a separate thread when executed locally. Defaults * to true when running in embedded mode. */ public IndexRequestBuilder setOperationThreaded(boolean operationThreaded) { request.operationThreaded(operationThreaded); return this; } @Override protected void doExecute(ActionListener listener) { client.index(request, listener); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy