org.elasticsearch.action.index
Class IndexRequest

java.lang.Object
  extended by org.elasticsearch.action.support.replication.ShardReplicationOperationRequest
      extended by org.elasticsearch.action.index.IndexRequest
All Implemented Interfaces:
ActionRequest, Streamable

public class IndexRequest
extends ShardReplicationOperationRequest

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 ShardReplicationOperationRequest.index(), type(String), id(String) and source(byte[]) to be set.

The source (content to index) can be set in its bytes form using (source(byte[])), its string form (source(String)) 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)

Nested Class Summary
static class IndexRequest.OpType
          Operation type controls if the type of the index operation.
 
Field Summary
 
Fields inherited from class org.elasticsearch.action.support.replication.ShardReplicationOperationRequest
DEFAULT_TIMEOUT, index, timeout
 
Constructor Summary
IndexRequest()
           
IndexRequest(String index)
          Constructs a new index request against the specific index.
IndexRequest(String index, String type, String id)
          Constructs a new index request against the index, type, id and using the source.
 
Method Summary
 void beforeLocalFork()
          Before we fork on a local thread, make sure we copy over the bytes if they are unsafe
 IndexRequest consistencyLevel(WriteConsistencyLevel consistencyLevel)
          Sets the consistency level of write.
 IndexRequest contentType(XContentType contentType)
          Sets the content type that will be used when generating a document from user provided objects (like Map).
 IndexRequest create(boolean create)
          Set to true to force this index to use IndexRequest.OpType.CREATE.
 String id()
          The id of the indexed document.
 IndexRequest id(String id)
          Sets the id of the indexed document.
 IndexRequest index(String index)
          Sets the index the index operation will happen on.
 IndexRequest listenerThreaded(boolean threadedListener)
          Should the listener be called on a separate thread if needed.
 IndexRequest operationThreaded(boolean threadedOperation)
          Controls if the operation will be executed on a separate thread when executed locally.
 IndexRequest.OpType opType()
          The type of operation to perform.
 IndexRequest opType(IndexRequest.OpType opType)
          Sets the type of operation to perform.
 IndexRequest opType(String opType)
          Sets a string representation of the opType(org.elasticsearch.action.index.IndexRequest.OpType).
 String parent()
           
 IndexRequest parent(String parent)
          Sets the parent id of this document.
 String percolate()
           
 IndexRequest percolate(String percolate)
          Causes the index request document to be percolated.
 void process(MetaData metaData, String aliasOrIndex, MappingMetaData mappingMd, boolean allowIdGeneration)
           
 void readFrom(StreamInput in)
           
 boolean refresh()
           
 IndexRequest refresh(boolean refresh)
          Should a refresh be executed post this index operation causing the operation to be searchable.
 IndexRequest replicationType(ReplicationType replicationType)
          Set the replication type for this operation.
 IndexRequest replicationType(String replicationType)
          Set the replication type for this operation.
 String routing()
          Controls the shard routing of the request.
 IndexRequest routing(String routing)
          Controls the shard routing of the request.
 BytesReference safeSource()
           
 BytesReference source()
          The source of the document to index, recopied to a new array if it is unsage.
 IndexRequest source(byte[] source)
          Sets the document to index in bytes form.
 IndexRequest source(byte[] source, int offset, int length)
          Sets the document to index in bytes form (assumed to be safe to be used from different threads).
 IndexRequest source(byte[] source, int offset, int length, boolean unsafe)
          Sets the document to index in bytes form.
 IndexRequest source(BytesReference source, boolean unsafe)
          Sets the document to index in bytes form.
 IndexRequest source(Map source)
          Index the Map as a Requests.INDEX_CONTENT_TYPE.
 IndexRequest source(Map source, XContentType contentType)
          Index the Map as the provided content type.
 IndexRequest source(String source)
          Sets the document source to index.
 IndexRequest source(String field1, Object value1)
           
 IndexRequest source(String field1, Object value1, String field2, Object value2)
           
 IndexRequest source(String field1, Object value1, String field2, Object value2, String field3, Object value3)
           
 IndexRequest source(String field1, Object value1, String field2, Object value2, String field3, Object value3, String field4, Object value4)
           
 IndexRequest source(XContentBuilder sourceBuilder)
          Sets the content source to index.
 Map<String,Object> sourceAsMap()
           
 IndexRequest timeout(String timeout)
          A timeout to wait if the index operation can't be performed immediately.
 IndexRequest timeout(TimeValue timeout)
          A timeout to wait if the index operation can't be performed immediately.
 String timestamp()
           
 IndexRequest timestamp(String timestamp)
          Sets the timestamp either as millis since the epoch, or, in the configured date format.
 String toString()
           
 long ttl()
           
 IndexRequest ttl(Long ttl)
          Sets the relative ttl value.
 String type()
          The type of the indexed document.
 IndexRequest type(String type)
          Sets the type of the indexed document.
 ActionRequestValidationException validate()
           
 long version()
           
 IndexRequest version(long version)
          Sets the version, which will cause the index operation to only be performed if a matching version exists and no changes happened on the doc since then.
 VersionType versionType()
           
 IndexRequest versionType(VersionType versionType)
          Sets the versioning type.
 void writeTo(StreamOutput out)
           
 
