You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Build a Jupyter Notebook that creates, loads, and queries an Azure Cognitive Search index using Python and the [azure-search-documents library](/python/api/overview/azure/search-documents-readme) in the Azure SDK for Python. This article explains how to build a notebook step by step. Alternatively, you can [download and run a finished Jupyter Python notebook](https://github.com/Azure-Samples/azure-search-python-samples).
25
+
In this exercise, build a Jupyter Notebook that creates, loads, and queries an Azure Cognitive Search index using Python and the [azure-search-documents library](/python/api/overview/azure/search-documents-readme) in the Azure SDK for Python. This article explains how to build a notebook step by step. Alternatively, you can [download and run a finished Jupyter Python notebook](https://github.com/Azure-Samples/azure-search-python-samples).
26
26
27
27
If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
28
28
29
29
## Prerequisites
30
30
31
-
The following services and tools are required for this quickstart.
31
+
The following services and tools are used in this quickstart.
32
32
33
33
* Visual Studio Code with the Python extension (or equivalent tool), with Python 3.7 or later
34
34
@@ -38,25 +38,28 @@ The following services and tools are required for this quickstart.
38
38
39
39
## Copy a key and URL
40
40
41
-
REST calls require the service URL and an access key on every request. A search service is created with both, so if you added Azure Cognitive Search to your subscription, follow these steps to get the necessary information:
41
+
To connect to your search service, provide the endpoint and an access key. A search service is created with both, so if you added Azure Cognitive Search to your subscription, follow these steps to get the necessary information:
42
42
43
43
1.[Sign in to the Azure portal](https://portal.azure.com/), and in your search service **Overview** page, get the URL. An example endpoint might look like `https://mydemo.search.windows.net`.
44
44
45
45
1. In **Settings** > **Keys**, get an admin key for full rights on the service. There are two interchangeable admin keys, provided for business continuity in case you need to roll one over. You can use either the primary or secondary key on requests for adding, modifying, and deleting objects.
46
46
47
47

48
48
49
-
All requests require an api-key on every request sent to your service. Having a valid key establishes trust, on a per request basis, between the application sending the request and the service that handles it.
49
+
You'll provide these values in the next section when you set up the connection.
50
50
51
51
## Connect to Azure Cognitive Search
52
52
53
-
In this task, start Jupyter Notebook and verify that you can connect to Azure Cognitive Search. You'll do this step by requesting a list of indexes from your service.
53
+
In this task, create the notebook, load the libraries, and set up your clients.
54
54
55
-
1. Create a new Python3 notebook.
55
+
1. Create a new Python3 notebook in Visual Studio Code:
56
+
57
+
1. Press F1 and search for "Python Select Interpreter" and choose a version of Python 3.7 or later.
58
+
1. Press F1 again and search for "Create: New Jupyter Notebook". You should have an empty, untitled `.ipynb` file open in the editor ready for the first entry.
56
59
57
60
1. In the first cell, load the libraries from the Azure SDK for Python, including [azure-search-documents](/python/api/azure-search-documents).
58
61
59
-
```python
62
+
```python
60
63
%pip install azure-search-documents --pre
61
64
%pip show azure-search-documents
62
65
@@ -73,11 +76,11 @@ In this task, start Jupyter Notebook and verify that you can connect to Azure Co
73
76
SimpleField,
74
77
SearchableField
75
78
)
76
-
```
79
+
```
77
80
78
-
1.In the second cell, input the request elements that will be constants on every request. Provide your search service name, admin API key, and query API key, copied in a previous step. This cell also sets up the clients you'll use for specific operations: [SearchIndexClient](/python/api/azure-search-documents/azure.search.documents.indexes.searchindexclient) to create an index, and [SearchClient](/python/api/azure-search-documents/azure.search.documents.searchclient) to query an index.
81
+
1. Add a second cellandpaste inthe connection information. This cell also sets up the clients you'll use for specific operations: [SearchIndexClient](/python/api/azure-search-documents/azure.search.documents.indexes.searchindexclient) to create an index, and [SearchClient](/python/api/azure-search-documents/azure.search.documents.searchclient) to query an index.
79
82
80
-
```python
83
+
```python
81
84
service_name ="YOUR-SEARCH-SERVICE-NAME"
82
85
admin_key ="YOUR-SEARCH-SERVICE-ADMIN-API-KEY"
83
86
@@ -92,25 +95,25 @@ In this task, start Jupyter Notebook and verify that you can connect to Azure Co
92
95
search_client = SearchClient(endpoint=endpoint,
93
96
index_name=index_name,
94
97
credential=AzureKeyCredential(admin_key))
95
-
```
98
+
```
96
99
97
100
1. In the third cell, run a delete_index operation to clear your service of any existing *hotels-quickstart* indexes. Deleting the index allows you to create another *hotels-quickstart* index of the same name.
98
101
99
-
```python
102
+
```python
100
103
try:
101
104
result = admin_client.delete_index(index_name)
102
105
print ('Index', index_name, 'Deleted')
103
106
exceptExceptionas ex:
104
107
print (ex)
105
-
```
108
+
```
106
109
107
110
1. Run each step.
108
111
109
112
## 1 - Create an index
110
113
111
-
Required elements of an index include a name, a fields collection, and a key. The fields collection defines the structure of a logical *search document*, used for both loading data and returning results.
114
+
Required index elements include a name, a fields collection, and a document key that uniquely identifies each search document. The fields collection defines the structure of a logical *search document*, used for both loading data and returning results.
112
115
113
-
Each field has a name, type, and attributes that determine how the field is used (for example, whether it's full-text searchable, filterable, or retrievable in search results). Within an index, one of the fields of type `Edm.String` must be designated as the *key* for document identity.
116
+
Within the fields collection, each field has a name, type, and attributes that determine how the field is used (for example, whether it's full-text searchable, filterable, or retrievable in search results). Within an index, one of the fields of type `Edm.String` must be designated as the *key* for document identity.
114
117
115
118
This index is named "hotels-quickstart"and has the field definitions you see below. It's a subset of a larger [Hotels index](https://github.com/Azure-Samples/azure-search-sample-data/blob/master/hotels/Hotels_IndexDefinition.JSON) used in other walkthroughs. We trimmed it in this quickstart for brevity.
116
119
@@ -329,7 +332,7 @@ This step shows you how to query an index using the **search** method of the [se
329
332
print("Category: {}".format(result["Category"]))
330
333
```
331
334
332
-
1. In this example, we'll use the autocomplete function. Autocomplete is typically used in a search box to provide potential matches as the user types into the search box.
335
+
1. In the last example, we'll use the autocomplete function. Autocomplete is typically used in a search box to provide potential matches as the user types into the search box.
333
336
334
337
When the index was created, a suggester named `sg` was also created as part of the request. A suggester definition specifies which fields can be used to find potential matches to suggester requests. In this example, those fields are 'Tags', 'Address/City', 'Address/Country'. To simulate auto-complete, passin the letters "sa"as a partial string. The autocomplete method of [SearchClient](/python/api/azure-search-documents/azure.search.documents.searchclient) sends back potential term matches.
335
338
@@ -352,7 +355,7 @@ If you're using a free service, remember that you're limited to three indexes, i
352
355
353
356
## Next steps
354
357
355
-
In this JavaScript quickstart, you worked through a series of tasks to create an index, load it with documents, andrun queries. To continue learning, try the following tutorial.
358
+
In this Python quickstart, you worked through the fundamental workflow using the azure.search.documents library from the Python SDK. You performed tasks that created an index, loaded it with documents, andran queries. To continue learning, try the following tutorial.
356
359
357
360
> [!div class="nextstepaction"]
358
361
> [Tutorial: Add search to web apps](tutorial-python-overview.md)
0 commit comments