How to automate query from trained ML model

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:

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

Hi Federica,

Would a KNN query with query vector builder 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:

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"
      }
    }
  }
}

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