Skip to content

Commit

Permalink
Merge pull request #29 from HeidiSteen/master
Browse files Browse the repository at this point in the history
[Azure Cognitive Search] Added heading, introduction, and fixed instructions.
  • Loading branch information
HeidiSteen authored Apr 1, 2020
2 parents 74c46a5 + be6d92a commit a41ecb3
Showing 1 changed file with 128 additions and 5 deletions.
133 changes: 128 additions & 5 deletions Quickstart/azure-search-quickstart.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create a search index using REST APIs and Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"\n",
"\n",
"As a first step, load the libraries used for working with JSON and formulating HTTP requests."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -15,7 +32,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Add the name and key of your search service."
"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."
]
},
{
Expand All @@ -30,6 +47,13 @@
" 'api-key': '<YOUR-ADMIN-API-KEY>' }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -42,6 +66,13 @@
"pprint(index_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -73,6 +104,13 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -85,6 +123,13 @@
"pprint(index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, provide four documents that conform to the index schema. Specify an upload action for each document."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -173,6 +218,13 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formulate the request. This POST request targets the docs collection of the hotels-quickstart index and pushes the documents provided in the previous step."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -185,13 +237,32 @@
"pprint(index_content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"searchstring = '&search=hotels wifi&$count=true&$select=HotelId,HotelName'"
"searchstring = '&search=*&$count=true'\n",
"\n",
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
"response = requests.get(url, headers=headers, json=searchstring)\n",
"query = response.json()\n",
"pprint(query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next query adds whole terms to the search expression (\"hotels\" and \"wifi\") and selects just a few fields to return in the results."
]
},
{
Expand All @@ -200,19 +271,40 @@
"metadata": {},
"outputs": [],
"source": [
"searchstring = '&search=hotels wifi&$count=true&$select=HotelId,HotelName'\n",
"\n",
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
"response = requests.get(url, headers=headers, json=searchstring)\n",
"query = response.json()\n",
"pprint(query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This query adds a $filter expression, returning only those hotels with a rating greater than 4."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"searchstring = '&search=*&$filter=Rating gt 4&$select=HotelId,HotelName,Description'"
"searchstring = '&search=*&$filter=Rating gt 4&$select=HotelId,HotelName,Description'\n",
"\n",
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
"response = requests.get(url, headers=headers, json=searchstring)\n",
"query = response.json()\n",
"pprint(query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
Expand All @@ -221,7 +313,19 @@
"metadata": {},
"outputs": [],
"source": [
"searchstring = '&search=boutique&$top=2&$select=HotelId,HotelName,Description'"
"searchstring = '&search=boutique&$top=2&$select=HotelId,HotelName,Description'\n",
"\n",
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
"response = requests.get(url, headers=headers, json=searchstring)\n",
"query = response.json()\n",
"pprint(query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this last example, use $orderby to sort results by city. This example includes fields from the Address collection."
]
},
{
Expand All @@ -230,7 +334,19 @@
"metadata": {},
"outputs": [],
"source": [
"searchstring = '&search=pool&$orderby=Address/City&$select=HotelId, HotelName, Address/City, Address/StateProvince'"
"searchstring = '&search=pool&$orderby=Address/City&$select=HotelId, HotelName, Address/City, Address/StateProvince'\n",
"\n",
"url = endpoint + \"indexes/hotels-quickstart/docs\" + api_version + searchstring\n",
"response = requests.get(url, headers=headers, json=searchstring)\n",
"query = response.json()\n",
"pprint(query)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
Expand All @@ -243,6 +359,13 @@
"response = requests.delete(url, headers=headers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit a41ecb3

Please sign in to comment.