4

I want to add doc in elastic search using python elasticsearch but in the example on the documentation i have this code and on this example is specify the id , i dont want to specify the id, I want elastic to generate id for me like this for example AK3286826fds83

def addBrandInES():

    doc = {
        'author': 'kimchy',
        'text': 'Elasticsearch: cool. bonsai cool.',
        'timestamp': datetime.now(),
    }

    # res = es.index(index="brands", doc_type='external', id=1, body=doc)
    res = es.index(index="brands", doc_type='external', body=doc) <-- can i do that ??
    print(res['created'])
2
  • yes you can. I will auto-generate an id for that doc. Commented Sep 7, 2017 at 12:57
  • you can also send it using requests module. Commented Sep 7, 2017 at 12:58

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

es.index() is legit
-2
res = es.index(index="brands", doc_type='external', body=doc , id=<your id>)

2 Comments

Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar or using Ctrl+K on your keyboard to nicely format and syntax highlight it!
The question asks not to specify the id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.