Elasticsearch Обновление значения поля - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь работать с update_by_query, но не могу заставить его работать.

Ниже приведен простой запрос,

curl -X GET "172.17.0.3:9200/useripvsuserid/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "_source":"userid","query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 10.803431,
    "hits" : [
      {
        "_index" : "useripvsuserid",
        "_type" : "_doc",
        "_id" : "PhfBW3AB8mhGfmGvIs-j",
        "_score" : 10.803431,
        "_source" : {
          "userid" : "hasan1855"
        }
      }
    ]
  }
}

Ниже приводится update_by_query, который не работает. Я пытаюсь заменить userid значение hasan1855 на arif. Где проблема?

curl -X POST "172.17.0.3:9200/useripvsuserid/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {                                                                                                                                                                                                        
    "source": "ctx._source.userid='arif';",
    "lang": "painless"       
  }, 
  "query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "ctx._source.userid=arif;",
          "                   ^---- HERE"
        ],
        "script" : "ctx._source.userid=arif;",
        "lang" : "painless"
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "ctx._source.userid=arif;",
      "                   ^---- HERE"
    ],
    "script" : "ctx._source.userid=arif;",
    "lang" : "painless",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Variable [arif] is not defined."
    }
  },
  "status" : 400
}

1 Ответ

1 голос
/ 19 февраля 2020

Это та же проблема, что и описанная здесь , то есть одинарные кавычки вокруг arif конфликтуют с одинарными кавычками вокруг JSON запроса.

Так что вы можете отправить свой запрос в двоичном режиме, как описано в ссылке выше, или экранируйте кавычки, например:

curl -X POST "172.17.0.3:9200/useripvsuserid/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {                                                                                                                                                                                                        
    "source": "ctx._source.userid = \"arif\";",         <---- escape quotes
    "lang": "painless"       
  }, 
  "query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...