@@ -43,7 +43,7 @@ The quickstart assumes the following is available on your computer:
4343 npm install @azure/identity @azure/search-documents dotenv
4444 ```
4545
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.
4747
4848 ` ` ` ini
4949 AZURE_SEARCH_ENDPOINT=YOUR-SEARCH-SERVICE-ENDPOINT
@@ -72,8 +72,8 @@ import { DefaultAzureCredential } from "@azure/identity";
7272
7373// Configuration - use environment variables
7474export 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" ;
7675export const indexName = process.env.AZURE_SEARCH_INDEX_NAME || " hotels-sample-index" ;
76+ export const semanticConfigurationName = process.env.SEMANTIC_CONFIGURATION_NAME || " semantic-config" ;
7777
7878// Create credential
7979export const credential = new DefaultAzureCredential ();
@@ -137,30 +137,67 @@ In this section, you get settings for the existing `hotels-sample-index` index o
137137 import {
138138 SearchIndexClient
139139 } from " @azure/search-documents" ;
140- import { searchEndpoint, indexName, credential } from " ./config.js" ;
140+ import { searchEndpoint, indexName, credential, semanticConfigurationName } from " ./config.js" ;
141141
142- const indexClient = new SearchIndexClient(searchEndpoint, credential) ;
142+ try {
143143
144- console.log( ' Getting semantic search index settings... ' );
144+ const indexClient = new SearchIndexClient(searchEndpoint, credential );
145145
146- // Get the existing schema
147- const index = await indexClient.getIndex(indexName);
146+ const existingIndex = await indexClient.getIndex(indexName);
148147
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+ }
151178
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);
155180
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." );
161196 }
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);
164201 }
165202 ` ` `
166203
0 commit comments