Elasticsearch的script使用

来源:互联网 发布:元数据和数据元区别 编辑:程序博客网 时间:2024/06/05 06:46
</pre><p class="p1"><span class="s1">从ES 1.4.3以后, inline script默认是被禁止的,但是可以使用file script的模式。</span></p><p class="p1"><span class="s1"></span></p><pre code_snippet_id="1661228" snippet_file_name="blog_20160425_2_5419351" name="code" class="python">{"query":{    "term": {      "content": "中国"    }  },  "script_fields": {    "test1": {      "script": "doc['content']+'hello'"    }  }}

会出现下面的错误日志:

nested: ScriptException[scripts of type [inline], operation [search] and lang [groovy] are disabled


要打开, 需要在config/elasticsearch.yml中添加如下配置:

script.inline:true

script.indexed:true

重启ES后即可生效了。得到如下输出:

另外, 需要注意的是, 如果有多个node, 必须在每个node的elasticsearch.yml中, 都加入上述配置, 否则, script不能使用。

{ "took": 479, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, 

 "hits": { "total": 2, "max_score": 1.5, "hits": [ 

 { "_index": "testik", "_type": "typeik", "_id": "1", "_score": 1.5,

   "fields": { "test1": ["中国", "亚", "亚裔", "击", "嫌", "嫌犯", "子枪", "已", "杉", "枪", "枪击", "洛", "洛杉矶", "犯", "男子", "矶", "自首", "裔", 

"遭", "领事", "领事馆", "馆", "驻", "hello"] } 

  }, 

{ "_index": "testik", "_type": "typeik", "_id": "3", "_score": 0.53699243, 

 "fields": { "test1": ["1", "中国", "中韩", "冲", "冲突", "均", "平均", "扣", "每天", "渔", "渔船", "突", "船", "艘", "警", "调查", "韩", "hello"] } 

 } ] } }

同时也可以看出, doc[‘field’]得到的analyzed之后的分词结果,例如doc[‘field’][1]可得到“亚”和“中国”。若是not_analyzed,则可得到原值

而_source.field可以得到任意的部分, 无论分词与否。

{  "query":{    "term": {        "content": "中国"    }  },   "script_fields": {    "test1": {      "script": "_source.content+'hello'"    }  }}

得到的结果如下:

{  "took": 7,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 2,    "max_score": 1.5,    "hits": [      {        "_index": "testik",        "_type": "typeik",        "_id": "1",        "_score": 1.5,        "fields": {          "test1": [            "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首hello"          ]        }      },      {        "_index": "testik",        "_type": "typeik",        "_id": "3",        "_score": 0.53699243,        "fields": {          "test1": [            "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船hello"          ]        }      }    ]  }}

1 0
原创粉丝点击