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 intUNSET_AUTO_GENERATED_TIMESTAMPValue 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 IndexRequestcreate(boolean create)Set totrueto force this index to useDocWriteRequest.OpType.CREATE.longgetAutoGeneratedTimestamp()Returns the timestamp the auto generated ID was created or -1 if the document has no auto generated timestamp.XContentTypegetContentType()The content type.java.lang.StringgetPipeline()Returns the ingest pipeline to be executed before indexing the documentjava.lang.Stringid()The id of the indexed document.IndexRequestid(java.lang.String id)Sets the id of the indexed document.longifPrimaryTerm()If set, only perform this indexing request if the document was last modification was assigned this primary term.longifSeqNo()If set, only perform this indexing request if the document was last modification was assigned this sequence number.booleanisRetry()Returnstrueif this request has been sent to a shard copy more than once.voidonRetry()This method is called before this replication request is retried the first time.DocWriteRequest.OpTypeopType()Get the requested document operation type of the requestIndexRequestopType(java.lang.String opType)Sets a string representation of theopType(OpType).IndexRequestopType(DocWriteRequest.OpType opType)Sets the type of operation to perform.java.lang.Stringparent()Get the parent for this requestIndexRequestparent(java.lang.String parent)Sets the parent id of this document.voidprocess(Version indexCreatedVersion, MappingMetaData mappingMd, java.lang.String concreteIndex)voidreadFrom(StreamInput in)Set this object's fields from a StreamInput.voidresolveRouting(MetaData metaData)java.lang.Stringrouting()Controls the shard routing of the request.IndexRequestrouting(java.lang.String routing)Controls the shard routing of the request.IndexRequestsetIfPrimaryTerm(long term)only performs this indexing request if the document was last modification was assigned the given primary term.IndexRequestsetIfSeqNo(long seqNo)only perform this indexing request if the document was last modification was assigned the given sequence number.IndexRequestsetPipeline(java.lang.String pipeline)Sets the ingest pipeline to be executed before indexing the documentIndexRequestsetShardId(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).BytesReferencesource()The source of the document to index, recopied to a new array if it is unsafe.IndexRequestsource(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).IndexRequestsource(byte[] source, XContentType xContentType)Sets the document to index in bytes form.IndexRequestsource(java.lang.Object... source)Sets the content source to index using the default content type (Requests.INDEX_CONTENT_TYPE)IndexRequestsource(java.lang.String source, XContentType xContentType)Sets the document source to index.IndexRequestsource(java.util.Map source)Index the Map inRequests.INDEX_CONTENT_TYPEformatIndexRequestsource(java.util.Map source, XContentType contentType)Index the Map as the provided content type.IndexRequestsource(BytesReference source, XContentType xContentType)Sets the document to index in bytes form.IndexRequestsource(XContentBuilder sourceBuilder)Sets the content source to index.IndexRequestsource(XContentType xContentType, java.lang.Object... source)Sets the content source to index.java.util.Map<java.lang.String,java.lang.Object>sourceAsMap()java.lang.StringtoString()java.lang.Stringtype()The type of the indexed document.IndexRequesttype(java.lang.String type)Sets the type of the indexed document.ActionRequestValidationExceptionvalidate()longversion()Returns stored version.IndexRequestversion(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.VersionTypeversionType()Get the document version type for this requestIndexRequestversionType(VersionType versionType)Sets the versioning type.voidwriteTo(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:
validatein interfaceWriteRequest<IndexRequest>- Overrides:
validatein 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:
typein interfaceDocWriteRequest<IndexRequest>- Returns:
- the type
-
type
public IndexRequest type(java.lang.String type)
Sets the type of the indexed document.- Specified by:
typein 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:
idin 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:
routingin 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:
routingin 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:DocWriteRequestGet the parent for this request- Specified by:
parentin 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_TYPEformat- 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 totrueto force this index to useDocWriteRequest.OpType.CREATE.
-
opType
public DocWriteRequest.OpType opType()
Description copied from interface:DocWriteRequestGet the requested document operation type of the request- Specified by:
opTypein interfaceDocWriteRequest<IndexRequest>- Returns:
- the operation type
DocWriteRequest.OpType
-
version
public IndexRequest version(long version)
Description copied from interface:DocWriteRequestSets the version, which will perform the operation only if a matching version exists and no changes happened on the doc since then.- Specified by:
versionin interfaceDocWriteRequest<IndexRequest>
-
version
public long version()
Returns stored version. If currently stored version isVersions.MATCH_ANYand opType isDocWriteRequest.OpType.CREATE, returnsVersions.MATCH_DELETED.- Specified by:
versionin interfaceDocWriteRequest<IndexRequest>- Returns:
- the document version
-
versionType
public IndexRequest versionType(VersionType versionType)
Description copied from interface:DocWriteRequestSets the versioning type. Defaults toVersionType.INTERNAL.- Specified by:
versionTypein 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 aVersionConflictEngineExceptionwill be thrown.- Specified by:
setIfSeqNoin 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 aVersionConflictEngineExceptionwill be thrown.- Specified by:
setIfPrimaryTermin 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 aVersionConflictEngineExceptionwill be thrown.- Specified by:
ifSeqNoin 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 aVersionConflictEngineExceptionwill be thrown.- Specified by:
ifPrimaryTermin interfaceDocWriteRequest<IndexRequest>
-
versionType
public VersionType versionType()
Description copied from interface:DocWriteRequestGet the document version type for this request- Specified by:
versionTypein 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:StreamableSet this object's fields from a StreamInput.- Specified by:
readFromin interfaceStreamable- Overrides:
readFromin classReplicatedWriteRequest<IndexRequest>- Throws:
java.io.IOException
-
writeTo
public void writeTo(StreamOutput out) throws java.io.IOException
Description copied from interface:StreamableWrite this object's fields to a StreamOutput.- Specified by:
writeToin interfaceStreamable- Specified by:
writeToin interfaceWriteable- Overrides:
writeToin classReplicatedWriteRequest<IndexRequest>- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Specified by:
toStringin classReplicationRequest<IndexRequest>
-
isRetry
public boolean isRetry()
Returnstrueif this request has been sent to a shard copy more than once.
-
onRetry
public void onRetry()
Description copied from class:ReplicationRequestThis method is called before this replication request is retried the first time.- Overrides:
onRetryin 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:
setShardIdin classReplicationRequest<IndexRequest>
-
-