# Aproximate Nearest Neighbours python with leastic 8.8

**URL:** <https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [June 22, 2023, 12:45pm UTC](https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692 "2023-06-22T12:45:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ran\_Dubin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ran_dubin/32/122596_2.png) [@Ran\_Dubin](https://discuss.elastic.co/u/Ran_Dubin)\
**Post date:** [June 22, 2023, 12:45pm UTC](https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692/1 "2023-06-22T12:45:38Z")

</div>

Hello All  
I am using elastic version 8.8.1.  
The API for ANN has changed and I managed to index but not to query content.

> **[kNN search in Elasticsearch | Elastic Docs](https://www.elastic.co/docs/solutions/search/vector/knn)**
>
> A k-nearest neighbor (kNN) search finds the k nearest vectors to a query vector using a similarity metric such as cosine or L2 norm. With Elasticsearch...

Index:  
from datetime import datetime  
b\_index = 'shot\_index'  
dim = 1280  
response = es.indices.create(  
index=b\_index,  
body={  
"mappings": {  
"properties": {  
"my\_vector": {  
"type": "dense\_vector",  
"dims": dim, # adjust this according to your vector dimensions  
"index": True,  
"similarity": "l2\_norm" # choose the similarity metric  
},  
"timestamp": {  
"type": "date"  
}  
}  
}  
}  
)

# Index a document with a vector

doc = {  
'my\_vector': fvecs[0], # your vector goes here  
'timestamp': datetime.now(),  
'label':1  
}  
resp = es.index(index=b\_index, id=1, document=doc)  
print(resp['result'])

That works but the query didn't due to the change in the API in 8.8.  
How can I query?  
body = {  
"size": 10,  
"query": {  
"knn": {  
"image\_vector": {  
"vector": fvecs[0],  
"k": 5  
}  
}  
},  
"\_source": ["name", "file\_type"]  
}

response = es.search(index=b\_index, body=body)

Result in:

BadRequestError: BadRequestError(400, 'illegal\_argument\_exception', '[knn] queries cannot be provided directly, use the [knn] body parameter instead')

I tried various methods and checked the docs but I didn't see any python related updates.

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [June 22, 2023, 2:12pm UTC](https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692/2 "2023-06-22T14:12:44Z")

</div>

Heya @Ran_Dubin !

`knn` is its own top level object and isn't nested under `query`. For the python client it should look something like

```python
es.search(index= b_index, knn={"query_vector": fvecs[0], "k":5, "num_candidates": 50, "field": image_vector}, size=10)

```

---

<div class="post-metadata">

**Author:** ![Ran\_Dubin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ran_dubin/32/122596_2.png) [@Ran\_Dubin](https://discuss.elastic.co/u/Ran_Dubin)\
**Post date:** [June 25, 2023, 1:58pm UTC](https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692/3 "2023-06-25T13:58:29Z")

</div>

Thank you for the reply. Is there a blog/ python example for version 8.8?  
Thanks!

---

<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:** [July 23, 2023, 1:59pm UTC](https://discuss.elastic.co/t/aproximate-nearest-neighbours-python-with-leastic-8-8/336692/4 "2023-07-23T13:59:07Z")

</div>

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