Skip to content

Commit a41ecb3

Browse files
authored
Merge pull request #29 from HeidiSteen/master
[Azure Cognitive Search] Added heading, introduction, and fixed instructions.
2 parents 74c46a5 + be6d92a commit a41ecb3

File tree

1 file changed

+128
-5
lines changed

1 file changed

+128
-5
lines changed

Quickstart/azure-search-quickstart.ipynb

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Create a search index using REST APIs and Python"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"This Jupyter Notebook demonstrates index creation, data ingestion, and queries of an Azure Cognitive Search index by calling the REST APIs from Python code. This notebook is a companion document to this [Python quickstart](https://docs.microsoft.com/azure/search/search-get-started-python). \n",
15+
"\n",
16+
"\n",
17+
"As a first step, load the libraries used for working with JSON and formulating HTTP requests."
18+
]
19+
},
320
{
421
"cell_type": "code",
522
"execution_count": null,
@@ -15,7 +32,7 @@
1532
"cell_type": "markdown",
1633
"metadata": {},
1734
"source": [
18-
"Add the name and key of your search service."
35+
"In the second cell, input the request elements that will be constants on every request. Replace the search service name (YOUR-SEARCH-SERVICE-NAME) and admin API key (YOUR-ADMIN-API-KEY) with valid values. If you get ConnectionError \"Failed to establish a new connection\", verify that the api-key is a primary or secondary admin key, and that all leading and trailing characters (? and /) are in place."
1936
]
2037
},
2138
{
@@ -30,6 +47,13 @@
3047
" 'api-key': '<YOUR-ADMIN-API-KEY>' }"
3148
]
3249
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"In the third cell, formulate the request. This GET request targets the indexes collection of your search service and selects the name property of existing indexes so that you can see which indexes already exist. Index names must be unique. Check the list to make sure \"hotels-quickstart\" isn't listed.."
55+
]
56+
},
3357
{
3458
"cell_type": "code",
3559
"execution_count": null,
@@ -42,6 +66,13 @@
4266
"pprint(index_list)"
4367
]
4468
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"Specify the index definition, including the fields that define each search document. Fields have a name type, and attributes that determine how you can use the field. For example, \"searchable\" enables full text search on the field, \"retrievable\" means it can be returned in results, and \"filterable\" allows the field to be used in a filter expression."
74+
]
75+
},
4576
{
4677
"cell_type": "code",
4778
"execution_count": null,
@@ -73,6 +104,13 @@
73104
"}"
74105
]
75106
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"In the following cell, formulate the request. This POST request targets the indexes collection of your search service and creates an index based on the index schema you provided in the previous cell."
112+
]
113+
},
76114
{
77115
"cell_type": "code",
78116
"execution_count": null,
@@ -85,6 +123,13 @@
85123
"pprint(index)"
86124
]
87125
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"Next, provide four documents that conform to the index schema. Specify an upload action for each document."
131+
]
132+
},
88133
{
89134
"cell_type": "code",
90135
"execution_count": null,
@@ -173,6 +218,13 @@
173218
"}"
174219
]
175220
},
221+
{
222+
"cell_type": "markdown",
223+
"metadata": {},
224+
"source": [
225+
"Formulate the request. This POST request targets the docs collection of the hotels-quickstart index and pushes the documents provided in the previous step."
226+
]
227+
},
176228
{
177229
"cell_type": "code",
178230
"execution_count": null,
@@ -185,13 +237,32 @@
185237
"pprint(index_content)"
186238
]
187239
},
240+
{
241+
"cell_type": "markdown",
242+
"metadata": {},
243+
"source": [
244+
"You are now ready to run some queries. The next cell contains a query expression that executes an empty search (search=*), returning an unranked list (search score = 1.0) of arbitrary documents. By default, Azure Cognitive Search returns 50 matches at a time. As structured, this query returns an entire document structure and values. Add $count=true to get a count of all documents (4) in the results."
245+
]
246+
},
188247
{
189248
"cell_type": "code",
190249
"execution_count": null,
191250
"metadata": {},
192251
"outputs": [],
193252
"source": [
194-
"searchstring = '&search=hotels wifi&$count=true&$select=HotelId,HotelName'"
253+
"searchstring = '&search=*&$count=true'\n",
254+
"\n",
255+
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
256+
"response = requests.get(url, headers=headers, json=searchstring)\n",
257+
"query = response.json()\n",
258+
"pprint(query)"
259+
]
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"metadata": {},
264+
"source": [
265+
"The next query adds whole terms to the search expression (\"hotels\" and \"wifi\") and selects just a few fields to return in the results."
195266
]
196267
},
197268
{
@@ -200,19 +271,40 @@
200271
"metadata": {},
201272
"outputs": [],
202273
"source": [
274+
"searchstring = '&search=hotels wifi&$count=true&$select=HotelId,HotelName'\n",
275+
"\n",
203276
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
204277
"response = requests.get(url, headers=headers, json=searchstring)\n",
205278
"query = response.json()\n",
206279
"pprint(query)"
207280
]
208281
},
282+
{
283+
"cell_type": "markdown",
284+
"metadata": {},
285+
"source": [
286+
"This query adds a $filter expression, returning only those hotels with a rating greater than 4."
287+
]
288+
},
209289
{
210290
"cell_type": "code",
211291
"execution_count": null,
212292
"metadata": {},
213293
"outputs": [],
214294
"source": [
215-
"searchstring = '&search=*&$filter=Rating gt 4&$select=HotelId,HotelName,Description'"
295+
"searchstring = '&search=*&$filter=Rating gt 4&$select=HotelId,HotelName,Description'\n",
296+
"\n",
297+
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
298+
"response = requests.get(url, headers=headers, json=searchstring)\n",
299+
"query = response.json()\n",
300+
"pprint(query)"
301+
]
302+
},
303+
{
304+
"cell_type": "markdown",
305+
"metadata": {},
306+
"source": [
307+
"By default, the search engine returns the top 50 documents but you can use top and skip to add pagination and choose how many documents in each result. This query returns two documents in each result set."
216308
]
217309
},
218310
{
@@ -221,7 +313,19 @@
221313
"metadata": {},
222314
"outputs": [],
223315
"source": [
224-
"searchstring = '&search=boutique&$top=2&$select=HotelId,HotelName,Description'"
316+
"searchstring = '&search=boutique&$top=2&$select=HotelId,HotelName,Description'\n",
317+
"\n",
318+
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
319+
"response = requests.get(url, headers=headers, json=searchstring)\n",
320+
"query = response.json()\n",
321+
"pprint(query)"
322+
]
323+
},
324+
{
325+
"cell_type": "markdown",
326+
"metadata": {},
327+
"source": [
328+
"In this last example, use $orderby to sort results by city. This example includes fields from the Address collection."
225329
]
226330
},
227331
{
@@ -230,7 +334,19 @@
230334
"metadata": {},
231335
"outputs": [],
232336
"source": [
233-
"searchstring = '&search=pool&$orderby=Address/City&$select=HotelId, HotelName, Address/City, Address/StateProvince'"
337+
"searchstring = '&search=pool&$orderby=Address/City&$select=HotelId, HotelName, Address/City, Address/StateProvince'\n",
338+
"\n",
339+
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
340+
"response = requests.get(url, headers=headers, json=searchstring)\n",
341+
"query = response.json()\n",
342+
"pprint(query)"
343+
]
344+
},
345+
{
346+
"cell_type": "markdown",
347+
"metadata": {},
348+
"source": [
349+
"If you are finished with this index, you can delete it by running the following lines. Deleting unnecessary indexes frees up space for steeping through more quickstarts and tutorials."
234350
]
235351
},
236352
{
@@ -243,6 +359,13 @@
243359
"response = requests.delete(url, headers=headers)"
244360
]
245361
},
362+
{
363+
"cell_type": "markdown",
364+
"metadata": {},
365+
"source": [
366+
"Confirm the index deletion by running the following script that lists all of the indexes on your search service. If hotels-quickstart is not listed, you've successfully deleted the index and have completed this quickstart."
367+
]
368+
},
246369
{
247370
"cell_type": "code",
248371
"execution_count": null,

0 commit comments

Comments
 (0)