in demo-java/demo-vectors/src/main/java/azure/search/sample/Main.java [69:194]
public static void main(String[] args) throws InterruptedException {
Dotenv dotenv = Dotenv.load();
String endpoint = dotenv.get("AZURE_SEARCH_ENDPOINT");
String key = dotenv.get("AZURE_SEARCH_ADMIN_KEY");
String indexName = dotenv.get("AZURE_SEARCH_INDEX");
String azureOpenAIEndpoint = dotenv.get("AZURE_OPENAI_ENDPOINT");
String azureOpenAIEmbeddingDeployment = dotenv.get("AZURE_OPENAI_EMBEDDING_DEPLOYMENT");
String azureOpenAIKey = dotenv.get("AZURE_OPENAI_KEY");
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
var searchIndexClientBuilder = new SearchIndexClientBuilder()
.endpoint(endpoint);
if (!key.isBlank()) {
searchIndexClientBuilder = searchIndexClientBuilder.credential(new AzureKeyCredential(key));
} else {
searchIndexClientBuilder = searchIndexClientBuilder.credential(tokenCredential);
}
SearchIndexClient searchIndexClient = searchIndexClientBuilder.buildClient();
// Create the search index, including the new SearchFieldDataType.Single field for vector description.
SearchIndex searchIndex = new SearchIndex(indexName)
.setFields(
new SearchField("id", SearchFieldDataType.STRING)
.setKey(true)
.setFilterable(true)
.setSortable(true),
new SearchField("content", SearchFieldDataType.STRING)
.setSearchable(true),
new SearchField("contentVector", SearchFieldDataType.collection(SearchFieldDataType.SINGLE))
.setSearchable(true)
.setVectorSearchDimensions(1536)
// This must match a vector search configuration name.
.setVectorSearchProfileName("my-vector-profile"),
new SearchField("title", SearchFieldDataType.STRING)
.setSearchable(true),
new SearchField("titleVector", SearchFieldDataType.collection(SearchFieldDataType.SINGLE))
.setSearchable(true)
.setVectorSearchDimensions(1536)
// This must match a vector search configuration name.
.setVectorSearchProfileName("my-vector-profile"),
new SearchField("category", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true))
// VectorSearch configuration is required for a vector field.
// The name used for the vector search algorithm configuration must match the configuration used by the
// search field used for vector search.
.setVectorSearch(new VectorSearch()
.setProfiles(Collections.singletonList(
new VectorSearchProfile("my-vector-profile", "my-vector-config")
.setVectorizer("my-vectorizer")))
.setAlgorithms(Collections.singletonList(
new HnswAlgorithmConfiguration("my-vector-config")))
.setVectorizers(Collections.singletonList(
new AzureOpenAIVectorizer("my-vectorizer")
.setAzureOpenAIParameters(
new AzureOpenAIParameters()
.setResourceUri(azureOpenAIEndpoint)
.setDeploymentId(azureOpenAIEmbeddingDeployment)
.setApiKey(azureOpenAIKey))))
)
.setSemanticSearch(new SemanticSearch().setConfigurations(Arrays.asList(new SemanticConfiguration(
"my-semantic-config", new SemanticPrioritizedFields()
.setContentFields(new SemanticField("content"))))));
searchIndexClient.createOrUpdateIndex(searchIndex);
System.out.println("Created index");
var openAIClientBuilder = new OpenAIClientBuilder()
.endpoint(azureOpenAIEndpoint);
if (!azureOpenAIKey.isBlank()) {
openAIClientBuilder = openAIClientBuilder
.credential(new AzureKeyCredential(azureOpenAIKey));
} else {
openAIClientBuilder = openAIClientBuilder
.credential(tokenCredential);
}
OpenAIClient openAIClient = openAIClientBuilder.buildClient();
// Load JSON file from resources
InputStream inputStream = Main.class.getResourceAsStream("/text-sample.json");
// Parse JSON using org.json library
JSONArray jsonArray = new JSONArray(new Scanner(inputStream).useDelimiter("\\A").next());
var searchClientBuilder = new SearchClientBuilder()
.endpoint(endpoint)
.indexName(indexName);
if (!key.isBlank()) {
searchClientBuilder = searchClientBuilder.credential(new AzureKeyCredential(key));
} else {
searchClientBuilder = searchClientBuilder.credential(tokenCredential);
}
SearchClient searchClient = searchClientBuilder.buildClient();
// Load documents from JSON array
var contentTexts = new ArrayList<String>();
var titleTexts = new ArrayList<String>();
var searchDocuments = new ArrayList<SearchDocument>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
var doc = new SearchDocument();
doc.put("title", jsonObject.getString("title"));
doc.put("id", jsonObject.getString("id"));
doc.put("content", jsonObject.getString("content"));
doc.put("category", jsonObject.getString("category"));
contentTexts.add(jsonObject.getString("content"));
titleTexts.add(jsonObject.getString("title"));
searchDocuments.add(doc);
}
System.out.println("Embedding documents...");
embedTextAndUpdateDocuments(openAIClient, contentTexts, searchDocuments, "contentVector", azureOpenAIEmbeddingDeployment);
embedTextAndUpdateDocuments(openAIClient, titleTexts, searchDocuments, "titleVector", azureOpenAIEmbeddingDeployment);
searchClient.uploadDocuments(searchDocuments);
System.out.println("Pausing after uploading documents...");
// IMPORTANT - Pause for a few seconds to ensure the search service has the latest data
TimeUnit.SECONDS.sleep(2);
singleVectorSearchWithEmbedding(searchClient, openAIClient, "tools for software development", azureOpenAIEmbeddingDeployment);
singleVectorSearch(searchClient, "tools for software development");
singleVectorSearchWithFilter(searchClient, "tools for software development");
simpleHybridSearch(searchClient, "scalable storage solution");
semanticHybridSearch(searchClient, "scalable storage solution");
semanticFullSearch(searchClient, "title:Azure AND \"scalable\"^3", "Azure scalable");
multiVectorSearch(searchClient, "tools for software development");
}