Class IndexRequest

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

Index request to index a typed JSON document into a specific index and make it searchable.

The index requires the ReplicationRequest.index(), 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.xcontent.XContentBuilder)).

If the id(String) is not set, it will be automatically generated.

See Also:
  • Field Details

    • MAX_DOCUMENT_ID_LENGTH_IN_BYTES

      public static final int MAX_DOCUMENT_ID_LENGTH_IN_BYTES
      Maximal allowed length (in bytes) of the document ID.
      See Also:
    • UNSET_AUTO_GENERATED_TIMESTAMP

      public static final long UNSET_AUTO_GENERATED_TIMESTAMP
      Value for getAutoGeneratedTimestamp() if the document has an external provided ID.
      See Also:
  • Constructor Details

  • Method Details

    • validate

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

      public 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
    • id

      public 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(String id)
      Sets the id of the indexed document. If not set, will be automatically generated.
    • routing

      public IndexRequest routing(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 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(String pipeline)
      Sets the ingest pipeline to be executed before indexing the document
    • getPipeline

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

      public IndexRequest setFinalPipeline(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 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 Map<String,Object> sourceAsMap()
    • sourceAsMap

      public Map<String,Object> sourceAsMap(DocumentSizeObserver documentSizeObserver)
    • source

      public IndexRequest source(Map<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(Map<String,?> source, XContentType contentType) throws ElasticsearchGenerationException
      Index the Map as the provided content type.
      Parameters:
      source - The map to index
      Throws:
      ElasticsearchGenerationException
    • source

      public IndexRequest source(Map<String,?> source, XContentType contentType, boolean ensureNoSelfReferences) throws ElasticsearchGenerationException
      Throws:
      ElasticsearchGenerationException
    • source

      public IndexRequest source(String source, XContentType xContentType)
      Sets the document source to index.

      Note, its preferable to either set it using source(org.elasticsearch.xcontent.XContentBuilder) or using the source(byte[], XContentType).

    • source

      public IndexRequest source(XContentBuilder sourceBuilder)
      Sets the content source to index.
    • source

      public IndexRequest source(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(XContentType xContentType, 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.

    • getXContentBuilder

      public static XContentBuilder getXContentBuilder(XContentType xContentType, Object... source)
      Returns an XContentBuilder for the given xContentType and source array

      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, XContentType xContentType)
      Sets the document to index in bytes form.
    • source

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

      public IndexRequest source(byte[] source, int offset, int length, 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(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(IndexRouting indexRouting)
      Description copied from interface: DocWriteRequest
      Finalize the request before executing or routing it.
      Specified by:
      process in interface DocWriteRequest<IndexRequest>
    • autoGenerateId

      public void autoGenerateId()
      Set the #id() to an automatically generated one and make this request compatible with the append-only optimization.
    • reset

      public void reset()
      Resets this IndexRequest class, so that in case this instance can be used by the bulk/index action if it was already used before. For example if retrying a retryable failure.
    • writeTo

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

      public void writeThin(StreamOutput out) throws 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:
      IOException
    • toString

      public 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
    • isRequireDataStream

      public boolean isRequireDataStream()
      Description copied from interface: DocWriteRequest
      Should this request override specifically require the destination to be a data stream?
      Specified by:
      isRequireDataStream in interface DocWriteRequest<IndexRequest>
      Returns:
      boolean flag, when true specifically requires a data stream
    • setRequireDataStream

      public IndexRequest setRequireDataStream(boolean requireDataStream)
      Set whether this IndexRequest requires a data stream. The data stream may be pre-existing or to-be-created.
    • getConcreteWriteIndex

      public Index getConcreteWriteIndex(IndexAbstraction ia, Metadata metadata)
      Description copied from interface: DocWriteRequest
      Resolves the write index that should receive this request based on the provided index abstraction.
      Specified by:
      getConcreteWriteIndex in interface DocWriteRequest<IndexRequest>
      Parameters:
      ia - The provided index abstraction
      metadata - The metadata instance used to resolve the write index.
      Returns:
      the write index that should receive this request
    • route

      public int route(IndexRouting indexRouting)
      Description copied from interface: DocWriteRequest
      Pick the appropriate shard id to receive this request.
      Specified by:
      route in interface DocWriteRequest<IndexRequest>
    • setRequireAlias

      public IndexRequest setRequireAlias(boolean requireAlias)
    • isWriteToFailureStore

      public boolean isWriteToFailureStore()
    • setWriteToFailureStore

      public IndexRequest setWriteToFailureStore(boolean writeToFailureStore)
    • setListExecutedPipelines

      public IndexRequest setListExecutedPipelines(boolean listExecutedPipelines)
    • getListExecutedPipelines

      public boolean getListExecutedPipelines()
    • setDynamicTemplates

      public IndexRequest setDynamicTemplates(Map<String,String> dynamicTemplates)
      Specifies a map from the full path of field names to the name of dynamic mapping templates
    • getDynamicTemplates

      public Map<String,String> getDynamicTemplates()
      Returns a map from the full path of field names to the name of dynamic mapping templates.
      See Also:
    • getRawTimestamp

      public Object getRawTimestamp()
    • setRawTimestamp

      public void setRawTimestamp(Object rawTimestamp)
    • getNormalisedBytesParsed

      public long getNormalisedBytesParsed()
      Returns a number of bytes observed when parsing a document in earlier stages of ingestion (like update/ingest service) Defaults to -1 when a document size was not observed in earlier stages.
      Returns:
      a number of bytes observed
    • setNormalisedBytesParsed

      public IndexRequest setNormalisedBytesParsed(long normalisedBytesParsed)
      Sets number of bytes observed by a DocumentSizeObserver
      Returns:
      an index request
    • noParsedBytesToReport

      public IndexRequest noParsedBytesToReport()
      when observing document size while parsing, this method indicates that this request should not be recorded.
      Returns:
      an index request
    • addPipeline

      public void addPipeline(String pipeline)
      Adds the pipeline to the list of executed pipelines, if listExecutedPipelines is true
      Parameters:
      pipeline -
    • getExecutedPipelines

      @Nullable public List<String> getExecutedPipelines()
      This returns the list of pipelines executed on the document for this request. If listExecutedPipelines is false, the response will be null, even if pipelines were executed. If listExecutedPipelines is true but no pipelines were executed, the list will be empty.
      Returns: