簡體   English   中英

用python進行彈性搜索部分更新

[英]elasticsearch partial update with python

我有一個彈性搜索文檔,格式如下。 我需要部分更新“x”字段並在其中添加python dict。

{
        "_index": "gdata34",
        "_type": "gdat",
        "_id": "328091-72341-118",
        "_version": 1,
        "_score": 1,
        "_source": {
            "d": {
                "Thursday": {
                    "s": ""
                },
                "Email": {
                    "s": ""
                },
               "Country": {
                    "s": "US"
                },

            },
            "x": {
                "Geo": {
                    "s": "45.335428,-118.057133",
                    "g": [
                        -118.057133
                        ,
                        45.335428
                    ]
                }
            },
          }
        }

我嘗試了以下代碼來更新:

from elasticsearch import Elasticsearch, exceptions
import pprint


elasticsearch = Elasticsearch()
doc = elasticsearch.get(index='gdata34', doc_type='gdat', id='328091-72341-7')

elasticsearch.update(index='gdata34', doc_type='gdat', id='328091-72341-7',
                     body={"script":"ctx._source.x += y",
                           "params":{"y":"z"}
                     }
                     )
elasticsearch.indices.refresh(index='gdata34')
new_doc = elasticsearch.get(index='gdata34', doc_type='gdat', id='328091-72341-7')

我收到此錯誤:

elasticsearch.exceptions.RequestError: TransportError(400, u'ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [groovy] disabled]; ')

使用python在elasticsearch中進行部分更新的正確方法是什么?

為了將來參考,以下部分更新方法有效。

elasticsearch.update(index='gdata34', doc_type='gdat', id='328091-72341-7',
                     body={
                         'doc': {'x': {'y':'z'}}
                     }
                     )

來自腳本編寫ElasticSearch文檔

我們建議在應用程序或代理后面運行Elasticsearch,以保護Elasticsearch免受外界影響。 如果允許用戶運行動態腳本(即使在搜索請求中),則他們對運行Elasticsearch的用戶具有相同的訪問權限。 因此,默認情況下,僅允許沙盒語言使用動態腳本。

現在,在最近的ES版本中,Groovy腳本引擎中的漏洞存在一個漏洞,它允許腳本在運行Elasticsearch Java VM時逃離沙箱並執行shell命令 - 這就是為什么最近默認禁用Groovy沙箱的原因版本 ,因此執行在請求正文或.scripts索引中傳遞的Groovy腳本。 使用此默認配置執行Groovy腳本的唯一方法是將它們放在節點上的config/scripts/目錄中。

所以你有兩個選擇:

  • 如果您的ES實例無法直接訪問並且在代理后面受到保護,則可以通過在節點上的config/elasticsearch.yml script.groovy.sandbox.enabled: true設置script.groovy.sandbox.enabled: true來再次打開Groovy沙盒。 如果您的ES實例可由您訪問
  • 您可以准備腳本並將其放在節點的config/scripts目錄中的文件系統上,並按名稱調用它。 有關詳細信息,請參閱運行不帶動態腳本的Groovy腳本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM