forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/synonym maps sample (Azure#242)
* Samples for managing synonym maps * Update SynonymMapsExample.java * Update SynonymMapsExample.java * Update SynonymMapsExample.java * Update SynonymMapsExample.java * Removed cleanup resources * Removed CURD operations. Build only create synonym map * Update SynonymMapsCreateExample.java * Update SynonymMapsCreateExample.java * Update SynonymMapsCreateExample.java
- Loading branch information
1 parent
554b4be
commit 5c14557
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
sdk/search/azure-search/src/samples/java/com/azure/search/SynonymMapsCreateExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.search; | ||
|
||
import com.azure.core.util.Configuration; | ||
import com.azure.search.models.SynonymMap; | ||
import com.azure.search.models.Index; | ||
import com.azure.search.models.Field; | ||
import com.azure.search.models.DataType; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
/** | ||
* This example shows how to create an index with a synonym map | ||
* See https://docs.microsoft.com/en-us/azure/search/search-get-started-portal | ||
*/ | ||
public class SynonymMapsCreateExample { | ||
|
||
/** | ||
* From the Azure portal, get your Azure Cognitive Search service URL and API key, | ||
* and set the values of these environment variables: | ||
*/ | ||
private static final String ENDPOINT = Configuration.getGlobalConfiguration().get("AZURE_SEARCH_ENDPOINT"); | ||
private static final String API_ADMIN_KEY = Configuration.getGlobalConfiguration().get("AZURE_SEARCH_API_KEY"); | ||
|
||
public static void main(String[] args) | ||
{ | ||
SearchServiceClient serviceClient = new SearchServiceClientBuilder() | ||
.endpoint(ENDPOINT) | ||
.credential(new ApiKeyCredentials(API_ADMIN_KEY)) | ||
.buildClient(); | ||
|
||
String synonymMapName = "desc-synonymmap"; | ||
|
||
System.out.println("Create synonym map...\n"); | ||
createSynonymMap(serviceClient,synonymMapName); | ||
|
||
System.out.println("Create index and assign synonym to it...\n"); | ||
assignSynonymMapToIndex(synonymMapName); | ||
|
||
System.out.println("Complete....\n"); | ||
} | ||
|
||
private static void createSynonymMap(SearchServiceClient serviceClient, String synonymMapName) | ||
{ | ||
SynonymMap synonymMap = new SynonymMap() | ||
.setName(synonymMapName) | ||
.setSynonyms("hotel, motel\ninternet,wifi\nfive star=>luxury\neconomy,inexpensive=>budget"); | ||
serviceClient.createSynonymMap(synonymMap); | ||
} | ||
|
||
private static void assignSynonymMapToIndex(String synonymMapName) | ||
{ | ||
Index index = new Index() | ||
.setName("hotels") | ||
.setFields(Arrays.asList( | ||
new Field() | ||
.setName("HotelId") | ||
.setType(DataType.EDM_STRING) | ||
.setKey(true), | ||
new Field() | ||
.setName("HotelName") | ||
.setType(DataType.EDM_STRING) | ||
.setSynonymMaps(Collections.singletonList(synonymMapName)) | ||
)); | ||
} | ||
} |