Package org.elasticsearch.action.index
Class IndexRequest
- All Implemented Interfaces:
org.apache.lucene.util.Accountable
,CompositeIndicesRequest
,DocWriteRequest<IndexRequest>
,IndicesRequest
,WriteRequest<IndexRequest>
,Writeable
,org.elasticsearch.core.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.-
Nested Class Summary
Nested classes/interfaces inherited from class org.elasticsearch.transport.TransportRequest
TransportRequest.Empty
Nested classes/interfaces inherited from interface org.elasticsearch.action.DocWriteRequest
DocWriteRequest.OpType
Nested classes/interfaces inherited from interface org.elasticsearch.action.IndicesRequest
IndicesRequest.Replaceable
Nested classes/interfaces inherited from interface org.elasticsearch.common.io.stream.Writeable
Writeable.Reader<V>, Writeable.Writer<V>
Nested classes/interfaces inherited from interface org.elasticsearch.action.support.WriteRequest
WriteRequest.RefreshPolicy
-
Field Summary
Modifier and TypeFieldDescriptionstatic long
Value forgetAutoGeneratedTimestamp()
if the document has an external provided ID.Fields inherited from class org.elasticsearch.action.support.replication.ReplicationRequest
DEFAULT_TIMEOUT, index, shardId, timeout, waitForActiveShards
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
Fields inherited from interface org.elasticsearch.action.DocWriteRequest
REQUIRE_ALIAS
-
Constructor Summary
ConstructorDescriptionIndexRequest(String index)
Constructs a new index request against the specific index.IndexRequest(String index, String type)
Deprecated.Types are in the process of being removed.IndexRequest(String index, String type, String id)
Deprecated.Types are in the process of being removed.IndexRequest(ShardId shardId, StreamInput in)
-
Method Summary
Modifier and TypeMethodDescriptionvoid
create(boolean create)
Set totrue
to force this index to useDocWriteRequest.OpType.CREATE
.defaultTypeIfNull(String defaultType)
Deprecated.Types are in the process of being removed.long
Returns the timestamp the auto generated ID was created or -1L if the document has no auto generated timestamp.org.elasticsearch.common.xcontent.XContentType
The content type.Returns a map from the full path of field names to the name of dynamic mapping templates.Returns the final ingest pipeline to be executed before indexing the document.Returns the ingest pipeline to be executed before indexing the documentid()
The id of the indexed document.Sets the id of the indexed document.long
If set, only perform this indexing request if the document was last modification was assigned this primary term.long
ifSeqNo()
If set, only perform this indexing request if the document was last modification was assigned this sequence number.boolean
Determines whether the request should be applied to data streams.Returns the indices options used to resolve indices.boolean
Returns whether or not the pipeline for this request has been resolved by the coordinating node.isPipelineResolved(boolean isPipelineResolved)
Sets if the pipeline for this request has been resolved by the coordinating node.boolean
Should this request override specifically require the destination to be an alias?boolean
isRetry()
Returnstrue
if this request has been sent to a shard copy more than once.void
onRetry()
This method is called before this replication request is retried the first time.opType()
Get the requested document operation type of the requestSets a string representation of theopType(OpType)
.opType(DocWriteRequest.OpType opType)
Sets the type of operation to perform.void
process(Version indexCreatedVersion, MappingMetadata mappingMd, String concreteIndex)
long
void
resolveRouting(Metadata metadata)
routing()
Controls the shard routing of the request.Controls the shard routing of the request.setDynamicTemplates(Map<String,String> dynamicTemplates)
Specifies a map from the full path of field names to the name of dynamic mapping templatessetFinalPipeline(String finalPipeline)
Sets the final ingest pipeline to be executed before indexing the document.setIfPrimaryTerm(long term)
only performs this indexing request if the document was last modification was assigned the given primary term.setIfSeqNo(long seqNo)
only perform this indexing request if the document was last modification was assigned the given sequence number.setPipeline(String pipeline)
Sets the ingest pipeline to be executed before indexing the documentsetRequireAlias(boolean requireAlias)
source()
The source of the document to index, recopied to a new array if it is unsafe.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).source(byte[] source, org.elasticsearch.common.xcontent.XContentType xContentType)
Sets the document to index in bytes form.Sets the content source to index using the default content type (Requests.INDEX_CONTENT_TYPE
)Sets the document source to index.Index the Map inRequests.INDEX_CONTENT_TYPE
formatIndex the Map as the provided content type.source(BytesReference source, org.elasticsearch.common.xcontent.XContentType xContentType)
Sets the document to index in bytes form.source(org.elasticsearch.common.xcontent.XContentBuilder sourceBuilder)
Sets the content source to index.Sets the content source to index.toString()
type()
Deprecated.Types are in the process of being removed.Deprecated.Types are in the process of being removed.validate()
long
version()
Returns stored version.version(long version)
Sets the version, which will perform the operation only if a matching version exists and no changes happened on the doc since then.Get the document version type for this requestversionType(VersionType versionType)
Sets the versioning type.void
writeThin(StreamOutput out)
Thin serialization that does not writeReplicationRequest.shardId
and will only writeReplicationRequest.index
if it is different from the index name inReplicationRequest.shardId
.void
writeTo(StreamOutput out)
Write this into the StreamOutput.Methods inherited from class org.elasticsearch.action.support.replication.ReplicatedWriteRequest
getRefreshPolicy, setRefreshPolicy
Methods inherited from class org.elasticsearch.action.support.replication.ReplicationRequest
createTask, getDescription, index, index, indices, routedBasedOnClusterVersion, shardId, timeout, timeout, timeout, waitForActiveShards, waitForActiveShards, waitForActiveShards
Methods inherited from class org.elasticsearch.action.ActionRequest
getShouldStoreResult
Methods inherited from class org.elasticsearch.transport.TransportRequest
getParentTask, setParentTask
Methods inherited from class org.elasticsearch.transport.TransportMessage
decRef, incRef, remoteAddress, remoteAddress, tryIncRef
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.lucene.util.Accountable
getChildResources
Methods inherited from interface org.elasticsearch.action.DocWriteRequest
index, index
Methods inherited from interface org.elasticsearch.action.IndicesRequest
allowsRemoteIndices, indices
Methods inherited from interface org.elasticsearch.tasks.TaskAwareRequest
setParentTask
Methods inherited from interface org.elasticsearch.action.support.WriteRequest
setRefreshPolicy
-
Field Details
-
UNSET_AUTO_GENERATED_TIMESTAMP
public static final long UNSET_AUTO_GENERATED_TIMESTAMPValue forgetAutoGeneratedTimestamp()
if the document has an external provided ID.- See Also:
- Constant Field Values
-
-
Constructor Details
-
IndexRequest
- Throws:
IOException
-
IndexRequest
- Throws:
IOException
-
IndexRequest
public IndexRequest() -
IndexRequest
Constructs a new index request against the specific index. Thetype(String)
source(byte[], XContentType)
must be set. -
IndexRequest
Deprecated.Types are in the process of being removed. UseIndexRequest(String)
instead.Constructs a new index request against the specific index and type. Thesource(byte[], XContentType)
must be set. -
IndexRequest
Deprecated.Types are in the process of being removed. UseIndexRequest(String)
withid(String)
instead.Constructs a new index request against the index, type, id and using the source.- Parameters:
index
- The index to index intotype
- The type to index intoid
- The id of document
-
-
Method Details
-
validate
- Specified by:
validate
in interfaceWriteRequest<IndexRequest>
- Overrides:
validate
in classReplicationRequest<IndexRequest>
-
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 interfaceDocWriteRequest<IndexRequest>
- Specified by:
indicesOptions
in interfaceIndicesRequest
- Overrides:
indicesOptions
in classReplicationRequest<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.Types are in the process of being removed.The type of the indexed document.- Specified by:
type
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the type
-
type
Deprecated.Types are in the process of being removed.Sets the type of the indexed document.- Specified by:
type
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Request
-
defaultTypeIfNull
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 interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Request
-
id
The id of the indexed document. If not set, will be automatically generated.- Specified by:
id
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the id
-
id
Sets the id of the indexed document. If not set, will be automatically generated. -
routing
Controls the shard routing of the request. Using this value to hash the shard and not the id.- Specified by:
routing
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Request
-
routing
Controls the shard routing of the request. Using this value to hash the shard and not the id.- Specified by:
routing
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Routing
-
setPipeline
Sets the ingest pipeline to be executed before indexing the document -
getPipeline
Returns the ingest pipeline to be executed before indexing the document -
setFinalPipeline
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
Returns the final ingest pipeline to be executed before indexing the document.- Returns:
- the name of the final pipeline
-
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
The source of the document to index, recopied to a new array if it is unsafe. -
sourceAsMap
-
source
Index the Map inRequests.INDEX_CONTENT_TYPE
format- Parameters:
source
- The map to index- Throws:
ElasticsearchGenerationException
-
source
public IndexRequest source(Map<String,?> source, org.elasticsearch.common.xcontent.XContentType contentType) throws ElasticsearchGenerationExceptionIndex the Map as the provided content type.- Parameters:
source
- The map to index- Throws:
ElasticsearchGenerationException
-
source
public IndexRequest source(String source, org.elasticsearch.common.xcontent.XContentType xContentType)Sets the document source to index. Note, its preferable to either set it usingsource(org.elasticsearch.common.xcontent.XContentBuilder)
or using thesource(byte[], XContentType)
. -
source
Sets the content source to index. -
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, 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 indexoffset
- The offset in the byte arraylength
- The length of the data
-
opType
Sets the type of operation to perform. -
opType
Sets a string representation of theopType(OpType)
. Can be either "index" or "create". -
create
Set totrue
to force this index to useDocWriteRequest.OpType.CREATE
. -
opType
Description copied from interface:DocWriteRequest
Get the requested document operation type of the request- Specified by:
opType
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the operation type
DocWriteRequest.OpType
-
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 interfaceDocWriteRequest<IndexRequest>
-
version
public long version()Returns stored version. If currently stored version isVersions.MATCH_ANY
and opType isDocWriteRequest.OpType.CREATE
, returnsVersions.MATCH_DELETED
.- Specified by:
version
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the document version
-
versionType
Description copied from interface:DocWriteRequest
Sets the versioning type. Defaults toVersionType.INTERNAL
.- Specified by:
versionType
in interfaceDocWriteRequest<IndexRequest>
-
setIfSeqNo
only perform this indexing request if the document was last modification was assigned the given sequence number. Must be used in combination withsetIfPrimaryTerm(long)
If the document last modification was assigned a different sequence number aVersionConflictEngineException
will be thrown.- Specified by:
setIfSeqNo
in interfaceDocWriteRequest<IndexRequest>
-
setIfPrimaryTerm
only performs this indexing request if the document was last modification was assigned the given primary term. Must be used in combination withsetIfSeqNo(long)
If the document last modification was assigned a different term aVersionConflictEngineException
will be thrown.- Specified by:
setIfPrimaryTerm
in interfaceDocWriteRequest<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 aVersionConflictEngineException
will be thrown.- Specified by:
ifSeqNo
in interfaceDocWriteRequest<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 aVersionConflictEngineException
will be thrown.- Specified by:
ifPrimaryTerm
in interfaceDocWriteRequest<IndexRequest>
-
versionType
Description copied from interface:DocWriteRequest
Get the document version type for this request- Specified by:
versionType
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the document version type
-
process
public void process(Version indexCreatedVersion, @Nullable MappingMetadata mappingMd, String concreteIndex) -
resolveRouting
-
checkAutoIdWithOpTypeCreateSupportedByVersion
-
writeTo
Description copied from interface:Writeable
Write this into the StreamOutput.- Specified by:
writeTo
in interfaceWriteable
- Overrides:
writeTo
in classReplicatedWriteRequest<IndexRequest>
- Throws:
IOException
-
writeThin
Description copied from class:ReplicationRequest
Thin serialization that does not writeReplicationRequest.shardId
and will only writeReplicationRequest.index
if it is different from the index name inReplicationRequest.shardId
.- Overrides:
writeThin
in classReplicatedWriteRequest<IndexRequest>
- Throws:
IOException
-
toString
- Specified by:
toString
in classReplicationRequest<IndexRequest>
-
includeDataStreams
public boolean includeDataStreams()Description copied from interface:IndicesRequest
Determines whether the request should be applied to data streams. Whenfalse
, none of the names or wildcard expressions inIndicesRequest.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 interfaceIndicesRequest
-
isRetry
public boolean isRetry()Returnstrue
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 classReplicationRequest<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 interfaceorg.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 interfaceDocWriteRequest<IndexRequest>
- Returns:
- boolean flag, when true specifically requires an alias
-
setRequireAlias
-
setDynamicTemplates
Specifies a map from the full path of field names to the name of dynamic mapping templates -
getDynamicTemplates
Returns a map from the full path of field names to the name of dynamic mapping templates.- See Also:
setDynamicTemplates(Map)
-