如何在elasticsearch中添加文档而不使用python指定id
我想使用python elasticsearch在elastic搜索中添加文档,但在示例中,我有这段代码,在这个示例中是指定id,我不想指定id,我希望elastic像这样为我生成id,例如AK3286826fds83如何在elasticsearch中添加文档而不使用python指定id,python,elasticsearch,Python,elasticsearch,我想使用python elasticsearch在elastic搜索中添加文档,但在示例中,我有这段代码,在这个示例中是指定id,我不想指定id,我希望elastic像这样为我生成id,例如AK3286826fds83 def addBrandInES(): doc = { 'author': 'kimchy', 'text': 'Elasticsearch: cool. bonsai cool.', 'timestamp': datet
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'])
def addBrandInES():
文件={
“作者”:“金奇”,
“文本”:“Elasticsearch:酷。盆景酷。”,
“时间戳”:datetime.now(),
}
#res=es.index(index=“brands”,doc_type='external',id=1,body=doc)
res=es.index(index=“brands”,doc\u type='external',body=doc)res=es.index(index=“brands”,doc\u type='external',body=doc,id=)
是的,您可以省略id
参数。当参数丢失时,Elasticsearch将为该文档创建一个参数。以下代码段来自elasticsearch pyindex
方法:
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)
因此,如果缺少id
,将使用HTTP“POST”创建新对象,否则将使用“PUT”更新现有文档
还有另一个API名为create()
,它需要设置id
。此API专门用于创建具有指定id的文档。可以。我将自动生成该文档的id。您也可以使用“请求”模块发送该文档。欢迎使用StackOverflow:如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,然后单击编辑器工具栏上的“代码示例”按钮({}),或者使用键盘上的Ctrl+K将其格式化并语法突出显示!es.index()是合法的
SKIP_IN_PATH = (None, '', b'', [], ())