Package org.elasticsearch.action.index
Class IndexRequest
- java.lang.Object
-
- All Implemented Interfaces:
CompositeIndicesRequest
,DocWriteRequest<IndexRequest>
,IndicesRequest
,WriteRequest<IndexRequest>
,Streamable
,Writeable
,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 usingRequests.indexRequest(String)
. The index requires theReplicationRequest.index()
,type(String)
,id(String)
andsource(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 aXContentBuilder
(source(org.elasticsearch.common.xcontent.XContentBuilder)
). If theid(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
Fields Modifier and Type Field Description static int
UNSET_AUTO_GENERATED_TIMESTAMP
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
-
-
Constructor Summary
Constructors Constructor Description IndexRequest()
IndexRequest(java.lang.String index)
Constructs a new index request against the specific index.IndexRequest(java.lang.String index, java.lang.String type)
Constructs a new index request against the specific index and type.IndexRequest(java.lang.String index, java.lang.String type, java.lang.String id)
Constructs a new index request against the index, type, id and using the source.
-
Method Summary
Modifier and Type Method Description IndexRequest
create(boolean create)
Set totrue
to force this index to useDocWriteRequest.OpType.CREATE
.long
getAutoGeneratedTimestamp()
Returns the timestamp the auto generated ID was created or -1 if the document has no auto generated timestamp.XContentType
getContentType()
The content type.java.lang.String
getPipeline()
Returns the ingest pipeline to be executed before indexing the documentjava.lang.String
id()
The id of the indexed document.IndexRequest
id(java.lang.String id)
Sets the id of the indexed document.long
ifPrimaryTerm()
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
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.DocWriteRequest.OpType
opType()
Get the requested document operation type of the requestIndexRequest
opType(java.lang.String opType)
Sets a string representation of theopType(OpType)
.IndexRequest
opType(DocWriteRequest.OpType opType)
Sets the type of operation to perform.java.lang.String
parent()
Get the parent for this requestIndexRequest
parent(java.lang.String parent)
Sets the parent id of this document.void
process(Version indexCreatedVersion, MappingMetaData mappingMd, java.lang.String concreteIndex)
void
readFrom(StreamInput in)
Set this object's fields from a StreamInput.void
resolveRouting(MetaData metaData)
java.lang.String
routing()
Controls the shard routing of the request.IndexRequest
routing(java.lang.String routing)
Controls the shard routing of the request.IndexRequest
setIfPrimaryTerm(long term)
only performs this indexing request if the document was last modification was assigned the given primary term.IndexRequest
setIfSeqNo(long seqNo)
only perform this indexing request if the document was last modification was assigned the given sequence number.IndexRequest
setPipeline(java.lang.String pipeline)
Sets the ingest pipeline to be executed before indexing the documentIndexRequest
setShardId(ShardId shardId)
Override this method from ReplicationAction, this is where we are storing our state in the request object (which we really shouldn't do).BytesReference
source()
The source of the document to index, recopied to a new array if it is unsafe.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).IndexRequest
source(byte[] source, XContentType xContentType)
Sets the document to index in bytes form.IndexRequest
source(java.lang.Object... source)
Sets the content source to index using the default content type (Requests.INDEX_CONTENT_TYPE
)IndexRequest
source(java.lang.String source, XContentType xContentType)
Sets the document source to index.IndexRequest
source(java.util.Map source)
Index the Map inRequests.INDEX_CONTENT_TYPE
formatIndexRequest
source(java.util.Map source, XContentType contentType)
Index the Map as the provided content type.IndexRequest
source(BytesReference source, XContentType xContentType)
Sets the document to index in bytes form.IndexRequest
source(XContentBuilder sourceBuilder)
Sets the content source to index.IndexRequest
source(XContentType xContentType, java.lang.Object... source)
Sets the content source to index.java.util.Map<java.lang.String,java.lang.Object>
sourceAsMap()
java.lang.String
toString()
java.lang.String
type()
The type of the indexed document.IndexRequest
type(java.lang.String type)
Sets the type of the indexed document.ActionRequestValidationException
validate()
long
version()
Returns stored version.IndexRequest
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.VersionType
versionType()
Get the document version type for this requestIndexRequest
versionType(VersionType versionType)
Sets the versioning type.void
writeTo(StreamOutput out)
Write this object's fields to a 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, indicesOptions, 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
remoteAddress, remoteAddress
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.elasticsearch.action.DocWriteRequest
index, index, indicesOptions
-
Methods inherited from interface org.elasticsearch.action.IndicesRequest
indices
-
Methods inherited from interface org.elasticsearch.tasks.TaskAwareRequest
setParentTask
-
Methods inherited from interface org.elasticsearch.action.support.WriteRequest
setRefreshPolicy
-
-
-
-
Field Detail
-
UNSET_AUTO_GENERATED_TIMESTAMP
public static final int UNSET_AUTO_GENERATED_TIMESTAMP
Value forgetAutoGeneratedTimestamp()
if the document has an external provided ID.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IndexRequest
public IndexRequest()
-
IndexRequest
public IndexRequest(java.lang.String index)
Constructs a new index request against the specific index. Thetype(String)
source(byte[], XContentType)
must be set.
-
IndexRequest
public IndexRequest(java.lang.String index, java.lang.String type)
Constructs a new index request against the specific index and type. Thesource(byte[], XContentType)
must be set.
-
IndexRequest
public IndexRequest(java.lang.String index, java.lang.String type, java.lang.String id)
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 Detail
-
validate
public ActionRequestValidationException validate()
- Specified by:
validate
in interfaceWriteRequest<IndexRequest>
- Overrides:
validate
in classReplicationRequest<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
-
type
public java.lang.String type()
The type of the indexed document.- Specified by:
type
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the type
-
type
public IndexRequest type(java.lang.String type)
Sets the type of the indexed document.- Specified by:
type
in interfaceDocWriteRequest<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 interfaceDocWriteRequest<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 interfaceDocWriteRequest<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 interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Routing
-
parent
public IndexRequest parent(java.lang.String parent)
Sets the parent id of this document.
-
parent
public java.lang.String parent()
Description copied from interface:DocWriteRequest
Get the parent for this request- Specified by:
parent
in interfaceDocWriteRequest<IndexRequest>
- Returns:
- the Parent
-
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
-
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 source) throws ElasticsearchGenerationException
Index the Map inRequests.INDEX_CONTENT_TYPE
format- Parameters:
source
- The map to index- Throws:
ElasticsearchGenerationException
-
source
public IndexRequest source(java.util.Map 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(java.lang.String source, 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
public IndexRequest source(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(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, 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 indexoffset
- The offset in the byte arraylength
- 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 theopType(OpType)
. Can be either "index" or "create".
-
create
public IndexRequest create(boolean create)
Set totrue
to force this index to useDocWriteRequest.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 interfaceDocWriteRequest<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 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
public IndexRequest versionType(VersionType versionType)
Description copied from interface:DocWriteRequest
Sets the versioning type. Defaults toVersionType.INTERNAL
.- Specified by:
versionType
in interfaceDocWriteRequest<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 withsetIfPrimaryTerm(long)
If the document last modification was assigned a different sequence number aVersionConflictEngineException
will be thrown.- Specified by:
setIfSeqNo
in interfaceDocWriteRequest<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 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
public VersionType 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, java.lang.String concreteIndex)
-
resolveRouting
public void resolveRouting(MetaData metaData)
-
readFrom
public void readFrom(StreamInput in) throws java.io.IOException
Description copied from interface:Streamable
Set this object's fields from a StreamInput.- Specified by:
readFrom
in interfaceStreamable
- Overrides:
readFrom
in classReplicatedWriteRequest<IndexRequest>
- Throws:
java.io.IOException
-
writeTo
public void writeTo(StreamOutput out) throws java.io.IOException
Description copied from interface:Streamable
Write this object's fields to a StreamOutput.- Specified by:
writeTo
in interfaceStreamable
- Specified by:
writeTo
in interfaceWriteable
- Overrides:
writeTo
in classReplicatedWriteRequest<IndexRequest>
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Specified by:
toString
in classReplicationRequest<IndexRequest>
-
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 -1 if the document has no auto generated timestamp. This method will return a positive value iff the id was auto generated.
-
setShardId
public IndexRequest setShardId(ShardId shardId)
Override this method from ReplicationAction, this is where we are storing our state in the request object (which we really shouldn't do). Once the transport client goes away we can move away from making this available, but in the meantime this is dangerous to set or use because the IndexRequest object will always be wrapped in a bulk request envelope, which is where this *should* be set.- Overrides:
setShardId
in classReplicationRequest<IndexRequest>
-
-