@@ -43,7 +43,7 @@ The quickstart assumes the following is available on your computer:
43
43
npm install @azure/identity @azure/search-documents dotenv
44
44
```
45
45
46
- 1. Rename ` sample.env ` to ` .env` , and provide your search service endpoint. You can get the endpoint from the Azure portal on the search service ** Overview** page.
46
+ 1. Create ` .env` , and provide your search service endpoint. You can get the endpoint from the Azure portal on the search service ** Overview** page.
47
47
48
48
` ` ` ini
49
49
AZURE_SEARCH_ENDPOINT=YOUR-SEARCH-SERVICE-ENDPOINT
@@ -72,8 +72,8 @@ import { DefaultAzureCredential } from "@azure/identity";
72
72
73
73
// Configuration - use environment variables
74
74
export const searchEndpoint = process.env.AZURE_SEARCH_ENDPOINT || " PUT-YOUR-SEARCH-SERVICE-ENDPOINT-HERE" ;
75
- export const searchApiKey = process.env.AZURE_SEARCH_API_KEY || " PUT-YOUR-SEARCH-SERVICE-ADMIN-API-KEY-HERE" ;
76
75
export const indexName = process.env.AZURE_SEARCH_INDEX_NAME || " hotels-sample-index" ;
76
+ export const semanticConfigurationName = process.env.SEMANTIC_CONFIGURATION_NAME || " semantic-config" ;
77
77
78
78
// Create credential
79
79
export const credential = new DefaultAzureCredential ();
@@ -137,30 +137,67 @@ In this section, you get settings for the existing `hotels-sample-index` index o
137
137
import {
138
138
SearchIndexClient
139
139
} from " @azure/search-documents" ;
140
- import { searchEndpoint, indexName, credential } from " ./config.js" ;
140
+ import { searchEndpoint, indexName, credential, semanticConfigurationName } from " ./config.js" ;
141
141
142
- const indexClient = new SearchIndexClient(searchEndpoint, credential) ;
142
+ try {
143
143
144
- console.log( ' Getting semantic search index settings... ' );
144
+ const indexClient = new SearchIndexClient(searchEndpoint, credential );
145
145
146
- // Get the existing schema
147
- const index = await indexClient.getIndex(indexName);
146
+ const existingIndex = await indexClient.getIndex(indexName);
148
147
149
- console.log(` Index name: ${index.name} ` );
150
- console.log(` Number of fields: ${index.fields.length} ` );
148
+ const fields = {
149
+ titleField: {
150
+ name: " HotelName"
151
+ },
152
+ keywordsFields: [{
153
+ name: " Tags"
154
+ }],
155
+ contentFields: [{
156
+ name: " Description"
157
+ }]
158
+ };
159
+
160
+ const newSemanticConfiguration = {
161
+ name: semanticConfigurationName,
162
+ prioritizedFields: fields
163
+ };
164
+
165
+ // Add the new semantic configuration to the existing index
166
+ if (existingIndex.semanticSearch && existingIndex.semanticSearch.configurations) {
167
+ existingIndex.semanticSearch.configurations.push(newSemanticConfiguration);
168
+ } else {
169
+ const configExists = existingIndex.semanticSearch? .configurations? .some(
170
+ config => config.name === semanticConfigurationName
171
+ );
172
+ if (! configExists) {
173
+ existingIndex.semanticSearch = {
174
+ configurations: [newSemanticConfiguration]
175
+ };
176
+ }
177
+ }
151
178
152
- for(const field of index.fields) {
153
- console.log(` Field: ${field.name} , Type: ${field.type} , Searchable: ${field.searchable} ` );
154
- }
179
+ await indexClient.createOrUpdateIndex(existingIndex);
155
180
156
- if(index.semanticSearch && index.semanticSearch.configurations) {
157
- console.log(` Semantic search configurations: ${index.semanticSearch.configurations.length} ` );
158
- for(const config of index.semanticSearch.configurations) {
159
- console.log(` Configuration name: ${config.name} ` );
160
- console.log(` Title field: ${config.prioritizedFields.titleField?.name} ` );
181
+ const updatedIndex = await indexClient.getIndex(indexName);
182
+
183
+ console.log(` Semantic configurations:` );
184
+ console.log(" -" .repeat(40));
185
+
186
+ if (updatedIndex.semanticSearch && updatedIndex.semanticSearch.configurations) {
187
+ for (const config of updatedIndex.semanticSearch.configurations) {
188
+ console.log(` Configuration name: ${config.name} ` );
189
+ console.log(` Title field: ${config.prioritizedFields.titleField?.name} ` );
190
+ console.log(` Keywords fields: ${config.prioritizedFields.keywordsFields?.map(f => f.name).join(" , " )} ` );
191
+ console.log(` Content fields: ${config.prioritizedFields.contentFields?.map(f => f.name).join(" , " )} ` );
192
+ console.log(" -" .repeat(40));
193
+ }
194
+ } else {
195
+ console.log(" No semantic configurations found." );
161
196
}
162
- } else {
163
- console.log(" No semantic configuration exists for this index." );
197
+
198
+ console.log(" Semantic configuration updated successfully." );
199
+ } catch (error) {
200
+ console.error(" Error updating semantic configuration:" , error);
164
201
}
165
202
` ` `
166
203
0 commit comments