Elasticsearch: ('search_phase_execution_exception', 'compile error')

I am executing script_query with cosineSimilarity using tensorflow embedding. The embeddings are obtained from tensorflow-hub. model which is loaded using the url:
"TensorFlow Hub"

I am able to successfully create the index and execute simple search queries. But I get the following error when I try and execute script_query.
Query:

    similar_query = {
        "script_score":{
            "query":{"match_all":{}},
            "script":{
                "source":"cosineSimilarity(params.query_vector, 'vector')+1.0",
                    "params": {
                        "query_vector": query_vector
                    }
            }
        }
    }

Error:
raceback (most recent call last):
File "similarity_search.py", line 49, in
similar_Utterances(es, query_string)
File "similarity_search.py", line 34, in similar_Utterances
response = es.search(index=INDEX_NAME, body=body)
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 152, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/client/init.py", line 1663, in search
body=body,
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/transport.py", line 392, in perform_request
raise e
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/transport.py", line 365, in perform_request
timeout=timeout,
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 269, in perform_request
self._raise_error(response.status, raw_data)
File "/home/narora/anaconda3/envs/tf_env/lib/python3.6/site-packages/elasticsearch/connection/base.py", line 316, in _raise_error
status_code, error_message, additional_info
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'compile error')

The first time I create the index and execute script query, it works and gives me output but after that simple search queries do work but for script query I get this error.
I have this code as part of the docker image but the error persists as an independent python script too.
I have checked the index using localhost:/index_name and the index structure is accurate.