Class IndexRequest

All Implemented Interfaces:
org.apache.lucene.util.Accountable, CompositeIndicesRequest, DocWriteRequest<IndexRequest>, IndicesRequest, WriteRequest<IndexRequest>, Writeable, org.elasticsearch.common.util.concurrent.RefCounted, TaskAwareRequest

public class IndexRequest
extends ReplicatedWriteRequest<IndexRequest>
implements DocWriteRequest<IndexRequest>, CompositeIndicesRequest
Index request to index a typed JSON document into a specific index and make it searchable. Best created using Requests.indexRequest(String). The index requires the ReplicationRequest.index(), type(String), id(String) and source(byte[], XContentType) to be set. The source (content to index) can be set in its bytes form using (source(byte[], XContentType)), its string form (source(String, XContentType)) or using a XContentBuilder (source(org.elasticsearch.common.xcontent.XContentBuilder)). If the id(String) is not set, it will be automatically generated.
See Also:
IndexResponse, Requests.indexRequest(String), Client.index(IndexRequest)
  • Field Details

  • Constructor Details

    • IndexRequest

      public IndexRequest​(StreamInput in) throws java.io.IOException
      Throws:
      java.io.IOException
    • IndexRequest

      public IndexRequest​(@Nullable ShardId shardId, StreamInput in) throws java.io.IOException
      Throws:
      java.io.IOException
    • IndexRequest

      public IndexRequest()
    • IndexRequest

      public IndexRequest​(java.lang.String index)
      Constructs a new index request against the specific index. The type(String) source(byte[], XContentType) must be set.
    • IndexRequest

      @Deprecated public IndexRequest​(java.lang.String index, java.lang.String type)
      Deprecated.
      Types are in the process of being removed. Use IndexRequest(String) instead.
      Constructs a new index request against the specific index and type. The source(byte[], XContentType) must be set.
    • IndexRequest

      @Deprecated public IndexRequest​(java.lang.String index, java.lang.String type, java.lang.String id)
      Deprecated.
      Types are in the process of being removed. Use IndexRequest(String) with id(String) instead.
      Constructs a new index request against the index, type, id and using the source.
      Parameters:
      index - The index to index into
      type - The type to index into
      id - The id of document
  • Method Details

    • validate

      Specified by:
      validate in interface WriteRequest<IndexRequest>
      Overrides:
      validate in class ReplicationRequest<IndexRequest>
    • indicesOptions

      public IndicesOptions indicesOptions()
      Description copied from interface: IndicesRequest
      Returns the indices options used to resolve indices. They tell for instance whether a single index is accepted, whether an empty array will be converted to _all, and how wildcards will be expanded if needed.
      Specified by:
      indicesOptions in interface DocWriteRequest<IndexRequest>
      Specified by:
      indicesOptions in interface IndicesRequest
      Overrides:
      indicesOptions in class ReplicationRequest<IndexRequest>
      Returns:
      the indices options
    • getContentType

      public org.elasticsearch.common.xcontent.XContentType getContentType()
      The content type. This will be used when generating a document from user provided objects like Maps and when parsing the source at index time
    • type

      @Deprecated public java.lang.String type()
      Deprecated.
      Types are in the process of being removed.
      The type of the indexed document.
      Specified by:
      type in interface DocWriteRequest<IndexRequest>
      Returns:
      the type
    • type

      @Deprecated public IndexRequest type​(java.lang.String type)
      Deprecated.
      Types are in the process of being removed.
      Sets the type of the indexed document.
      Specified by:
      type in interface DocWriteRequest<IndexRequest>
      Returns:
      the Request
    • defaultTypeIfNull

      @Deprecated public IndexRequest defaultTypeIfNull​(java.lang.String defaultType)
      Deprecated.
      Types are in the process of being removed.
      Set the default type supplied to a bulk request if this individual request's type is null or empty
      Specified by:
      defaultTypeIfNull in interface DocWriteRequest<IndexRequest>
      Returns:
      the Request
    • id

      public java.lang.String id()
      The id of the indexed document. If not set, will be automatically generated.
      Specified by:
      id in interface DocWriteRequest<IndexRequest>
      Returns:
      the id
    • id

      public IndexRequest id​(java.lang.String id)
      Sets the id of the indexed document. If not set, will be automatically generated.
    • routing

      public IndexRequest routing​(java.lang.String routing)
      Controls the shard routing of the request. Using this value to hash the shard and not the id.
      Specified by:
      routing in interface DocWriteRequest<IndexRequest>
      Returns:
      the Request
    • routing

      public java.lang.String routing()
      Controls the shard routing of the request. Using this value to hash the shard and not the id.
      Specified by:
      routing in interface DocWriteRequest<IndexRequest>
      Returns:
      the Routing
    • setPipeline

      public IndexRequest setPipeline​(java.lang.String pipeline)
      Sets the ingest pipeline to be executed before indexing the document
    • getPipeline

      public java.lang.String getPipeline()
      Returns the ingest pipeline to be executed before indexing the document
    • setFinalPipeline

      public IndexRequest setFinalPipeline​(java.lang.String finalPipeline)
      Sets the final ingest pipeline to be executed before indexing the document.
      Parameters:
      finalPipeline - the name of the final pipeline
      Returns:
      this index request
    • getFinalPipeline

      public java.lang.String getFinalPipeline()
      Returns the final ingest pipeline to be executed before indexing the document.
      Returns:
      the name of the final pipeline
    • isPipelineResolved

      public IndexRequest isPipelineResolved​(boolean isPipelineResolved)
      Sets if the pipeline for this request has been resolved by the coordinating node.
      Parameters:
      isPipelineResolved - true if the pipeline has been resolved
      Returns:
      the request
    • isPipelineResolved

      public boolean isPipelineResolved()
      Returns whether or not the pipeline for this request has been resolved by the coordinating node.
      Returns:
      true if the pipeline has been resolved
    • source

      public BytesReference source()
      The source of the document to index, recopied to a new array if it is unsafe.
    • sourceAsMap

      public java.util.Map<java.lang.String,​java.lang.Object> sourceAsMap()
    • source

      public IndexRequest source​(java.util.Map<java.lang.String,​?> source) throws ElasticsearchGenerationException
      Index the Map in Requests.INDEX_CONTENT_TYPE format
      Parameters:
      source - The map to index
      Throws:
      ElasticsearchGenerationException
    • source

      public IndexRequest source​(java.util.Map<java.lang.String,​?> source, org.elasticsearch.common.xcontent.XContentType contentType) throws ElasticsearchGenerationException
      Index the Map as the provided content type.
      Parameters:
      source - The map to index
      Throws:
      ElasticsearchGenerationException
    • source

      public IndexRequest source​(java.lang.String source, org.elasticsearch.common.xcontent.XContentType xContentType)
      Sets the document source to index. Note, its preferable to either set it using source(org.elasticsearch.common.xcontent.XContentBuilder) or using the source(byte[], XContentType).
    • source

      public IndexRequest source​(org.elasticsearch.common.xcontent.XContentBuilder sourceBuilder)
      Sets the content source to index.
    • source

      public IndexRequest source​(java.lang.Object... source)
      Sets the content source to index using the default content type (Requests.INDEX_CONTENT_TYPE)

      Note: the number of objects passed to this method must be an even number. Also the first argument in each pair (the field name) must have a valid String representation.

    • source

      public IndexRequest source​(org.elasticsearch.common.xcontent.XContentType xContentType, java.lang.Object... source)
      Sets the content source to index.

      Note: the number of objects passed to this method as varargs must be an even number. Also the first argument in each pair (the field name) must have a valid String representation.

    • source

      public IndexRequest source​(BytesReference source, org.elasticsearch.common.xcontent.XContentType xContentType)
      Sets the document to index in bytes form.
    • source

      public IndexRequest source​(byte[] source, org.elasticsearch.common.xcontent.XContentType xContentType)
      Sets the document to index in bytes form.
    • source

      public IndexRequest source​(byte[] source, int offset, int length, org.elasticsearch.common.xcontent.XContentType xContentType)
      Sets the document to index in bytes form (assumed to be safe to be used from different threads).
      Parameters:
      source - The source to index
      offset - The offset in the byte array
      length - The length of the data
    • opType

      public IndexRequest opType​(DocWriteRequest.OpType opType)
      Sets the type of operation to perform.
    • opType

      public IndexRequest opType​(java.lang.String opType)
      Sets a string representation of the opType(OpType). Can be either "index" or "create".
    • create

      public IndexRequest create​(boolean create)
      Set to true to force this index to use DocWriteRequest.OpType.CREATE.
    • opType

      public DocWriteRequest.OpType opType()
      Description copied from interface: DocWriteRequest
      Get the requested document operation type of the request
      Specified by:
      opType in interface DocWriteRequest<IndexRequest>
      Returns:
      the operation type DocWriteRequest.OpType
    • version

      public IndexRequest version​(long version)
      Description copied from interface: DocWriteRequest
      Sets the version, which will perform the operation only if a matching version exists and no changes happened on the doc since then.
      Specified by:
      version in interface DocWriteRequest<IndexRequest>
    • version

      public long version()
      Returns stored version. If currently stored version is Versions.MATCH_ANY and opType is DocWriteRequest.OpType.CREATE, returns Versions.MATCH_DELETED.
      Specified by:
      version in interface DocWriteRequest<IndexRequest>
      Returns:
      the document version
    • versionType

      public IndexRequest versionType​(VersionType versionType)
      Description copied from interface: DocWriteRequest
      Sets the versioning type. Defaults to VersionType.INTERNAL.
      Specified by:
      versionType in interface DocWriteRequest<IndexRequest>
    • setIfSeqNo

      public IndexRequest setIfSeqNo​(long seqNo)
      only perform this indexing request if the document was last modification was assigned the given sequence number. Must be used in combination with setIfPrimaryTerm(long) If the document last modification was assigned a different sequence number a VersionConflictEngineException will be thrown.
      Specified by:
      setIfSeqNo in interface DocWriteRequest<IndexRequest>
    • setIfPrimaryTerm

      public IndexRequest setIfPrimaryTerm​(long term)
      only performs this indexing request if the document was last modification was assigned the given primary term. Must be used in combination with setIfSeqNo(long) If the document last modification was assigned a different term a VersionConflictEngineException will be thrown.
      Specified by:
      setIfPrimaryTerm in interface DocWriteRequest<IndexRequest>
    • ifSeqNo

      public long ifSeqNo()
      If set, only perform this indexing request if the document was last modification was assigned this sequence number. If the document last modification was assigned a different sequence number a VersionConflictEngineException will be thrown.
      Specified by:
      ifSeqNo in interface DocWriteRequest<IndexRequest>
    • ifPrimaryTerm

      public long ifPrimaryTerm()
      If set, only perform this indexing request if the document was last modification was assigned this primary term. If the document last modification was assigned a different term a VersionConflictEngineException will be thrown.
      Specified by:
      ifPrimaryTerm in interface DocWriteRequest<IndexRequest>
    • versionType

      public VersionType versionType()
      Description copied from interface: DocWriteRequest
      Get the document version type for this request
      Specified by:
      versionType in interface DocWriteRequest<IndexRequest>
      Returns:
      the document version type
    • process

      public void process​(Version indexCreatedVersion, @Nullable MappingMetadata mappingMd, java.lang.String concreteIndex)
    • resolveRouting

      public void resolveRouting​(Metadata metadata)
    • checkAutoIdWithOpTypeCreateSupportedByVersion

      public void checkAutoIdWithOpTypeCreateSupportedByVersion​(Version version)
    • writeTo

      public void writeTo​(StreamOutput out) throws java.io.IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Specified by:
      writeTo in interface Writeable
      Overrides:
      writeTo in class ReplicatedWriteRequest<IndexRequest>
      Throws:
      java.io.IOException
    • writeThin

      public void writeThin​(StreamOutput out) throws java.io.IOException
      Description copied from class: ReplicationRequest
      Thin serialization that does not write ReplicationRequest.shardId and will only write ReplicationRequest.index if it is different from the index name in ReplicationRequest.shardId.
      Overrides:
      writeThin in class ReplicatedWriteRequest<IndexRequest>
      Throws:
      java.io.IOException
    • toString

      public java.lang.String toString()
      Specified by:
      toString in class ReplicationRequest<IndexRequest>
    • includeDataStreams

      public boolean includeDataStreams()
      Description copied from interface: IndicesRequest
      Determines whether the request should be applied to data streams. When false, none of the names or wildcard expressions in IndicesRequest.indices() should be applied to or expanded to any data streams. All layers involved in the request's fulfillment including security, name resolution, etc., should respect this flag.
      Specified by:
      includeDataStreams in interface IndicesRequest
    • isRetry

      public boolean isRetry()
      Returns true if this request has been sent to a shard copy more than once.
    • onRetry

      public void onRetry()
      Description copied from class: ReplicationRequest
      This method is called before this replication request is retried the first time.
      Overrides:
      onRetry in class ReplicationRequest<IndexRequest>
    • getAutoGeneratedTimestamp

      public long getAutoGeneratedTimestamp()
      Returns the timestamp the auto generated ID was created or -1L if the document has no auto generated timestamp. This method will return a positive value iff the id was auto generated.
    • ramBytesUsed

      public long ramBytesUsed()
      Specified by:
      ramBytesUsed in interface org.apache.lucene.util.Accountable
    • isRequireAlias

      public boolean isRequireAlias()
      Description copied from interface: DocWriteRequest
      Should this request override specifically require the destination to be an alias?
      Specified by:
      isRequireAlias in interface DocWriteRequest<IndexRequest>
      Returns:
      boolean flag, when true specifically requires an alias
    • setRequireAlias

      public IndexRequest setRequireAlias​(boolean requireAlias)