不清楚如何使用python弹性搜索来推荐ElasticSearch [英] Not clear how to upsert ElasticSearch using python elasticsearch

查看:236
本文介绍了不清楚如何使用python弹性搜索来推荐ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里查看一个类似的例子:



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'真的})

但现在想象的目标是附加到数组或增加一个以前的值无需首先获得文档。)



如果您正式提出要求,则位于文档

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

我想知道如何ish附加到数组(只是添加到默认列表)。看起来弹性搜索自己的例子比任何可以专门为Python找到的东西都容易一些。



我之前读过一篇关于人们使用python请求进行弹性搜索的东西,你可以把你的脚本

解决方案

code> c $ c> c code code code code code code code code Elasticsearch

es = Elasticsearch()

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

更多关于更新api



这里的标签是一个数组,我正在追加搜索。您还需要为此启用脚本,或者您可以将脚本放在文件中,并将该文件放在 config / scripts 文件夹中。根据ES版本,语法可能不同。让我知道如果不行。


Look here for a similar example:

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.

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...

解决方案

You can put your script inside the body parameter.

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

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. Syntax might be different depending on ES version. Let me know if it does not work.

这篇关于不清楚如何使用python弹性搜索来推荐ElasticSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