Script caching

Im having script like

if (doc['type'].value == 'pwp') {
            if (doc['rodzaj_pwp'].value == 1) {if (_score < params.pwp['1']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 2) {if (_score < params.pwp['2']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 3) {if (_score < params.pwp['3']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 4) {if (_score < params.pwp['4']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 5) {if (_score < params.pwp['5']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 6) {if (_score < params.pwp['6']) {return 0;} else {return _score;}}
            if (doc['rodzaj_pwp'].value == 7) {if (_score < params.pwp['7']) {return 0;} else {return _score;}}    
        }
        
        if (doc['type'].value == 'kolekcja') {
            if (doc['kolekcja'].value == 1) {if (_score < params.collections['1']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 2) {if (_score < params.collections['2']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 3) {if (_score < params.collections['3']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 4) {if (_score < params.collections['4']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 5) {if (_score < params.collections['5']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 6) {if (_score < params.collections['6']) {return 0;} else {return _score;}}
            if (doc['kolekcja'].value == 7) {if (_score < params.collections['7']) {return 0;} else {return _score;}}
        }

And Im using it in function_score

Documentation says that script should be cached

If you compile too many unique scripts within a small amount of time, Elasticsearch will reject the new dynamic scripts with a circuit_breaking_exception error. By default, up to 15 inline scripts per minute will be compiled. You can change this setting dynamically by setting script.max_compilations_rate.

When I look at GET _nodes/stats/script I get

"attributes" : {
        "ml.machine_memory" : "68311584768",
        "xpack.installed" : "true",
        "ml.max_open_jobs" : "20",
        "ml.enabled" : "true"
      },
      "script" : {
        "compilations" : 75668,
        "cache_evictions" : 75568
      }

And I would like to know what does it mean? That I have 75668 queries executed ?? does it counts all executions or only from some period of time ?? And how cache_evictions works ?

Hey,

are you possibly changing the script with every query you send? Elasticsearch is able to cache scripts, so it does not need to compile them again and will reuse them, if they are the same script. However in your example you seem to have a different script with every query execution resulting in that script compilation cache to become constantly evicted.

Is there any chance you can parametrize your script further to prevent the ongoing recompilation?

Script is never changed, only parametrs are changing,
What more can I parametrize ??

can you share two full sample requests?

@spinscale

{
  "source": {
    "query": {
      "function_score": {
        "min_score": 0.01,
        "boost_mode": "replace",
        "query": {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "multi_match": {
                        "query": "{{search_phrase}}",
                        "fields": [
                          "id",
                          "content.*_diacritics",
                        ],
                        "type": "phrase",
                        "boost": 10
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        "script_score": {
          "script": {
            "source": "if (doc['type'].value == 'pwp') {              if (doc['rodzaj_pwp'].value == 1) {if (_score < params.pwp['1']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 2) {if (_score < params.pwp['2']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 3) {if (_score < params.pwp['3']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 4) {if (_score < params.pwp['4']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 5) {if (_score < params.pwp['5']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 6) {if (_score < params.pwp['6']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 7) {if (_score < params.pwp['7']) {return 0;} else {return _score;}}              }                    if (doc['type'].value == 'kolekcja') {              if (doc['kolekcja'].value == 1) {if (_score < params.collections['1']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 2) {if (_score < params.collections['2']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 3) {if (_score < params.collections['3']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 4) {if (_score < params.collections['4']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 5) {if (_score < params.collections['5']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 6) {if (_score < params.collections['6']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 7) {if (_score < params.collections['7']) {return 0;} else {return _score;}}          }",
            "params": {
              "pwp": {
                "1": 9.783159255981445,
                "2": 6.081907081604004,
                "3": 5.965337562561035,
                "4": 0,
                "5": 6.081907081604004,
                "6": 6.081907081604004,
                "7": 0
              },
              "collections": {
                "1": 28.80948486328125,
                "2": 24.940118408203123,
                "3": 2.6661086082458496,
                "4": 10.809349822998046,
                "5": 6.160078811645508,
                "6": 6.687025451660156,
                "7": 4.0727208137512205,
                "8": 10.451463317871093
              }
            }
          }
        }
      }
    },
    "size": 50,
    "from": 0,
    "_source": {
      "excludes": [
        "content"
      ]
    },
    "highlight": {
      "fields": {
        "content.*": {
          "number_of_fragments": 10,
          "fragment_size": 1
        }
      }
    }
  },
  "params": {
    "search_phrase": "Phrase"
  }
}


{
  "source": {
    "query": {
      "function_score": {
        "min_score": 0.01,
        "boost_mode": "replace",
        "query": {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "multi_match": {
                        "query": "{{search_phrase}}",
                        "fields": [
                          "id",
                          "content.*_diacritics"
                        ],
                        "type": "phrase",
                        "boost": 10
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        "script_score": {
          "script": {
            "source": "if (doc['type'].value == 'pwp') {              if (doc['rodzaj_pwp'].value == 1) {if (_score < params.pwp['1']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 2) {if (_score < params.pwp['2']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 3) {if (_score < params.pwp['3']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 4) {if (_score < params.pwp['4']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 5) {if (_score < params.pwp['5']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 6) {if (_score < params.pwp['6']) {return 0;} else {return _score;}}              if (doc['rodzaj_pwp'].value == 7) {if (_score < params.pwp['7']) {return 0;} else {return _score;}}              }                    if (doc['type'].value == 'kolekcja') {              if (doc['kolekcja'].value == 1) {if (_score < params.collections['1']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 2) {if (_score < params.collections['2']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 3) {if (_score < params.collections['3']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 4) {if (_score < params.collections['4']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 5) {if (_score < params.collections['5']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 6) {if (_score < params.collections['6']) {return 0;} else {return _score;}}              if (doc['kolekcja'].value == 7) {if (_score < params.collections['7']) {return 0;} else {return _score;}}          }",
            "params": {
              "pwp": {
                "1": 14.189854431152343,
                "2": 14.785639572143554,
                "3": 9.516910171508789,
                "4": 3.2049719810485837,
                "5": 14.769041061401367,
                "6": 14.126902770996093,
                "7": 0.9219526290893554
              },
              "collections": {
                "1": 62.0042724609375,
                "2": 36.51323547363281,
                "3": 0.05659182071685791,
                "4": 16.65665473937988,
                "5": 14.443638610839843,
                "6": 10.999112319946288,
                "7": 14.300667572021483,
                "8": 16.40863494873047
              }
            }
          }
        }
      }
    },
    "size": 50,
    "from": 0,
    "_source": {
      "excludes": [
        "content"
      ]
    },
    "highlight": {
      "fields": {
        "content.*": {
          "number_of_fragments": 10,
          "fragment_size": 1
        }
      }
    }
  },
  "params": {
    "search_phrase": "Metalowe "
  }
}

what endpoint are you hitting? and what elasticsearch version is this?

1 _search/template
2 6.8

can you try using _search and replace the search term directly? I wold like to take the templating mechanism out of the equation - which might be the culprit here...

Removing template query worked, thanks a lot

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.