# Best method for calculating text embedding for a KNN search?

**URL:** <https://discuss.elastic.co/t/best-method-for-calculating-text-embedding-for-a-knn-search/329258>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning, runtime-fields, vector-search\
**Created:** [April 3, 2023, 10:36pm UTC](https://discuss.elastic.co/t/best-method-for-calculating-text-embedding-for-a-knn-search/329258 "2023-04-03T22:36:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott\_root](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scott_root/32/118018_2.png) [@scott\_root](https://discuss.elastic.co/u/scott_root)\
**Post date:** [April 3, 2023, 10:36pm UTC](https://discuss.elastic.co/t/best-method-for-calculating-text-embedding-for-a-knn-search/329258/1 "2023-04-03T22:36:43Z")

</div>

Hello,  
I am using Elastic Cloud and am using pre-trained ML models for text/semantic search and for image search. Currently, in order to do a text KNN search I have to make two API calls to ES, first to get the text embedding, and second to actually run the KNN search.

Is there no way to make a single call to ES (maybe using a script to get the text vector representation) to run a KNN search? I have searched through all the docs but haven't found an answer. ChatGPT had several suggestions that relied on runtime query scripts that would assign the vector value to a field used in the KNN query, but none of them worked for me.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dkyle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dkyle/32/59114_2.png) [@dkyle](https://discuss.elastic.co/u/dkyle)\
**Post date:** [April 4, 2023, 7:50am UTC](https://discuss.elastic.co/t/best-method-for-calculating-text-embedding-for-a-knn-search/329258/2 "2023-04-04T07:50:15Z")

</div>

Yes you can now do this in ES 8.7 in a single step by specifying your text embedding model in the KNN search.

> **[k-nearest neighbor (kNN) search | Elasticsearch Guide \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/master/knn-search.html#semantic-search)**

I guess ChatGPT hasn't read the 8.7 docs yet.

---

<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:** [May 2, 2023, 7:50am UTC](https://discuss.elastic.co/t/best-method-for-calculating-text-embedding-for-a-knn-search/329258/3 "2023-05-02T07:50:31Z")

</div>

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