Skip to content

Commit 218379a

Browse files
xeraaJeff Vestalcarlyrichmondjeffvestal
authored
Serverless video notebook (PK) (#516)
Co-authored-by: Jeff Vestal <[email protected]> Co-authored-by: Carly Richmond <[email protected]> Co-authored-by: Jeff Vestal <[email protected]>
1 parent 5a008db commit 218379a

File tree

3 files changed

+347
-2
lines changed

3 files changed

+347
-2
lines changed

supporting-blog-content/lexical-and-semantic-search-with-elasticsearch/ecommerce_dense_sparse_project.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,8 @@
11061106
"metadata": {},
11071107
"outputs": [],
11081108
"source": [
1109-
"\"\"\" Convert search_results from es|ql to a dict with _source\n",
1110-
" and subproperties of score, description, category, and product \"\"\"\n",
1109+
"\"\"\"Convert search_results from es|ql to a dict with _source\n",
1110+
"and subproperties of score, description, category, and product\"\"\"\n",
11111111
"\n",
11121112
"\n",
11131113
"def normalize_results(search_results):\n",

supporting-blog-content/your-first-elastic-agent/Your_First_Elastic_Agent.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
"output_type": "stream",
7070
"name": "stdout",
7171
"text": [
72+
"Enter your Elasticsearch Endpoint URL: https://your-first-elastic-agent-a4dc2b.es.us-central1.gcp.elastic.cloud:443\n",
73+
"Enter your Elasticsearch API Key: bmU3SGRKa0JJdGhRdS1VdGJ5STg6a0ViYkZKcXA5ekx6LWNEZkR2TGxvZw==\n",
7274
"Using connection details:\n",
7375
"Elasticsearch URL: https://your-first-elastic-agent-a4dc2b.es.us-central1.gcp.elastic.cloud:443\n",
7476
"Kibana URL: https://your-first-elastic-agent-a4dc2b.kb.us-central1.gcp.elastic.cloud:443\n",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# <font color='orange'>Create an Elastic Serverless Search Project Programmatically</font>\n",
21+
"\n",
22+
"\n",
23+
"## This notebook will show you how to:\n",
24+
" - Create an Elastics Serverless Project\n",
25+
"\n",
26+
"[Serverless API Docs](https://www.elastic.co/docs/api/doc/elastic-cloud-serverless/operation/operation-createelasticsearchproject#operation-createelasticsearchproject-body-application-json-optimized_for)\n",
27+
"\n",
28+
"This tutorial assumes you have an Elastic Cloud account.<br>\n",
29+
"If not head over to [cloud.elastic.co/trial](https://cloud.elastic.co/trial)"
30+
],
31+
"metadata": {
32+
"id": "_ebYbHHh_0hI"
33+
}
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"source": [
38+
"# Notebook Setup\n",
39+
"- Install elasticsearch python library\n",
40+
"- import required libraries"
41+
],
42+
"metadata": {
43+
"id": "JBHESuD7PJ54"
44+
}
45+
},
46+
{
47+
"cell_type": "code",
48+
"source": [
49+
"!pip install -qq elasticsearch"
50+
],
51+
"metadata": {
52+
"id": "_DmXlQWsGNeM",
53+
"colab": {
54+
"base_uri": "https://localhost:8080/"
55+
},
56+
"outputId": "6c04a6f7-a4e7-4e96-faaf-04d9bf2e9af7"
57+
},
58+
"execution_count": null,
59+
"outputs": [
60+
{
61+
"output_type": "stream",
62+
"name": "stdout",
63+
"text": [
64+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/960.5 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m952.3/960.5 kB\u001b[0m \u001b[31m28.8 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m960.5/960.5 kB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
65+
"\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/65.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.3/65.3 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
66+
"\u001b[?25h"
67+
]
68+
}
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"source": [
74+
"import requests\n",
75+
"import getpass\n",
76+
"from pprint import pprint\n",
77+
"from elasticsearch import Elasticsearch\n",
78+
"from elasticsearch.exceptions import ConnectionTimeout\n",
79+
"from IPython.display import clear_output\n",
80+
"from time import sleep"
81+
],
82+
"metadata": {
83+
"id": "cuomUVE-zYjB"
84+
},
85+
"execution_count": null,
86+
"outputs": []
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"source": [
91+
"# Setup Your Cloud API Key\n",
92+
"\n",
93+
"1. Generate your secret API key at https://cloud.elastic.co/account/keys\n",
94+
"2. Run the next cell to store your API Key"
95+
],
96+
"metadata": {
97+
"id": "yWSg_D91x9mF"
98+
}
99+
},
100+
{
101+
"cell_type": "code",
102+
"source": [
103+
"api_key = getpass.getpass(\"Enter your API key: \")\n",
104+
"\n",
105+
"print(\"API key successfully entered!\")"
106+
],
107+
"metadata": {
108+
"colab": {
109+
"base_uri": "https://localhost:8080/"
110+
},
111+
"id": "bidHlfsy2OPf",
112+
"outputId": "33eceaab-1963-4c36-992a-ed96cd98904a"
113+
},
114+
"execution_count": null,
115+
"outputs": [
116+
{
117+
"output_type": "stream",
118+
"name": "stdout",
119+
"text": [
120+
"Enter your API key: ··········\n",
121+
"API key successfully entered!\n"
122+
]
123+
}
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"source": [
129+
"## Create Elasticsearch project\n"
130+
],
131+
"metadata": {
132+
"id": "mt4_kL0b0E75"
133+
}
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"source": [
138+
"Configure:\n",
139+
"- name of project - <font color='orange'>`name`</font>\n",
140+
"- cloud region - <font color='orange'>`region_id`</font>\n",
141+
"- headers\n",
142+
"\n",
143+
"Then create the project!"
144+
],
145+
"metadata": {
146+
"id": "ZcQ2AgeNQHOA"
147+
}
148+
},
149+
{
150+
"cell_type": "code",
151+
"source": [
152+
"url = \"https://api.elastic-cloud.com/api/v1/serverless/projects/elasticsearch\"\n",
153+
"\n",
154+
"project_data = {\"name\": \"Serverless Search Project\", \"region_id\": \"aws-us-east-1\"}\n",
155+
"\n",
156+
"auth_header = f\"ApiKey {api_key}\"\n",
157+
"headers = {\"Content-Type\": \"application/json\", \"Authorization\": auth_header}\n",
158+
"\n",
159+
"es_project = requests.post(url, json=project_data, headers=headers)"
160+
],
161+
"metadata": {
162+
"id": "lVkyA7KUyDEO"
163+
},
164+
"execution_count": null,
165+
"outputs": []
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"source": [
170+
"Serverless projects generally start pretty quickly<br>We'll loop and wait for the project to be configured and ready to use"
171+
],
172+
"metadata": {
173+
"id": "3oFstW-wQVWl"
174+
}
175+
},
176+
{
177+
"cell_type": "code",
178+
"source": [
179+
"if 200 <= es_project.status_code < 300:\n",
180+
" es_project_keys = es_project.json()\n",
181+
" prg_name = es_project_keys[\"name\"]\n",
182+
" print(f\"Project {prg_name} creation started\")\n",
183+
"\n",
184+
" # wait for the project to be initialized and ready\n",
185+
" project_id = es_project.json()[\"id\"]\n",
186+
" print(\"Checking if project is created and ready\")\n",
187+
" loop = 1\n",
188+
" while True:\n",
189+
" es_project_check = requests.get(url + f\"/{project_id}/status\", headers=headers)\n",
190+
" if es_project_check.json()[\"phase\"] == \"initialized\":\n",
191+
" break\n",
192+
" else:\n",
193+
" clear_output(wait=True)\n",
194+
" print(\n",
195+
" f\"Waiting for project to be ready. Current status:{es_project_check.json()['phase']} - Loop {loop} Sleeping 10 seconds\"\n",
196+
" )\n",
197+
" sleep(10)\n",
198+
" loop += 1\n",
199+
"\n",
200+
" print(\"Project is ready\")\n",
201+
"\n",
202+
"else:\n",
203+
" print(es_project.text)"
204+
],
205+
"metadata": {
206+
"colab": {
207+
"base_uri": "https://localhost:8080/"
208+
},
209+
"id": "jlOjE8l5QU91",
210+
"outputId": "b7ecd36e-62df-4d29-e75f-121aa37e2d76"
211+
},
212+
"execution_count": null,
213+
"outputs": [
214+
{
215+
"output_type": "stream",
216+
"name": "stdout",
217+
"text": [
218+
"Waiting for project to be ready. Current status:initializing - Loop 16 Sleeping 10 seconds\n",
219+
"Project is ready\n"
220+
]
221+
}
222+
]
223+
},
224+
{
225+
"cell_type": "markdown",
226+
"source": [
227+
"# Retrieve Serverless Project Connection Information"
228+
],
229+
"metadata": {
230+
"id": "RogmJvonQnRu"
231+
}
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"source": [
236+
"## Create elasticsearch client\n",
237+
"\n",
238+
"We set the connection information from the `es_project` response above."
239+
],
240+
"metadata": {
241+
"id": "Uh0JpsnONMhv"
242+
}
243+
},
244+
{
245+
"cell_type": "code",
246+
"source": [
247+
"es = Elasticsearch(\n",
248+
" es_project_keys[\"endpoints\"][\"elasticsearch\"],\n",
249+
" basic_auth=(\n",
250+
" es_project_keys[\"credentials\"][\"username\"],\n",
251+
" es_project_keys[\"credentials\"][\"password\"],\n",
252+
" ),\n",
253+
")"
254+
],
255+
"metadata": {
256+
"id": "KG01YrIwMdHz"
257+
},
258+
"execution_count": null,
259+
"outputs": []
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"source": [
264+
"## Project API Key\n",
265+
"Create a [Project level API key](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html)"
266+
],
267+
"metadata": {
268+
"id": "Xhu2U-YszbDe"
269+
}
270+
},
271+
{
272+
"cell_type": "code",
273+
"source": [
274+
"project_key_response = es.security.create_api_key(\n",
275+
" name=\"full_access_key\",\n",
276+
" metadata={\"description\": \"API key for full access\"},\n",
277+
" expiration=\"14d\",\n",
278+
")\n",
279+
"\n",
280+
"project_api_key = project_key_response[\"encoded\"]\n",
281+
"print(f\"{project_key_response['name']} has been created\")"
282+
],
283+
"metadata": {
284+
"colab": {
285+
"base_uri": "https://localhost:8080/"
286+
},
287+
"id": "Puj1UWIKVtSv",
288+
"outputId": "b195543a-2206-4a20-8d10-b80030d276de"
289+
},
290+
"execution_count": null,
291+
"outputs": [
292+
{
293+
"output_type": "stream",
294+
"name": "stdout",
295+
"text": [
296+
"full_access_key has been created\n"
297+
]
298+
}
299+
]
300+
},
301+
{
302+
"cell_type": "markdown",
303+
"source": [
304+
"# Summary"
305+
],
306+
"metadata": {
307+
"id": "EnKDbhz6RIYx"
308+
}
309+
},
310+
{
311+
"cell_type": "code",
312+
"source": [
313+
"print(\"\\n--- Project Summary ---\")\n",
314+
"print(f\"Project Name: {es_project_keys['name']}\")\n",
315+
"print(f\"Elasticsearch Endpoint: {es_project_keys['endpoints']['elasticsearch']}\")\n",
316+
"if \"kibana\" in es_project_keys[\"endpoints\"]:\n",
317+
" print(f\"Kibana Endpoint: {es_project_keys['endpoints']['kibana']}\")\n",
318+
"else:\n",
319+
" print(\n",
320+
" \"Kibana Endpoint: Not available (Kibana may not be part of the serverless project)\"\n",
321+
" )\n",
322+
"\n",
323+
"\n",
324+
"print(\"\\n--- API Key ---\")\n",
325+
"print(f\"API key stored in variable: project_api_key\")\n",
326+
"\n",
327+
"show_api_key = input(\n",
328+
" \"Do you want to print the API key in clear text? (yes/no): \"\n",
329+
").lower()\n",
330+
"\n",
331+
"if show_api_key == \"yes\":\n",
332+
" print(f\"Project API Key: {project_api_key}\")\n",
333+
"else:\n",
334+
" print(\"API key not printed in clear text as requested. Stored in `project_api_key`\")"
335+
],
336+
"metadata": {
337+
"id": "ACDRmumLRGd_"
338+
},
339+
"execution_count": null,
340+
"outputs": []
341+
}
342+
]
343+
}

0 commit comments

Comments
 (0)