简体   繁体   中英

update a particular field of elasticsearch document

Hi I am trying to update documents a elasticsearch which meets specific criteria. I am using google sense(chrome extension) for making request. The request that I am making is as shown below:

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

sample document is as shown below:

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

desired result: update all the document which has product_line_attribute="brand" and product_line_name="women_skirts" to product_line_attribute="brands" .

problem: I am getting the error as follows:

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

thanks in advance!

You should use the _update_by_query endpoint and not _update . Also the script section is not correct, which is probably why you're getting a class_cast_exception .

Try this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM