/
SynonymMapsCreateExample.java
66 lines (53 loc) · 2.77 KB
/
SynonymMapsCreateExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.documents;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.Configuration;
import com.azure.search.documents.indexes.SearchIndexClient;
import com.azure.search.documents.indexes.SearchIndexClientBuilder;
import com.azure.search.documents.indexes.models.SearchField;
import com.azure.search.documents.indexes.models.SearchFieldDataType;
import com.azure.search.documents.indexes.models.SearchIndex;
import com.azure.search.documents.indexes.models.SynonymMap;
import java.util.Arrays;
/**
* This example shows how to create an index with a synonym map
* See <a href="https://docs.microsoft.com/azure/search/search-get-started-portal">Search getting started portal</a>
*/
public class SynonymMapsCreateExample {
/**
* From the Azure portal, get your Azure AI Search service URL and API key,
* and set the values of these environment variables:
*/
private static final String ENDPOINT = Configuration.getGlobalConfiguration().get("AZURE_COGNITIVE_SEARCH_ENDPOINT");
private static final String API_ADMIN_KEY = Configuration.getGlobalConfiguration().get("AZURE_COGNITIVE_SEARCH_ADMIN_KEY");
public static void main(String[] args) {
SearchIndexClient serviceClient = new SearchIndexClientBuilder()
.endpoint(ENDPOINT)
.credential(new AzureKeyCredential(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 map to it...\n");
SearchIndex searchIndex = assignSynonymMapToIndex(synonymMapName);
System.out.println("Assigned synonym map: " + searchIndex.getFields().stream()
.filter(field -> field.getSynonymMapNames() != null)
.findFirst().get());
System.out.println("Complete....\n");
// Clean up resources
serviceClient.deleteSynonymMap(synonymMapName);
}
private static void createSynonymMap(SearchIndexClient serviceClient, String synonymMapName) {
SynonymMap synonymMap = new SynonymMap(synonymMapName,
"hotel, motel\ninternet,wifi\nfive star=>luxury\neconomy,inexpensive=>budget");
serviceClient.createSynonymMap(synonymMap);
}
private static SearchIndex assignSynonymMapToIndex(String synonymMapName) {
return new SearchIndex("hotels", Arrays.asList(
new SearchField("HotelId", SearchFieldDataType.STRING)
.setKey(true),
new SearchField("HotelName", SearchFieldDataType.STRING)
.setSynonymMapNames(synonymMapName)));
}
}