|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Vector Similarity\n", |
| 8 | + "## Adding Vector Fields" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "data": { |
| 18 | + "text/plain": [ |
| 19 | + "b'OK'" |
| 20 | + ] |
| 21 | + }, |
| 22 | + "execution_count": 1, |
| 23 | + "metadata": {}, |
| 24 | + "output_type": "execute_result" |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "import redis\n", |
| 29 | + "from redis.commands.search.field import VectorField\n", |
| 30 | + "from redis.commands.search.query import Query\n", |
| 31 | + "\n", |
| 32 | + "r = redis.Redis(host='localhost', port=36379)\n", |
| 33 | + "\n", |
| 34 | + "schema = (VectorField(\"v\", \"HNSW\", {\"TYPE\": \"FLOAT32\", \"DIM\": 2, \"DISTANCE_METRIC\": \"L2\"}),)\n", |
| 35 | + "r.ft().create_index(schema)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Searching" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "### Querying vector fields" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 2, |
| 55 | + "metadata": { |
| 56 | + "pycharm": { |
| 57 | + "name": "#%%\n" |
| 58 | + } |
| 59 | + }, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "data": { |
| 63 | + "text/plain": [ |
| 64 | + "Result{2 total, docs: [Document {'id': 'a', 'payload': None, '__v_score': '0'}, Document {'id': 'b', 'payload': None, '__v_score': '3.09485009821e+26'}]}" |
| 65 | + ] |
| 66 | + }, |
| 67 | + "execution_count": 2, |
| 68 | + "metadata": {}, |
| 69 | + "output_type": "execute_result" |
| 70 | + } |
| 71 | + ], |
| 72 | + "source": [ |
| 73 | + "r.hset(\"a\", \"v\", \"aaaaaaaa\")\n", |
| 74 | + "r.hset(\"b\", \"v\", \"aaaabaaa\")\n", |
| 75 | + "r.hset(\"c\", \"v\", \"aaaaabaa\")\n", |
| 76 | + "\n", |
| 77 | + "q = Query(\"*=>[KNN 2 @v $vec]\").return_field(\"__v_score\")\n", |
| 78 | + "r.ft().search(q, query_params={\"vec\": \"aaaaaaaa\"})" |
| 79 | + ] |
| 80 | + } |
| 81 | + ], |
| 82 | + "metadata": { |
| 83 | + "interpreter": { |
| 84 | + "hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe" |
| 85 | + }, |
| 86 | + "kernelspec": { |
| 87 | + "display_name": "Python 3.8.12 64-bit ('venv': venv)", |
| 88 | + "language": "python", |
| 89 | + "name": "python3" |
| 90 | + }, |
| 91 | + "language_info": { |
| 92 | + "codemirror_mode": { |
| 93 | + "name": "ipython", |
| 94 | + "version": 3 |
| 95 | + }, |
| 96 | + "file_extension": ".py", |
| 97 | + "mimetype": "text/x-python", |
| 98 | + "name": "python", |
| 99 | + "nbconvert_exporter": "python", |
| 100 | + "pygments_lexer": "ipython3", |
| 101 | + "version": "3.9.2" |
| 102 | + }, |
| 103 | + "orig_nbformat": 4 |
| 104 | + }, |
| 105 | + "nbformat": 4, |
| 106 | + "nbformat_minor": 2 |
| 107 | +} |
0 commit comments