-1

A functional script query is working fine when i dont add certain scripts to it but it fails and returns > [script] Too many dynamic script compilations within, max: [75/5m]; please use indexed, or scripts with parameters instead; this limit can be changed by the [script.max_compilations_rate] setting > this error when i add mainly 2 scripts to it. Also this is not happening everytime, sometimes it works fine but most of the times it returns error. Below are the queries :

Working query :

{
  "_source": false,
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "doc['profile_completed_score'].value >0 ? doc['profile_completed_score'].value * 0.15 : 0"
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['profile_pic'].empty)return 10; return 0;"
            }
          }
        },
        {
          "gauss": {
            "city_geolocation": {
              "origin": {
                "lat": "28.536000",
                "lon": "77.391000"
              },
              "scale": "100km",
              "offset": "100km",
              "decay": 0.0001
            }
          },
          "weight": 15
        },
        {
          "script_score": {
            "script": {
              "source": "def designation=0; if(doc['designation_name.keyword'].value != null && doc['designation_name.keyword'].value==params.designation) { designation=30; } else { if(doc['designation_name.keyword'].value != null && (doc['designation_name.keyword'].value.toUpperCase().indexOf(params.designation.toUpperCase())>-1 || params.designation.toUpperCase().indexOf(doc['designation_name.keyword'].value.toUpperCase())>-1)) designation=15; } return designation;",
              "params": {
                "designation": "Technical Manager"
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "def visited=0;def vscore=0;if(!doc['visited_event'].empty && doc['visited_event'].values.contains(params.fact))visited=10;if(!doc['avg_visitor_score'].empty)vscore=doc['avg_visitor_score'].value*0.2; return visited+vscore;",
              "params": {
                "fact": "13039"
              }
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  },
  "size": 20,
  "from": 0
}

Query returning error most of times :

{
  "_source": false,
  "query": {
    "function_score": {
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "doc['profile_completed_score'].value >0 ? doc['profile_completed_score'].value * 0.15 : 0"
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if(!doc['profile_pic'].empty)return 10; return 0;"
            }
          }
        },
        {
          "gauss": {
            "city_geolocation": {
              "origin": {
                "lat": "28.536000",
                "lon": "77.391000"
              },
              "scale": "100km",
              "offset": "100km",
              "decay": 0.0001
            }
          },
          "weight": 15
        },
        {
          "script_score": {
            "script": {
              "source": "def designation=0; if(doc['designation_name.keyword'].value != null && doc['designation_name.keyword'].value==params.designation) { designation=30; } else { if(doc['designation_name.keyword'].value != null && (doc['designation_name.keyword'].value.toUpperCase().indexOf(params.designation.toUpperCase())>-1 || params.designation.toUpperCase().indexOf(doc['designation_name.keyword'].value.toUpperCase())>-1)) designation=15; } return designation;",
              "params": {
                "designation": "Technical Manager"
              }
            }
          }
        },
        {
          "script_score": {
           "script": {
              "source": "if(!doc['email_active'].empty && doc['email_active'].toInstant().toEpochMilli()/(params.divi) >= (params.epochtime)) return 30; return 0;",
              "params": {
                "divi": 1000,
                "epochtime": 1607863137
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "if((!doc['checkin_edition'].empty && doc['checkin_edition'].contains(params.fact))) return 30; return 0;",
              "params": {
                "fact": "1330689"
              }
            }
          }
        },
        {
          "script_score": {
            "script": {
              "source": "def visited=0;def vscore=0;if(!doc['visited_event'].empty && doc['visited_event'].values.contains(params.fact))visited=10;if(!doc['avg_visitor_score'].empty)vscore=doc['avg_visitor_score'].value*0.2; return visited+vscore;",
              "params": {
                "fact": "13039"
              }
            }
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  },
  "size": 20,
  "from": 0
}

Any kind of help is much appriciated.

[enter image description here][1]

Thanks//

[1]: https://i.stack.imgur.com/8LMRj.png**strong text**

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 19 '21 at 09:44

1 Answers1

0

[script] Too many dynamic script compilations within, max: [75/5m]; please use indexed, or scripts with parameters instead; this limit can be changed by the [script.max_compilations_rate] setting

The problems stems from the verbatim scripts in your script_score queries. If you read the error you're getting, the important part is please use indexed, or scripts with parameters instead

So what you should do is to store scripts so they are compiled only once.

POST _scripts/my-script
{
   "script": {
      "lang": "painless",
      "source": "doc['profile_completed_score'].value >0 ? doc['profile_completed_score'].value * 0.15 : 0"
   }
}

And then you can reference them by id in your queries

  "functions": [
    {
      "script_score": {
        "script": {
          "id": "my-script"
        }
      }
    },
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks Val, I tried what you suggested. stored all scripts and used them using id in my query. still its giving same error// Anyother option you can suggest? – Agnivesh Sharma Oct 21 '21 at 11:27
  • You need to store all your scripts, otherwise you'll keep getting this error – Val Nov 12 '21 at 07:23