如何在elasticsearch中添加doc而不用python指定id

时间:2017-09-07 12:55:43

标签: python elasticsearch

我想使用python elasticsearch在弹性搜索中添加doc但是在documentation的示例中我有这个代码,在这个例子中是指定id,我不想指定id,我想要弹性到像这样为我生成id,例如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 个答案:

答案 0 :(得分:2)

是的,您可以简单地省略id参数。缺少参数时,Elasticsearch将为该文档创建一个参数。以下代码段来自elasticsearch-py index方法:

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)

注意第二行到最后一行:SKIP_IN_PATH定义为:

SKIP_IN_PATH = (None, '', b'', [], ())

因此,如果缺少id,将使用HTTP'POST',这将创建一个新对象,否则将使用'PUT',即更新现有文档。

还有另一个名为create()的API,需要设置id。此API专门用于创建具有指定标识的文档。

答案 1 :(得分:0)

res = es.index(index =“brands”,doc_type ='external',body = doc,id =)