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"
          ]
        }
      }
    ]
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值