简体   繁体   English

不清楚如何使用python elasticsearch upsert ElasticSearch

[英]Not clear how to upsert ElasticSearch using python elasticsearch

Look here for a similar example: 在这里查看类似的示例:

https://stackoverflow.com/a/33247409/1575066 https://stackoverflow.com/a/33247409/1575066

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})

But now imagine the goal is to append to an array or to increment a previous value (without having to get the document first). 但现在想象的目标是附加到数组或增加先前的值(无需先获取文档)。

If you go with official requests, this is in the documentation : 如果您提出正式请求,请参阅文档

POST /website/pageviews/1/_update
{
   "script" : "ctx._source.views+=1",
   "upsert": {
       "views": 1
   }
}

I was wondering how to accomplish appending to an array (just simply adding to a default list). 我想知道如何完成附加到数组(只是简单地添加到默认列表)。 It appeared elasticsearch's own examples are easier than anything I can find specifically for Python. 看来elasticsearch自己的例子比我专门为Python找到的任何东西都容易。

I read a post before about people just using python requests for doing elasticsearch stuff, and I'm thinking there might be a point to it... 我之前读过一篇关于人们只是使用python请求做弹性搜索的东西的帖子,我想可能有一点要点......

You can put your script inside the body parameter. 您可以将script放在body参数中。

from elasticsearch import Elasticsearch

es = Elasticsearch()

es.update(
    index="test",
    doc_type="test1",
    id="1",
    body={"script": "ctx._source.tags+=new_tag",
          "params": {
             "new_tag" : "search"
            }
          }
    )

More on update api 有关更新api的更多信息

Here tags is an array and I am appending search to it. 这里的标签是一个数组,我正在追加搜索 Also you need to enable scripting for this or you can put script in a file and put that file inside config/scripts folder. 您还需要为此启用脚本,或者您可以将脚本放在文件中并将该文件放在config/scripts文件夹中。 Syntax might be different depending on ES version. 语法可能因ES版本而异。 Let me know if it does not work. 如果它不起作用,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM