如何使用python Elasticsearch客户端upsert api
我使用Elasticsearch python客户端作为如何使用python Elasticsearch客户端upsert api,python,
elasticsearch,upsert,Python,
elasticsearch,Upsert,我使用Elasticsearch python客户端作为 我努力了,但仍然找不到upsert的更新api。请给我一个ES python客户端upsert api的示例。示例代码如下: from elasticsearch import Elasticsearch es = Elasticsearch("localhost:9200") es.update(index='test',doc_type='test1',id='1',body={'doc':{'username':'Tom'},'do
我努力了,但仍然找不到upsert的更新api。请给我一个ES python客户端upsert api的示例。示例代码如下:
from elasticsearch import Elasticsearch
es = Elasticsearch("localhost:9200")
es.update(index='test',doc_type='test1',id='1',body={'doc':{'username':'Tom'},'doc_as_upsert':True})
如果没有doc\u as\u upsert=true
,则当id不存在时将引发异常。此外,请确保您的数据已包装在文档{}中 方法index(*args,**kwargs)
在特定索引中添加或更新类型化的JSON文档,使其可搜索
正如。>中指出的,另外,请确保您的数据已包装在文档{}中。感谢you@LelouchLamperouge是否必须使用
doc
?如何根据条件进行升级?因此,对于传递包装在doc中的正文,有没有一种方法可以传递它?似乎它必须是一个dict,所以必须首先将json解析为dict,但我已经有了json,希望跳过一步。我知道我可以使用请求
库并将整个请求解析为json。太糟糕了,它不像助手那样灵活。bulk
这两种方法都需要。我想我也可以使用bulk和insert 1.0为我工作。不确定此解决方案与已接受的解决方案(es.update)之间的区别。这两种方法对我都很有效。索引重写了整个文档。例如,如果您有{“key”:“value”}
,然后您在同一个文档id上使用索引和{“key1”:“value1”}
该文档将只有{“key1”:“value1”}
,而使用update时您实际上有{“key”:“value”,“key1”:“value1”}