Methods inherited from class org.elasticsearch.action.support.replication.ShardReplicationOperationRequest
consistencyLevel, index, listenerThreaded, operationThreaded, replicationType, timeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndexRequest

public IndexRequest()

IndexRequest

public IndexRequest(String index)
Constructs a new index request against the specific index. The type(String), id(String) and source(byte[]) must be set.


IndexRequest

public IndexRequest(String index,
                    String type,
                    String id)
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 Detail

validate

public ActionRequestValidationException validate()
Specified by:
validate in interface ActionRequest
Overrides:
validate in class ShardReplicationOperationRequest

beforeLocalFork

public void beforeLocalFork()
Before we fork on a local thread, make sure we copy over the bytes if they are unsafe

Overrides:
beforeLocalFork in class ShardReplicationOperationRequest

index

public IndexRequest index(String index)
Sets the index the index operation will happen on.

Overrides:
index in class ShardReplicationOperationRequest

contentType

public IndexRequest contentType(XContentType contentType)
Sets the content type that will be used when generating a document from user provided objects (like Map).


listenerThreaded

public IndexRequest listenerThreaded(boolean threadedListener)
Should the listener be called on a separate thread if needed.

Specified by:
listenerThreaded in interface ActionRequest
Overrides:
listenerThreaded in class ShardReplicationOperationRequest

operationThreaded

public IndexRequest operationThreaded(boolean threadedOperation)
Controls if the operation will be executed on a separate thread when executed locally. Defaults to true when running in embedded mode.

Overrides:
operationThreaded in class ShardReplicationOperationRequest

type

public String type()
The type of the indexed document.


type

@Required
public IndexRequest type(String type)
Sets the type of the indexed document.


id

public String id()
The id of the indexed document. If not set, will be automatically generated.


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.


routing

public String routing()
Controls the shard routing of the request. Using this value to hash the shard and not the id.


parent

public IndexRequest parent(String parent)
Sets the parent id of this document. If routing is not set, automatically set it as the routing as well.


parent

public String parent()

timestamp

public IndexRequest timestamp(String timestamp)
Sets the timestamp either as millis since the epoch, or, in the configured date format.


timestamp

public String timestamp()

ttl

public IndexRequest ttl(Long ttl)
                 throws ElasticSearchGenerationException
Sets the relative ttl value. It musts be > 0 as it makes little sense otherwise. Setting it to null will reset to have no ttl.

Throws:
ElasticSearchGenerationException

ttl

public long ttl()

source

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


safeSource

public BytesReference safeSource()

sourceAsMap

public Map<String,Object> sourceAsMap()

source

@Required
public IndexRequest source(Map source)
                    throws ElasticSearchGenerationException
Index the Map as a Requests.INDEX_CONTENT_TYPE.

