【问题标题】:Trying to update a nested geoip location field in elasticsearch尝试更新 elasticsearch 中的嵌套 geoip 位置字段
【发布时间】:2020-04-30 15:57:26
【问题描述】:

这是我尝试过的:

POST orders/_update_by_query
{
    "script" : "ctx._source.geoip += newElement",
    "params": {
        "newElement": {
           "location" : "[40.730610, -73.935242]"
        }
    },
  "query": {
    "term": {
      "CITY": {
        "value": "nyc"
      }
    }
  }
}

上面会抛出错误[params] 中 START_OBJECT 的未知键。

第二次尝试:

POST orders/_update_by_query
{
  "script":{
    "source":
      "for (item in ctx._source.geoip){item.location = '[40.730610, -73.935242]'}",
      "lang":"painless"
  },
  "query": {
    "term": {
      "CITY": {
        "value": "nyc"
      }
    }
  }
}

上面抛出空指针异常,指向source.geoip处的句点

我也尝试将 location 的值更改为只是 test 但收到相同的错误。

这是我的映射:

{
  "orders" : {
    "mappings" : {
      "properties" : {
        "geoip" : {
          "dynamic" : "true",
          "properties" : {
            "location" : {
              "type" : "geo_point"
            }
          }
        }
     }
}

我正在使用 ES v7.2 和 Kibana v7.2

【问题讨论】:

    标签: elasticsearch geolocation kibana elasticsearch-painless


    【解决方案1】:

    第一种方法的几个问题:

    • params 需要在脚本对象中定义,而不是在它下面
    • newElement 需要使用 params.newElement 访问
    • 您不能将+= params.newElement 附加到不存在的ctx._source.geoip
    • 您不能将对象附加到单值字段 - 您可以分配它
    • location 属于 geo_point 类型,所以要么是 [40.730610, -73.935242] ([lon, lat]) 要么是 "-73.935242,40.730610" ("lat,lon"),但不是两者的混合

    工作指令:

    POST orders/_update_by_query
    {
      "script": {
        "inline": "ctx._source.geoip = params.newElement",
        "params": {
          "newElement": {
            "location": [
              40.73061,
              -73.935242
            ]
          }
        }
      },
      "query": {
        "term": {
          "CITY": {
            "value": "nyc"
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2021-05-16
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多