更新elasticsearch文档的特定字段

时间:2017-05-01 07:33:05

标签: elasticsearch

您好我正在尝试更新符合特定条件的弹性搜索文档。我正在使用google sense(chrome扩展名)来提出请求。我正在提出的请求如下所示:

GET styling_rules2/product_line_filters/_update
{
  "query": {
    "filtered": {
      "query":  {
    "bool": {
      "should": [
          {"term":{"product_line_attribute": "brand"}} 
      ],
      "minimum_should_match": 1
    }
  },
      "filter": {
        "term": {
          "product_line_name": "women_skirts"
        }
      }
    }
  },
  "script" : "ctx._source.brand=brands"
}

示例文档如下所示:

{
               "product_line_attribute_db_path": "product_filter.brand",
               "product_line_attribute": "brand",
               "product_line_name": "women_skirts",
               "product_line_attribute_value_list": [
                  "vero moda",
                  "faballey",
                  "only",
                  "rider republic",
                  "dorothy perkins"
               ]
}

所需结果:将所有包含product_line_attribute="brand"product_line_name="women_skirts"的文档更新为product_line_attribute="brands"

问题:我收到的错误如下:

{
   "error": {
      "root_cause": [
         {
            "type": "search_parse_exception",
            "reason": "failed to parse search source. unknown search element [script]",
            "line": 18,
            "col": 4
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "styling_rules2",
            "node": "2ijp1pXwT46FN4on4-JPlg",
            "reason": {
               "type": "search_parse_exception",
               "reason": "failed to parse search source. unknown search element [script]",
               "line": 18,
               "col": 4
            }
         }
      ]
   },
   "status": 400
}

提前感谢!

1 个答案:

答案 0 :(得分:1)

您应该使用_update_by_query端点,而不是_update。另外,script部分不正确,这可能是您获得class_cast_exception的原因。

请改为尝试:

POST styling_rules2/product_line_filters/_update_by_query
{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "product_line_attribute": "brand"
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      "filter": {
        "term": {
          "product_line_name": "women_skirts"
        }
      }
    }
  },
  "script": {
    "inline": "ctx._source.brand=brands"
  }
}