Parameters:
source - The map to index
Throws:
ElasticSearchGenerationException

source

@Required
public IndexRequest source(Map source,
                                    XContentType contentType)
                    throws ElasticSearchGenerationException
Index the Map as the provided content type.

Parameters:
source - The map to index
Throws:
ElasticSearchGenerationException

source

@Required
public IndexRequest source(String source)
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[]).


source

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


source

@Required
public IndexRequest source(String field1,
                                    Object value1)

source

@Required
public IndexRequest source(String field1,
                                    Object value1,
                                    String field2,
                                    Object value2)

source

@Required
public IndexRequest source(String field1,
                                    Object value1,
                                    String field2,
                                    Object value2,
                                    String field3,
                                    Object value3)

source

@Required
public IndexRequest source(String field1,
                                    Object value1,
                                    String field2,
                                    Object value2,
                                    String field3,
                                    Object value3,
                                    String field4,
                                    Object value4)

source

public IndexRequest source(BytesReference source,
                           boolean unsafe)
Sets the document to index in bytes form.


source

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


source

@Required
public IndexRequest source(byte[] source,
                                    int offset,
                                    int length)
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

source

@Required
public IndexRequest source(byte[] source,
                                    int offset,
                                    int length,
                                    boolean unsafe)
Sets the document to index in bytes form.

Parameters:
source - The source to index
offset - The offset in the byte array
length - The length of the data
unsafe - Is the byte array safe to be used form a different thread

timeout

public IndexRequest timeout(TimeValue timeout)
A timeout to wait if the index operation can't be performed immediately. Defaults to 1m.


timeout

public IndexRequest timeout(String timeout)
A timeout to wait if the index operation can't be performed immediately. Defaults to 1m.


opType

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


opType

public IndexRequest opType(String opType)
                    throws ElasticSearchIllegalArgumentException
Sets a string representation of the opType(org.elasticsearch.action.index.IndexRequest.OpType). Can be either "index" or "create".

Throws:
ElasticSearchIllegalArgumentException

replicationType

public IndexRequest replicationType(ReplicationType replicationType)
Set the replication type for this operation.

Overrides:
replicationType in class ShardReplicationOperationRequest

consistencyLevel

public IndexRequest consistencyLevel(WriteConsistencyLevel consistencyLevel)
Sets the consistency level of write. Defaults to WriteConsistencyLevel.DEFAULT

Overrides:
consistencyLevel in class ShardReplicationOperationRequest

replicationType

public IndexRequest replicationType(String replicationType)
Set the replication type for this operation.


create

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


opType

public IndexRequest.OpType opType()
The type of operation to perform.


refresh

public IndexRequest refresh(boolean refresh)
Should a refresh be executed post this index operation causing the operation to be searchable. Note, heavy indexing should not set this to true. Defaults to false.


refresh

public boolean refresh()

version

public IndexRequest version(long version)
Sets the version, which will cause the index operation to only be performed if a matching version exists and no changes happened on the doc since then.


version

public long version()

versionType

public IndexRequest versionType(VersionType versionType)
Sets the versioning type. Defaults to VersionType.INTERNAL.


versionType

public VersionType versionType()

percolate

public IndexRequest percolate(String percolate)
Causes the index request document to be percolated. The parameter is the percolate query to use to reduce the percolated queries that are going to run against this doc. Can be set to * to indicate that all percolate queries should be run.


percolate

public String percolate()

process

public void process(MetaData metaData,
                    String aliasOrIndex,
                    @Nullable
                    MappingMetaData mappingMd,
                    boolean allowIdGeneration)
             throws ElasticSearchException
Throws:
ElasticSearchException

readFrom

public void readFrom(StreamInput in)
              throws IOException
Specified by:
readFrom in interface Streamable
Overrides:
readFrom in class ShardReplicationOperationRequest
Throws:
IOException

writeTo

public void writeTo(StreamOutput out)
             throws IOException
Specified by:
writeTo in interface Streamable
Overrides:
writeTo in class ShardReplicationOperationRequest
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2009-2012. All Rights Reserved.