Yes, you can simply omit the id parameter. When the parameter is missing, Elasticsearch will create one for that document. The following snippet is from the elasticsearch-py index method:
def index(self, index, doc_type, body, id=None, params=None):
"""
Adds or updates a typed JSON document in a specific index, making it searchable.
`<http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html>`_
:arg index: The name of the index
:arg doc_type: The type of the document
:arg body: The document
:arg id: Document ID
:arg op_type: Explicit operation type, default 'index', valid choices
are: 'index', 'create'
:arg parent: ID of the parent document
:arg pipeline: The pipeline id to preprocess incoming documents with
:arg refresh: If `true` then refresh the affected shards to make this
operation visible to search, if `wait_for` then wait for a refresh
to make this operation visible to search, if `false` (the default)
then do nothing with refreshes., valid choices are: u'true',
u'false', u'wait_for'
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg timestamp: Explicit timestamp for the document
:arg ttl: Expiration time for the document
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type, valid choices are: 'internal',
'external', 'external_gte', 'force'
:arg wait_for_active_shards: Sets the number of shard copies that must
be active before proceeding with the index operation. Defaults to 1,
meaning the primary shard only. Set to `all` for all shard copies,
otherwise set to any non-negative value less than or equal to the
total number of copies for the shard (number of replicas + 1)
"""
for param in (index, doc_type, body):
if param in SKIP_IN_PATH:
raise ValueError("Empty value passed for a required argument.")
return self.transport.perform_request('POST' if id in SKIP_IN_PATH else 'PUT',
_make_path(index, doc_type, id), params=params, body=body)
Note the second to the last line: the SKIP_IN_PATH is defined as:
SKIP_IN_PATH = (None, '', b'', [], ())
So if id is missing, HTTP 'POST' will be used, which is creating a new object, otherwise 'PUT' will be used, which is to update an existing document.
There is another API named create(), which requires the id to be set. This API is used specifically to create a document with a specified id.