/
CreateSkillsetExample.java
118 lines (93 loc) · 5.08 KB
/
CreateSkillsetExample.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// 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.InputFieldMappingEntry;
import com.azure.search.documents.indexes.models.OcrSkill;
import com.azure.search.documents.indexes.models.OutputFieldMappingEntry;
import com.azure.search.documents.indexes.models.SearchIndexerSkill;
import com.azure.search.documents.indexes.models.SearchIndexerSkillset;
import com.azure.search.documents.indexes.models.WebApiSkill;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class CreateSkillsetExample {
/**
* 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 ADMIN_KEY = Configuration.getGlobalConfiguration().get("AZURE_COGNITIVE_SEARCH_ADMIN_KEY");
private static final String OCR_SKILLSET_NAME = "ocr-skillset";
private static final String CUSTOM_SKILLSET_NAME = "custom-skillset";
public static void main(String[] args) {
SearchIndexerClient searchIndexerClient = new SearchIndexerClientBuilder()
.endpoint(ENDPOINT)
.credential(new AzureKeyCredential(ADMIN_KEY))
.buildClient();
createOcrSkillset(searchIndexerClient);
createCustomSkillset(searchIndexerClient);
cleanupSkillset(searchIndexerClient);
}
private static void createOcrSkillset(SearchIndexerClient searchIndexerClient) {
// Sample OCR definition
// https://docs.microsoft.com/azure/search/cognitive-search-skill-ocr#sample-definition
List<InputFieldMappingEntry> inputs = Collections.singletonList(
new InputFieldMappingEntry("image")
.setSource("/document/normalized_images/*")
);
List<OutputFieldMappingEntry> outputs = Arrays.asList(
new OutputFieldMappingEntry("text")
.setTargetName("mytext"),
new OutputFieldMappingEntry("layoutText")
.setTargetName("myLayoutText")
);
List<SearchIndexerSkill> skills = Collections.singletonList(
new OcrSkill(inputs, outputs)
.setShouldDetectOrientation(true)
.setDefaultLanguageCode(null)
.setName("myocr")
.setDescription("Extracts text (plain and structured) from image.")
.setContext("/document/normalized_images/*")
);
SearchIndexerSkillset skillset = new SearchIndexerSkillset(OCR_SKILLSET_NAME, skills)
.setDescription("Extracts text (plain and structured) from image.");
System.out.println(String.format("Creating OCR skillset '%s'", skillset.getName()));
SearchIndexerSkillset createdSkillset = searchIndexerClient.createSkillset(skillset);
System.out.println("Created OCR skillset");
System.out.println(String.format("Name: %s", createdSkillset.getName()));
System.out.println(String.format("ETag: %s", createdSkillset.getETag()));
System.out.println("\n");
}
private static void createCustomSkillset(SearchIndexerClient searchIndexerClient) {
HashMap<String, String> headers = new HashMap<>();
headers.put("Ocp-Apim-Subscription-Key", "foobar");
List<InputFieldMappingEntry> inputs = Collections.singletonList(
new InputFieldMappingEntry("text")
.setSource("/document/mytext")
);
List<OutputFieldMappingEntry> outputs = Collections.singletonList(
new OutputFieldMappingEntry("textItems")
.setTargetName("myTextItems")
);
SearchIndexerSkill webApiSkill = new WebApiSkill(inputs, outputs, "https://example.com")
.setHttpMethod("POST") // Supports only "POST" and "PUT" HTTP methods
.setHttpHeaders(headers)
.setName("webapi-skill")
.setDescription("A WebApiSkill that can be used to call a custom web api function");
SearchIndexerSkillset skillset = new SearchIndexerSkillset(CUSTOM_SKILLSET_NAME,
Collections.singletonList(webApiSkill))
.setDescription("Skillset for testing custom skillsets");
System.out.println(String.format("Creating custom skillset '%s'", skillset.getName()));
SearchIndexerSkillset createdSkillset = searchIndexerClient.createSkillset(skillset);
System.out.println("Created custom skillset");
System.out.println(String.format("Name: %s", createdSkillset.getName()));
System.out.println(String.format("ETag: %s", createdSkillset.getETag()));
}
private static void cleanupSkillset(SearchIndexerClient searchIndexerClient) {
searchIndexerClient.deleteSkillset(OCR_SKILLSET_NAME);
searchIndexerClient.deleteSkillset(CUSTOM_SKILLSET_NAME);
}
}