Skip to content

Commit bbe76b2

Browse files
Merge pull request #209812 from HeidiSteen/heidist-python
[azure search] Tested and updated Python quickstart to use VS Code
2 parents a5b5065 + 71db543 commit bbe76b2

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

articles/search/search-get-started-python.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: heidist
88
ms.service: cognitive-search
99
ms.devlang: python
1010
ms.topic: quickstart
11-
ms.date: 08/29/2022
11+
ms.date: 08/31/2022
1212
ms.custom: devx-track-python, mode-api
1313
---
1414

@@ -22,13 +22,13 @@ ms.custom: devx-track-python, mode-api
2222
> * [Portal](search-get-started-portal.md)
2323
>
2424
25-
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).
2626

2727
If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
2828

2929
## Prerequisites
3030

31-
The following services and tools are required for this quickstart.
31+
The following services and tools are used in this quickstart.
3232

3333
* Visual Studio Code with the Python extension (or equivalent tool), with Python 3.7 or later
3434

@@ -38,25 +38,28 @@ The following services and tools are required for this quickstart.
3838

3939
## Copy a key and URL
4040

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:
4242

4343
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`.
4444

4545
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.
4646

4747
![Get an HTTP endpoint and access key](media/search-get-started-rest/get-url-key.png "Get an HTTP endpoint and access key")
4848

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.
5050

5151
## Connect to Azure Cognitive Search
5252

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.
5454

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.
5659

5760
1. In the first cell, load the libraries from the Azure SDK for Python, including [azure-search-documents](/python/api/azure-search-documents).
5861

59-
```python
62+
```python
6063
%pip install azure-search-documents --pre
6164
%pip show azure-search-documents
6265

@@ -73,11 +76,11 @@ In this task, start Jupyter Notebook and verify that you can connect to Azure Co
7376
SimpleField,
7477
SearchableField
7578
)
76-
```
79+
```
7780

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 cell and paste in the 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.
7982

80-
```python
83+
```python
8184
service_name = "YOUR-SEARCH-SERVICE-NAME"
8285
admin_key = "YOUR-SEARCH-SERVICE-ADMIN-API-KEY"
8386

@@ -92,25 +95,25 @@ In this task, start Jupyter Notebook and verify that you can connect to Azure Co
9295
search_client = SearchClient(endpoint=endpoint,
9396
index_name=index_name,
9497
credential=AzureKeyCredential(admin_key))
95-
```
98+
```
9699

97100
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.
98101

99-
```python
102+
```python
100103
try:
101104
result = admin_client.delete_index(index_name)
102105
print ('Index', index_name, 'Deleted')
103106
except Exception as ex:
104107
print (ex)
105-
```
108+
```
106109

107110
1. Run each step.
108111

109112
## 1 - Create an index
110113

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.
112115

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.
114117

115118
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.
116119

@@ -329,7 +332,7 @@ This step shows you how to query an index using the **search** method of the [se
329332
print("Category: {}".format(result["Category"]))
330333
```
331334

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.
333336

334337
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, pass in 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.
335338

@@ -352,7 +355,7 @@ If you're using a free service, remember that you're limited to three indexes, i
352355

353356
## Next steps
354357

355-
In this JavaScript quickstart, you worked through a series of tasks to create an index, load it with documents, and run 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, and ran queries. To continue learning, try the following tutorial.
356359

357360
> [!div class="nextstepaction"]
358361
> [Tutorial: Add search to web apps](tutorial-python-overview.md)

0 commit comments

Comments
 (0)