Skip to content

Commit e6ed3b0

Browse files
authored
Add Vector Search Samples and Tests (#35820)
Add Vector Search Samples and Tests
1 parent fd5cbfe commit e6ed3b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+20380
-297
lines changed

eng/code-quality-reports/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Copyright (c) Microsoft Corporation. All rights reserved.
1+
<!-- Copyright (c) Microsoft Corporation. All rights reserved.
22
Licensed under the MIT License. -->
33
<project xmlns="http://maven.apache.org/POM/4.0.0"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

sdk/search/azure-search-documents/CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# Release History
22

3-
## 11.6.0-beta.7 (Unreleased)
3+
## 11.6.0-beta.7 (2023-07-11)
44

55
### Features Added
66

7+
- Added support for [Vector Search](https://learn.microsoft.com/azure/search/vector-search-overview) ([Examples](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/VectorSearchExample.java)).
8+
79
### Breaking Changes
810

9-
### Bugs Fixed
11+
- Deprecated `EntityRecognitionSkillVersion.V1` and `SentimentSkillVersion.V1`, and corresponding constructors in
12+
`EntityRecognitionSkill` and `SentimentSkill`, use `EntityRecognitionSkillVersion.V3` and `SentimentSkillVersion.V3`
13+
instead. See [Cognitive Search skill deprecated](https://learn.microsoft.com/azure/search/cognitive-search-skill-deprecated)
14+
for more details.
1015

1116
### Other Changes
17+
1218
- Migrate test recordings to assets repo.
1319

1420
## 11.5.8 (2023-06-09)

sdk/search/azure-search-documents/README.md

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,20 @@ See [choosing a pricing tier](https://docs.microsoft.com/azure/search/search-sku
9595

9696
### Authenticate the client
9797

98-
In order to interact with the Azure Cognitive Search service you'll need to create an instance of the Search Client class.
99-
To make this possible you will need,
98+
To interact with the Search service, you'll need to create an instance of the appropriate client class: `SearchClient`
99+
for searching indexed documents, `SearchIndexClient` for managing indexes, or `SearchIndexerClient` for crawling data
100+
sources and loading search documents into an index. To instantiate a client object, you'll need an **endpoint** and
101+
**API key**. You can refer to the documentation for more information on [supported authenticating approaches](https://learn.microsoft.com/azure/search/search-security-overview#authentication)
102+
with the Search service.
100103

101-
1. [URL endpoint](https://docs.microsoft.com/azure/search/search-create-service-portal#get-a-key-and-url-endpoint)
102-
1. [API key](https://docs.microsoft.com/azure/search/search-create-service-portal#get-a-key-and-url-endpoint)
104+
#### Get an API Key
103105

104-
for your service. [The api-key is the sole mechanism for authenticating access to
105-
your search service endpoint.](https://docs.microsoft.com/azure/search/search-security-api-keys)
106-
You can obtain your api-key from the [Azure portal](https://portal.azure.com/) or via the Azure CLI:
106+
You can get the **endpoint** and an **API key** from the Search service in the [Azure Portal](https://portal.azure.com/).
107+
Please refer the [documentation](https://docs.microsoft.com/azure/search/search-security-api-keys) for instructions on
108+
how to get an API key.
109+
110+
Alternatively, you can use the following [Azure CLI](https://learn.microsoft.com/cli/azure/) command to retrieve the
111+
API key from the Search service:
107112

108113
```bash
109114
az search admin-key show --service-name <mysearch> --resource-group <mysearch-rg>
@@ -188,6 +193,46 @@ SearchAsyncClient searchAsyncClient = new SearchClientBuilder()
188193
.buildAsyncClient();
189194
```
190195

196+
#### Create a client using Azure Active Directory authentication
197+
198+
You can also create a `SearchClient`, `SearchIndexClient`, or `SearchIndexerClient` using Azure Active Directory (AAD)
199+
authentication. Your user or service principal must be assigned the "Search Index Data Reader" role.
200+
Using the [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity/README.md#defaultazurecredential)
201+
you can authenticate a service using Managed Identity or a service principal, authenticate as a developer working on an
202+
application, and more all without changing code. Please refer the [documentation](https://learn.microsoft.com/azure/search/search-security-rbac?tabs=config-svc-portal%2Croles-portal%2Ctest-portal%2Ccustom-role-portal%2Cdisable-keys-portal)
203+
for instructions on how to connect to Azure Cognitive Search using Azure role-based access control (Azure RBAC).
204+
205+
Before you can use the `DefaultAzureCredential`, or any credential type from [Azure.Identity](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity/README.md),
206+
you'll first need to [install the Azure.Identity package](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity/README.md#include-the-package).
207+
208+
To use `DefaultAzureCredential` with a client ID and secret, you'll need to set the `AZURE_TENANT_ID`,
209+
`AZURE_CLIENT_ID`, and `AZURE_CLIENT_SECRET` environment variables; alternatively, you can pass those values
210+
to the `ClientSecretCredential` also in `azure-identity`.
211+
212+
Make sure you use the right namespace for `DefaultAzureCredential` at the top of your source file:
213+
214+
```java
215+
import com.azure.identity.DefaultAzureCredential;
216+
import com.azure.identity.DefaultAzureCredentialBuilder;
217+
```
218+
219+
Then you can create an instance of `DefaultAzureCredential` and pass it to a new instance of your client:
220+
221+
```java readme-sample-searchClientWithTokenCredential
222+
String indexName = "nycjobs";
223+
224+
// Get the service endpoint from the environment
225+
String endpoint = Configuration.getGlobalConfiguration().get("SEARCH_ENDPOINT");
226+
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
227+
228+
// Create a client
229+
SearchClient client = new SearchClientBuilder()
230+
.endpoint(endpoint)
231+
.indexName(indexName)
232+
.credential(credential)
233+
.buildClient();
234+
```
235+
191236
### Send your first search query
192237

193238
To get running with Azure Cognitive Search first create an index following this [guide][search-get-started-portal].
@@ -220,6 +265,35 @@ tables.)_ The `azure-search-documents` client library exposes operations on thes
220265
* [Start indexers to automatically crawl data sources](https://docs.microsoft.com/rest/api/searchservice/indexer-operations)
221266
* [Define AI powered Skillsets to transform and enrich your data](https://docs.microsoft.com/rest/api/searchservice/skillset-operations)
222267

268+
Azure Cognitive Search provides two powerful features:
269+
270+
### Semantic Search
271+
272+
Semantic search enhances the quality of search results for text-based queries. By enabling Semantic Search on your
273+
search service, you can improve the relevance of search results in two ways:
274+
275+
- It applies secondary ranking to the initial result set, promoting the most semantically relevant results to the top.
276+
- It extracts and returns captions and answers in the response, which can be displayed on a search page to enhance the
277+
user's search experience.
278+
279+
To learn more about Semantic Search, you can refer to the [documentation](https://learn.microsoft.com/azure/search/vector-search-overview).
280+
281+
### Vector Search
282+
283+
Vector Search is an information retrieval technique that overcomes the limitations of traditional keyword-based search.
284+
Instead of relying solely on lexical analysis and matching individual query terms, Vector Search utilizes machine
285+
learning models to capture the contextual meaning of words and phrases. It represents documents and queries as vectors
286+
in a high-dimensional space called an embedding. By understanding the intent behind the query, Vector Search can deliver
287+
more relevant results that align with the user's requirements, even if the exact terms are not present in the document.
288+
Moreover, Vector Search can be applied to various types of content, including images and videos, not just text.
289+
290+
To learn how to index vector fields and perform vector search, you can refer to the [sample](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/VectorSearchExample.java).
291+
This sample provides detailed guidance on indexing vector fields and demonstrates how to perform vector search.
292+
293+
Additionally, for more comprehensive information about Vector Search, including its concepts and usage, you can refer
294+
to the [documentation](https://learn.microsoft.com/azure/search/vector-search-overview). The documentation provides
295+
in-depth explanations and guidance on leveraging the power of Vector Search in Azure Cognitive Search.
296+
223297
## Examples
224298

225299
The following examples all use a simple [Hotel data set](https://github.com/Azure-Samples/azure-search-sample-data)

sdk/search/azure-search-documents/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "java",
44
"TagPrefix": "java/search/azure-search-documents",
5-
"Tag": "java/search/azure-search-documents_67bdd0527a"
5+
"Tag": "java/search/azure-search-documents_501669cb17"
66
}

sdk/search/azure-search-documents/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@
115115
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
116116
<scope>test</scope>
117117
</dependency>
118-
<dependency>
119-
<groupId>org.mockito</groupId>
120-
<artifactId>mockito-core</artifactId>
121-
<version>4.11.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} -->
122-
<scope>test</scope>
123-
</dependency>
124118
<dependency>
125119
<groupId>io.projectreactor</groupId>
126120
<artifactId>reactor-test</artifactId>

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/SearchAsyncClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,15 @@ static SearchRequest createSearchRequest(String searchText, SearchOptions option
10721072
.setSkip(options.getSkip())
10731073
.setTop(options.getTop())
10741074
.setCaptions(createSearchRequestCaptions(options))
1075-
.setSemanticFields(nullSafeStringJoin(options.getSemanticFields()));
1075+
.setSemanticFields(nullSafeStringJoin(options.getSemanticFields()))
1076+
.setSemanticErrorHandling(options.getSemanticErrorHandling())
1077+
.setSemanticMaxWaitInMilliseconds(options.getSemanticMaxWaitInMilliseconds())
1078+
.setDebug(options.getDebug())
1079+
.setVector(options.getVector());
10761080
}
10771081

10781082
static String createSearchRequestAnswers(SearchOptions searchOptions) {
1079-
QueryAnswerType answer = searchOptions.getAnswers();
1083+
QueryAnswerType answer = searchOptions.getQueryAnswer();
10801084
Integer answersCount = searchOptions.getAnswersCount();
10811085
Double answerThreshold = searchOptions.getAnswerThreshold();
10821086

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/SearchServiceVersion.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public enum SearchServiceVersion implements ServiceVersion {
1717
/**
1818
* {@code 2021-04-30-Preview} service version.
1919
*/
20-
V2021_04_30_PREVIEW("2021-04-30-Preview");
20+
V2021_04_30_PREVIEW("2021-04-30-Preview"),
21+
22+
/**
23+
* {@code 2023-07-01-Preview} service version.
24+
*/
25+
V2023_07_01_PREVIEW("2023-07-01-Preview");
2126

2227
private final String version;
2328

@@ -39,6 +44,6 @@ public String getVersion() {
3944
* @return The latest version supported by this client library.
4045
*/
4146
public static SearchServiceVersion getLatest() {
42-
return V2021_04_30_PREVIEW;
47+
return V2023_07_01_PREVIEW;
4348
}
4449
}

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/models/QueryResultDocumentSemanticFieldState.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/models/SearchRequest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.azure.search.documents.models.QueryType;
1818
import com.azure.search.documents.models.ScoringStatistics;
1919
import com.azure.search.documents.models.SearchMode;
20+
import com.azure.search.documents.models.SearchQueryVector;
2021
import com.azure.search.documents.models.SemanticErrorHandling;
2122
import java.io.IOException;
2223
import java.util.List;
@@ -205,7 +206,7 @@ public final class SearchRequest implements JsonSerializable<SearchRequest> {
205206
/*
206207
* The query parameters for vector and hybrid search queries.
207208
*/
208-
private Vector vector;
209+
private SearchQueryVector vector;
209210

210211
/** Creates an instance of SearchRequest class. */
211212
public SearchRequest() {}
@@ -865,7 +866,7 @@ public SearchRequest setSemanticFields(String semanticFields) {
865866
*
866867
* @return the vector value.
867868
*/
868-
public Vector getVector() {
869+
public SearchQueryVector getVector() {
869870
return this.vector;
870871
}
871872

@@ -875,7 +876,7 @@ public Vector getVector() {
875876
* @param vector the vector value to set.
876877
* @return the SearchRequest object itself.
877878
*/
878-
public SearchRequest setVector(Vector vector) {
879+
public SearchRequest setVector(SearchQueryVector vector) {
879880
this.vector = vector;
880881
return this;
881882
}
@@ -995,7 +996,7 @@ public static SearchRequest fromJson(JsonReader jsonReader) throws IOException {
995996
} else if ("semanticFields".equals(fieldName)) {
996997
deserializedSearchRequest.semanticFields = reader.getString();
997998
} else if ("vector".equals(fieldName)) {
998-
deserializedSearchRequest.vector = Vector.fromJson(reader);
999+
deserializedSearchRequest.vector = SearchQueryVector.fromJson(reader);
9991000
} else {
10001001
reader.skipChildren();
10011002
}

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/EntityRecognitionSkill.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ public final class EntityRecognitionSkill extends SearchIndexerSkill {
4444

4545
/**
4646
* Creates an instance of EntityRecognitionSkill class.
47+
* <p>
48+
* The instance of SentimentSkill uses {@link EntityRecognitionSkillVersion#V1}, to set the specific version of the
49+
* skill use {@link #EntityRecognitionSkill(List, List, EntityRecognitionSkillVersion)}.
4750
*
4851
* @param inputs the inputs value to set.
4952
* @param outputs the outputs value to set.
53+
* @deprecated Use {@link #EntityRecognitionSkill(List, List, EntityRecognitionSkillVersion)} as
54+
* {@link EntityRecognitionSkillVersion#V1} is deprecated. See
55+
* <a href="https://learn.microsoft.com/azure/search/cognitive-search-skill-deprecated">skill deprecation</a> for
56+
* more information.
5057
*/
58+
@Deprecated
5159
public EntityRecognitionSkill(List<InputFieldMappingEntry> inputs, List<OutputFieldMappingEntry> outputs) {
5260
this(inputs, outputs, EntityRecognitionSkillVersion.V1);
5361
}

0 commit comments

Comments
 (0)