2

I'm facing a challenge using painless in a post filter, what I need is to access a nested field from the painless script, the goal here is to apply the filter base on prices range (these ranges are base on a quoting), and we don't want to lose the aggregation applied, one of the problems is that each index has its own pricing factors and add-ons which means an unique price per index and we want to calculate that price with painless.

quoting: 30 units Index 1 price: 24 Index 2 price: 35

quoting 60 units Index 1 price: 30 Index 2 price: 67

mapping

{
    "properties": {
      "studentName"{
          "type": "text",          
      },
      "class_room":{
          "type":"integer"
      },
      "pricing":{
          "type":"nested",
          "properties": {
              "name":{
                  "type":"text",
                  "fielddata":true
              },
              "addons":{
                  "type":"double"
              },
              "factor":{
                  "type":"double"
              }
          }
       }
    }
}

Index example (_source)

Index 1

{
    "studentName": "Michael Smith",
    "class_room": 12,
    "pricing" : [
        {
            "name": "A",
            "addons": 2.25,
            "factor": 0.6
        },
        {
            "name": "B",
            "addons": 1.5,
            "factor": 0.75            
        },
        {
            "name": "C",
            "addons": 2.0,
            "factor": 0.8            
        }
    ]
}

Index 2

{
    "studentName": "Sara Morales",
    "class_room": 12,
    "pricing" : [
        {
            "name": "A",
            "addons": 2.25,
            "factor": 0.6
        },
        {
            "name": "B",
            "addons": 1.5,
            "factor": 0.75            
        },
        {
            "name": "C",
            "addons": 2.0,
            "factor": 0.8            
        },
        {
            "name": "D",
            "addons": 1.0,
            "factor": 0.8            
        },
        {
            "name": "E",
            "addons": 1.1,
            "factor": 0.5            
        }                
    ]
}

post filter

{
  "post_filter": {
    "nested":{
      "path":"seedData",
      "query":{
        "bool": {
          "must": [
            {
              "script": {
                "script": {
                  "lang": "painless",
                  "inline": """
                      def units = params.units;
                      def price = 0;
                      for(def i =0; i< doc['pricing'].length; i++){
                        price += doc['pricing'][i].addons;
                        price += (doc['pricing'][i].factor * units);
                      }
                                            
                      if( price >= params.minPrice && price <= 58){
                        return true;
                      } else{
                        return false;
                      }
                  """,
                  "params": {
                    "units": 30,
                    "minPrice": 15,
                    "maxPrice": 58
                  }
                }
              }
            }            
          ]
        }
      }
    }
  }
}

Issues:

  • With the current post-filter script I get this No field found for [seedData] in mapping with types []

  • If try to access the value in for-loop like this doc['pricing.addons'].length, I always get 0.

Is there an error or I am missing something in the mapping?

Is there an error in the way that I'm accessing the nested field with painless

Thank you for your help

  • Check https://stackoverflow.com/a/63994104/8160318 or https://stackoverflow.com/a/64317211/8160318 or https://stackoverflow.com/a/64452344/8160318 or https://stackoverflow.com/a/61340856/8160318. – Joe - GMapsBook.com Nov 08 '20 at 16:01

0 Answers0