-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CreateIndexExample.java
83 lines (75 loc) · 3.63 KB
/
CreateIndexExample.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.documents.indexes;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.Configuration;
import com.azure.search.documents.indexes.models.LexicalAnalyzerName;
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 java.util.Arrays;
public class CreateIndexExample {
/**
* From the Azure portal, get your Azure AI Search service name and API key and populate ADMIN_KEY and
* SEARCH_SERVICE_NAME.
*/
private static final String ENDPOINT = Configuration.getGlobalConfiguration().get("AZURE_COGNITIVE_SEARCH_ENDPOINT");
private static final String ADMIN_KEY = Configuration.getGlobalConfiguration().get("AZURE_COGNITIVE_SEARCH_API_KEY");
public static void main(String[] args) {
// Create the SearchIndex client.
SearchIndexClient client = new SearchIndexClientBuilder()
.endpoint(ENDPOINT)
.credential(new AzureKeyCredential(ADMIN_KEY))
.buildClient();
// Configure the index using SearchFields
String indexName = "hotels";
SearchIndex newIndex = new SearchIndex(indexName, Arrays.asList(
new SearchField("hotelId", SearchFieldDataType.STRING)
.setKey(true)
.setFilterable(true)
.setSortable(true),
new SearchField("hotelName", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setSortable(true),
new SearchField("description", SearchFieldDataType.STRING)
.setSearchable(true)
.setAnalyzerName(LexicalAnalyzerName.EN_LUCENE),
new SearchField("descriptionFr", SearchFieldDataType.STRING)
.setSearchable(true)
.setAnalyzerName(LexicalAnalyzerName.FR_LUCENE),
new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING))
.setSearchable(true)
.setFilterable(true)
.setFacetable(true),
new SearchField("address", SearchFieldDataType.COMPLEX)
.setFields(
new SearchField("streetAddress", SearchFieldDataType.STRING)
.setSearchable(true),
new SearchField("city", SearchFieldDataType.STRING)
.setFilterable(true)
.setSortable(true)
.setFacetable(true),
new SearchField("stateProvince", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setSortable(true)
.setFacetable(true),
new SearchField("country", SearchFieldDataType.STRING)
.setSearchable(true)
.setSynonymMapNames("synonymMapName")
.setFilterable(true)
.setSortable(true)
.setFacetable(true),
new SearchField("postalCode", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setSortable(true)
.setFacetable(true))
));
// Create index.
client.createIndex(newIndex);
// Cleanup index resource.
client.deleteIndex(indexName);
}
}