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
Use the [JavaScript/TypeScript SDK for Azure Cognitive Search](/javascript/api/overview/azure/search-documents-readme) to create a Node.js application in JavaScript that creates, loads, and queries a search index.
25
24
26
25
This article demonstrates how to create the application step by step. Alternatively, you can [download the source code and data](https://github.com/Azure-Samples/azure-search-javascript-samples/tree/master/quickstart/v11) and run the application from the command line.
@@ -33,30 +32,29 @@ Before you begin, have the following tools and services:
33
32
34
33
+ An Azure Cognitive Search service. [Create a service](search-create-service-portal.md) or [find an existing service](https://portal.azure.com/#blade/HubsExtension/BrowseResourceBlade/resourceType/Microsoft.Search%2FsearchServices). You can use a free service for this quickstart.
35
34
36
-
+[Node.js](https://nodejs.org) and [NPM](https://www.npmjs.com)
35
+
+[Node.js](https://nodejs.org) and [npm](https://www.npmjs.com)
37
36
38
37
+[Visual Studio Code](https://code.visualstudio.com) or another IDE
39
38
40
-
41
39
## Set up your project
42
40
43
-
Start by getting the endpoint and key for your search service. Then create a new project with NPM as outlined below.
41
+
Start by getting the endpoint and key for your search service. Then create a new project with npm as outlined below.
44
42
45
43
<aname="get-service-info"></a>
46
44
47
45
### Copy a key and endpoint
48
46
49
-
Calls to the service require a URL endpoint and an access key on every request. As a first step, find the API key and URL to add to your project. You will specify both values when creating the client in a later step.
47
+
Calls to the service require a URL endpoint and an access key on every request. As a first step, find the API key and URL to add to your project. You'll specify both values when creating the client in a later step.
50
48
51
49
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`.
52
50
53
-
2. In **Settings** > **Keys**, get an admin key for full rights on the service, required if you are creating or deleting objects. There are two interchangeable primary and secondary keys. You can use either one.
51
+
2. In **Settings** > **Keys**, get an admin key for full rights on the service, required if you're creating or deleting objects. There are two interchangeable primary and secondary keys. You can use either one.
54
52
55
53

56
54
57
55
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.
58
56
59
-
### Create a new NPM project
57
+
### Create a new npm project
60
58
61
59
Begin by opening VS Code and its [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal) or another terminal such as the Node.js command prompt.
62
60
@@ -67,20 +65,20 @@ Begin by opening VS Code and its [integrated terminal](https://code.visualstudio
67
65
cd quickstart
68
66
```
69
67
70
-
2. Initialize an empty project with NPM by running
68
+
2. Initialize an empty project with npm by running the following command. To fully initialize the project, press Enter multiple times to accept the default values, except for the License, which you should set to "MIT".
71
69
72
70
```cmd
73
71
npm init
74
72
```
75
-
Accept the default values, except for the License, which you should set to "MIT".
76
-
77
-
3. Install `@azure/search-documents`, the [JavaScript/TypeScript SDK for Azure Cognitive Search](/javascript/api/overview/azure/search-documents-readme).
73
+
74
+
3. Install `@azure/search-documents`, the [JavaScript/TypeScript SDK for Azure Cognitive Search](/javascript/api/overview/azure/search-documents-readme).
78
75
79
76
```cmd
80
77
npm install @azure/search-documents
81
78
```
82
79
83
-
4. Install `dotenv`, which is used to import the environment variables such as our service name and API key.
80
+
4. Install `dotenv`, which is used to import the environment variables such as your search service name and API key.
81
+
84
82
```cmd
85
83
npm install dotenv
86
84
```
@@ -103,8 +101,8 @@ Begin by opening VS Code and its [integrated terminal](https://code.visualstudio
103
101
"author": "Your Name",
104
102
"license": "MIT",
105
103
"dependencies": {
106
-
"@azure/search-documents": "^11.2.0",
107
-
"dotenv": "^8.2.0"
104
+
"@azure/search-documents": "^11.3.0",
105
+
"dotenv": "^16.0.2"
108
106
}
109
107
}
110
108
```
@@ -165,7 +163,7 @@ With that in place, we're ready to create an index.
165
163
166
164
Create a file **hotels_quickstart_index.json**. This file defines how Azure Cognitive Search works with the documents you'll be loading in the next step. Each field will be identified by a `name` and have a specified `type`. Each field also has a series of index attributes that specify whether Azure Cognitive Search can search, filter, sort, and facet upon the field. Most of the fields are simple data types, but some, like `AddressType` are complex types that allow you to create rich data structures in your index. You can read more about [supported data types](/rest/api/searchservice/supported-data-types) and index attributes described in [Create Index (REST)](/rest/api/searchservice/create-index).
167
165
168
-
Add the following to **hotels_quickstart_index.json** or [download the file](https://github.com/Azure-Samples/azure-search-javascript-samples/blob/master/quickstart/v11/hotels_quickstart_index.json).
166
+
Add the following content to **hotels_quickstart_index.json** or [download the file](https://github.com/Azure-Samples/azure-search-javascript-samples/blob/master/quickstart/v11/hotels_quickstart_index.json).
169
167
170
168
```json
171
169
{
@@ -311,7 +309,7 @@ Within the main function, we then create a `SearchIndexClient`, which is used to
Next, we want to delete the index if it already exists. This is a common practice for test/demo code.
312
+
Next, we want to delete the index if it already exists. This operation is a common practice for test/demo code.
315
313
316
314
We do this by defining a simple function that tries to delete the index.
317
315
@@ -356,7 +354,7 @@ If you [downloaded the source code](https://github.com/Azure-Samples/azure-searc
356
354
357
355
You should see a series of messages describing the actions being taken by the program.
358
356
359
-
Open the **Overview** of your search service in the Azure portal. Select the **Indexes** tab. You should see something like the following:
357
+
Open the **Overview** of your search service in the Azure portal. Select the **Indexes** tab. You should see something like the following example:
360
358
361
359
:::image type="content" source="media/search-get-started-javascript/create-index-no-data.png" alt-text="Screenshot of Azure portal, search service Overview, Indexes tab" border="false":::
362
360
@@ -504,7 +502,7 @@ The queries are written in a `sendQueries()` function that we'll call in the mai
504
502
awaitsendQueries(searchClient);
505
503
```
506
504
507
-
Queries are sent using the `search()` method of `searchClient`. The first parameter is the search text and the second parameter is any additional search options.
505
+
Queries are sent using the `search()` method of `searchClient`. The first parameter is the search text and the second parameter specifies search options.
508
506
509
507
The first query searches `*`, which is equivalent to searching everything and selects three of the fields in the index. It's a best practice to only `select` the fields you need because pulling back unnecessary data can add latency to your queries.
510
508
@@ -528,7 +526,7 @@ async function sendQueries(searchClient) {
528
526
}
529
527
```
530
528
531
-
The remaining queries outlined below should also be added to the `sendQueries()` function. They are separated here for readability.
529
+
The remaining queries outlined below should also be added to the `sendQueries()` function. They're separated here for readability.
532
530
533
531
In the next query, we specify the search term `"wifi"` and also include a filter to only return results where the state is equal to `'FL'`. Results are also ordered by the Hotel's `Rating`.
534
532
@@ -547,7 +545,7 @@ for await (const result of searchResults.results) {
547
545
}
548
546
```
549
547
550
-
Next, the search is limited to a single searchable field using the `searchFields` parameter. This is a great option to make your query more efficient if you know you're only interested in matches in certain fields.
548
+
Next, the search is limited to a single searchable field using the `searchFields` parameter. This approach is a great option to make your query more efficient if you know you're only interested in matches in certain fields.
551
549
552
550
```javascript
553
551
console.log('Query #3 - Limit searchFields:');
@@ -597,7 +595,7 @@ When you're working in your own subscription, it's a good idea at the end of a p
597
595
598
596
You can find and manage resources in the portal, using the **All resources** or **Resource groups** link in the left-navigation pane.
599
597
600
-
If you are using a free service, remember that you are limited to three indexes, indexers, and data sources. You can delete individual items in the portal to stay under the limit.
598
+
If you're using a free service, remember the limit of three indexes, indexers, and data sources. You can delete individual items in the portal to stay under the limit.
0 commit comments