# How to automate query from trained ML model

**URL:** https://discuss.elastic.co/t/how-to-automate-query-from-trained-ml-model/352956
**Category:** Elasticsearch
**Tags:** elastic-stack-machine-learning, vector-search
**Created:** [February 9, 2024, 4:07pm UTC](https://discuss.elastic.co/t/how-to-automate-query-from-trained-ml-model/352956 "2024-02-09T16:07:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![federica.forti](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@federica.forti](https://discuss.elastic.co/u/federica.forti)
#### Post date: [February 9, 2024, 4:07pm UTC](https://discuss.elastic.co/t/how-to-automate-query-from-trained-ml-model/352956/1 "2024-02-09T16:07:23Z")

</div>

Hi,

we have added a custom model among the machine learning models.  
During testing, we obtain, as a result, a vector that we use in the following query:

```auto
GET indexname/_search
{
  "query": {
    "script_score": {
      "query": {
        "exists": {
          "field": "content_embedding"
        }
      },
      "script": {
        "source": "cosineSimilarity(params.queryVector, 'content_embedding') + 1.0",
        "params": {
          "queryVector": [
            <VECTOR-RESULT-FROM-ML-TEST>
          ]
        }
      }
    }
  }
}

```

We wonder if there is a possibility to automate the query without necessarily having to write it in DevTools, by copying the vector of obtained results.

Thank you in advance.  
Federica

---

<div class="post-metadata">

### Author: ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)
#### Post date: [February 9, 2024, 8:49pm UTC](https://discuss.elastic.co/t/how-to-automate-query-from-trained-ml-model/352956/2 "2024-02-09T20:49:56Z")

</div>

Hi Federica,

Would a [KNN query with query vector builder](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-semantic-search) and cosine similarity calculation work, or do you need to use `script_score` for something specific? If your goal is to rank the results by semantic similarity, check if you can use this query:

```auto
GET indexname/_search
{
  "knn": {
    "filter": {
      "exists": {
        "field": "content_embedding"
      }
    }, 
    "field": "content_embedding",
    "k": 10,
    "num_candidates": 100,
    "similarity": "cosine", 
    "query_vector_builder": {
      "text_embedding": { 
        "model_id": "your-custom-model-id", 
        "model_text": "input query to your model"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 8, 2024, 8:50pm UTC](https://discuss.elastic.co/t/how-to-automate-query-from-trained-ml-model/352956/3 "2024-03-08T20:50:26Z")

</div>

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