diff options
author | dvora-h <67596500+dvora-h@users.noreply.github.com> | 2022-04-04 12:32:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-04 12:32:48 +0300 |
commit | 17f1140506310748a4f8164259d73fd0a4d362d5 (patch) | |
tree | 9a926a3fcbf8fa4acbe19b3c8462b198b8aef69b /docs | |
parent | 876cafc56ff465af5639a92ea18625f49de47563 (diff) | |
download | redis-py-17f1140506310748a4f8164259d73fd0a4d362d5.tar.gz |
Vector similarity search example (#2083)
* copy example
* fix
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples.rst | 1 | ||||
-rw-r--r-- | docs/examples/search_vector_similarity_examples.ipynb | 107 |
2 files changed, 108 insertions, 0 deletions
diff --git a/docs/examples.rst b/docs/examples.rst index 6d659a0..be577c0 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -10,3 +10,4 @@ Examples examples/asyncio_examples examples/search_json_examples examples/set_and_get_examples + examples/search_vector_similarity_examples
\ No newline at end of file diff --git a/docs/examples/search_vector_similarity_examples.ipynb b/docs/examples/search_vector_similarity_examples.ipynb new file mode 100644 index 0000000..42c03f0 --- /dev/null +++ b/docs/examples/search_vector_similarity_examples.ipynb @@ -0,0 +1,107 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Vector Similarity\n", + "## Adding Vector Fields" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "b'OK'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import redis\n", + "from redis.commands.search.field import VectorField\n", + "from redis.commands.search.query import Query\n", + "\n", + "r = redis.Redis(host='localhost', port=36379)\n", + "\n", + "schema = (VectorField(\"v\", \"HNSW\", {\"TYPE\": \"FLOAT32\", \"DIM\": 2, \"DISTANCE_METRIC\": \"L2\"}),)\n", + "r.ft().create_index(schema)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Searching" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Querying vector fields" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Result{2 total, docs: [Document {'id': 'a', 'payload': None, '__v_score': '0'}, Document {'id': 'b', 'payload': None, '__v_score': '3.09485009821e+26'}]}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r.hset(\"a\", \"v\", \"aaaaaaaa\")\n", + "r.hset(\"b\", \"v\", \"aaaabaaa\")\n", + "r.hset(\"c\", \"v\", \"aaaaabaa\")\n", + "\n", + "q = Query(\"*=>[KNN 2 @v $vec]\").return_field(\"__v_score\")\n", + "r.ft().search(q, query_params={\"vec\": \"aaaaaaaa\"})" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe" + }, + "kernelspec": { + "display_name": "Python 3.8.12 64-bit ('venv': venv)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} |