1

I have the following ElasticSearch data structure for products in a webshop. It's a main product with children that have a special price and a regular price. I have omitted some extraneous info in the children to make it more clear.

{
  "_index": "vue_storefront_catalog_1_product_1617378559",
  "_type": "_doc",
  "_source": {
    "configurable_children": [
      {
        "price": 49.99,
        "special_price": 34.99
      }
      {
        "price": 49.99,
        "special_price": null
      }
    ]
}

Using the following mapping in Elasticsearh:

{
  "vue_storefront_catalog_1_product_1614928276" : {
    "mappings" : {
      "properties" : {
        "configurable_children" : {
          "properties" : {
            "price" : {
              "type" : "double"
            }
            "special_price" : {
              "type" : "double"
            }
          }
        }
      }
    }
  }
}

I have created a loop in a painless script to go through the children of the configurable_children. I need to do this to determine if the main product is on sale, based on the children configurable_children

      boolean hasSale = false;
      for(item in doc['configurable_children']) {
        hasSale = true;
        if (1 - (item['special_price'].value / item['price'].value) > 0.5) {
          hasSale = true;
        }
      }

      return hasSale

When I look at the results I see the following error:

        "failed_shards": [{
            "shard": 0,
            "index": "vue_storefront_catalog_1_product_1617512844",
            "node": "2EQcMMqlQgiuT5GAFPo90w",
            "reason": {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": ["org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)", "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)", "for(item in doc['configurable_children']) { ", "                ^---- HERE"],
                "script": " boolean hasSale = false; for(item in doc['configurable_children']) { hasSale = true; if (1 - (item['special_price'].value / item['price'].value) > 0.5) { hasSale = true; } } return hasSale ",
                "lang": "painless",
                "position": {
                    "offset": 42,
                    "start": 26,
                    "end": 70
                },
                "caused_by": {
                    "type": "illegal_argument_exception",
                    "reason": "No field found for [configurable_children] in mapping with types []"
                }
            }
        }]

No field found for [configurable_children] in mapping with types []

Anyone knows what I'm doing wrong? It looks like the for in loop needs a different kind of data? How do I look through all of the products to determine the sale price?

0 Answers0