Elasticsearch的script使用
Posted 程序员超时空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch的script使用相关的知识,希望对你有一定的参考价值。
从ES 1.4.3以后, inline script默认是被禁止的,但是可以使用file script的模式。
"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"
]
]
以上是关于Elasticsearch的script使用的主要内容,如果未能解决你的问题,请参考以下文章