|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "e180af3a-3a2c-4186-a577-7051ec6460b1", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "!pip install -qU elasticsearch sentence-transformers" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "63d22ea2-ecca-41bb-b08f-de8ad49cda41", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "# get the Elasticsearch client\n", |
| 21 | + "from elasticsearch import Elasticsearch\n", |
| 22 | + "from getpass import getpass\n", |
| 23 | + "\n", |
| 24 | + "ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n", |
| 25 | + "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n", |
| 26 | + "\n", |
| 27 | + "client = Elasticsearch(cloud_id=ELASTIC_CLOUD_ID, api_key=ELASTIC_API_KEY,)" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "id": "b367acaa-90e6-43d0-b9ae-cf42a0e2c0f1", |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "import json\n", |
| 38 | + "from urllib.request import urlopen\n", |
| 39 | + "from sentence_transformers import SentenceTransformer\n", |
| 40 | + "\n", |
| 41 | + "if NBTEST[\"notebook\"] in ['01-keyword-querying-filtering.ipynb', '02-hybrid-search.ipynb', '06-synonyms-api.ipynb']:\n", |
| 42 | + " # these tests need book_index to exist ahead of time\n", |
| 43 | + " client.indices.delete(index=\"book_index\", ignore_unavailable=True)\n", |
| 44 | + " \n", |
| 45 | + " mappings = {\n", |
| 46 | + " \"properties\": {\n", |
| 47 | + " \"title_vector\": {\n", |
| 48 | + " \"type\": \"dense_vector\",\n", |
| 49 | + " \"dims\": 384,\n", |
| 50 | + " \"index\": \"true\",\n", |
| 51 | + " \"similarity\": \"cosine\"\n", |
| 52 | + " }\n", |
| 53 | + " }\n", |
| 54 | + " }\n", |
| 55 | + " client.indices.create(index='book_index', mappings=mappings)\n", |
| 56 | + "\n", |
| 57 | + " url = \"https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/notebooks/search/data.json\"\n", |
| 58 | + " response = urlopen(url)\n", |
| 59 | + " books = json.loads(response.read())\n", |
| 60 | + "\n", |
| 61 | + " model = SentenceTransformer('all-MiniLM-L6-v2')\n", |
| 62 | + " operations = []\n", |
| 63 | + " for book in books:\n", |
| 64 | + " operations.append({\"index\": {\"_index\": \"book_index\"}})\n", |
| 65 | + " # Transforming the title into an embedding using the model\n", |
| 66 | + " book[\"title_vector\"] = model.encode(book[\"title\"]).tolist()\n", |
| 67 | + " operations.append(book)\n", |
| 68 | + " client.bulk(index=\"book_index\", operations=operations, refresh=True)" |
| 69 | + ] |
| 70 | + } |
| 71 | + ], |
| 72 | + "metadata": { |
| 73 | + "kernelspec": { |
| 74 | + "display_name": "Python 3 (ipykernel)", |
| 75 | + "language": "python", |
| 76 | + "name": "python3" |
| 77 | + }, |
| 78 | + "language_info": { |
| 79 | + "codemirror_mode": { |
| 80 | + "name": "ipython", |
| 81 | + "version": 3 |
| 82 | + }, |
| 83 | + "file_extension": ".py", |
| 84 | + "mimetype": "text/x-python", |
| 85 | + "name": "python", |
| 86 | + "nbconvert_exporter": "python", |
| 87 | + "pygments_lexer": "ipython3", |
| 88 | + "version": "3.11.6" |
| 89 | + } |
| 90 | + }, |
| 91 | + "nbformat": 4, |
| 92 | + "nbformat_minor": 5 |
| 93 | +} |
0 commit comments