duanhong8839 2017-06-02 14:32
浏览 135

Elasticsearch:使用无痛脚本获取对象索引

I have a nested type and I need to get the index of a specific object to perform an update:

{
  "_index": "asset_en_v1",
  "_type": "note",
  "_id": "23217",
  "_version": 24,
  "found": true,
  "_source": {
    "user_id": "11",
    "title": "Title",
    "note": "Note.",
    "creation": "2017-05-31T21:36:01",
    "modification": "2017-05-31T21:36:01",
    "links": [
      {
        "note_link_id": "7310",
        "user_id": "11",
        "creation": "2017-06-01T14:41:50",
        "modification": "2019-06-01T14:42:00",
        "comment": "Comment goes here."
      },
      {
        "note_link_id": "7311",
        "user_id": "11",
        "creation": "2017-06-01T14:42:42",
        "modification": "2019-06-01T14:42:00",
        "comment": "Yep..."
      },
      {
        "note_link_id": "7312",
        "user_id": "11",
        "creation": "2017-06-01T15:33:55",
        "modification": "2017-06-01T15:34:00",
        "comment": "Jumo."
      }
    ]
  }
}

So far, I've created an _update statement in Painless script that almost does the job, but I'm struggling to get a match:

{
    "script": {
        "lang": "painless",
        "inline": "def note_link_id = 7311; def links = ctx._source.links; for (int i = 0; i < links.length; ++i) { if (links[i].note_link_id == note_link_id) { ctx._source.links[note_link_id].comment = params.comment; ctx._source.links[note_link_id].modification = params.modification } }",
        "params": {
            "modification": "2019-06-01T14:42:00",
            "comment": "QWERTY!"
        }
    }
}

Here, links[i].note_link_id == note_link_id isn't matching.

Any ideas?

  • 写回答

1条回答 默认 最新

  • doushi1847 2018-01-04 04:54
    关注

    In your inline script, it should have been i++, instead of ++i

    def note_link_id = 7311; 
    def links = ctx._source.links; 
    for (int i = 0; i < links.length; i++) //Note i++, instead of ++i
    { 
        if (links[i].note_link_id == note_link_id) 
        { 
            ctx._source.links[note_link_id].comment = params.comment;
            ctx._source.links[note_link_id].modification = params.modification 
        } 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题