Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
79 changes: 40 additions & 39 deletions sdk/search/azure-search-documents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,9 @@ Let's explore them with a search for a "luxury" hotel.
enumerate over the results, and extract data using `SearchDocument`'s dictionary indexer.

```java readme-sample-searchWithDynamicType
for (SearchResult searchResult : SEARCH_CLIENT.search("luxury")) {
SearchDocument doc = searchResult.getDocument(SearchDocument.class);
String id = (String) doc.get("hotelId");
String name = (String) doc.get("hotelName");
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", id, name);
for (SearchResult searchResult : SEARCH_CLIENT.search(new SearchOptions().setSearchText("luxury"))) {
Map<String, Object> doc = searchResult.getAdditionalProperties();
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", doc.get("HotelId"), doc.get("HotelName"));
}
```

Expand All @@ -330,9 +328,9 @@ Define a `Hotel` class.

```java readme-sample-hotelclass
public static class Hotel {
@SimpleField(isKey = true, isFilterable = true, isSortable = true)
@SimpleField(name = "Id", isKey = true, isFilterable = true, isSortable = true)
private String id;
@SearchableField(isFilterable = true, isSortable = true)
@SearchableField(name = "Name", isFilterable = true, isSortable = true)
private String name;

public String getId() {
Expand All @@ -358,11 +356,9 @@ public static class Hotel {
Use it in place of `SearchDocument` when querying.

```java readme-sample-searchWithStronglyType
for (SearchResult searchResult : SEARCH_CLIENT.search("luxury")) {
Hotel doc = searchResult.getDocument(Hotel.class);
String id = doc.getId();
String name = doc.getName();
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", id, name);
for (SearchResult searchResult : SEARCH_CLIENT.search(new SearchOptions().setSearchText("luxury"))) {
Map<String, Object> doc = searchResult.getAdditionalProperties();
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", doc.get("Id"), doc.get("Name"));
}
```

Expand All @@ -375,11 +371,11 @@ The `SearchOptions` provide powerful control over the behavior of our queries.
Let's search for the top 5 luxury hotels with a good rating.

```java readme-sample-searchWithSearchOptions
SearchOptions options = new SearchOptions()
SearchOptions options = new SearchOptions().setSearchText("luxury")
.setFilter("rating ge 4")
.setOrderBy("rating desc")
.setTop(5);
SearchPagedIterable searchResultsIterable = SEARCH_CLIENT.search("luxury", options, Context.NONE);
SearchPagedIterable searchResultsIterable = SEARCH_CLIENT.search(options);
// ...
```

Expand All @@ -394,58 +390,56 @@ There are multiple ways of preparing search fields for a search index. For basic
to configure the field of model class.

```java readme-sample-createIndexUseFieldBuilder
List<SearchField> searchFields = SearchIndexClient.buildSearchFields(Hotel.class, null);
List<SearchField> searchFields = SearchIndexClient.buildSearchFields(Hotel.class);
SEARCH_INDEX_CLIENT.createIndex(new SearchIndex("index", searchFields));
```

For advanced scenarios, we can build search fields using `SearchField` directly.

```java readme-sample-createIndex
List<SearchField> searchFieldList = new ArrayList<>();
searchFieldList.add(new SearchField("hotelId", SearchFieldDataType.STRING)
searchFieldList.add(new SearchField("HotelId", SearchFieldDataType.STRING)
.setKey(true)
.setFilterable(true)
.setSortable(true));

searchFieldList.add(new SearchField("hotelName", SearchFieldDataType.STRING)
searchFieldList.add(new SearchField("HotelName", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setSortable(true));
searchFieldList.add(new SearchField("description", SearchFieldDataType.STRING)
searchFieldList.add(new SearchField("Description", SearchFieldDataType.STRING)
.setSearchable(true)
.setAnalyzerName(LexicalAnalyzerName.EU_LUCENE));
searchFieldList.add(new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING))
searchFieldList.add(new SearchField("Tags", SearchFieldDataType.collection(SearchFieldDataType.STRING))
.setSearchable(true)
.setFilterable(true)
.setFacetable(true));
searchFieldList.add(new SearchField("address", SearchFieldDataType.COMPLEX)
.setFields(new SearchField("streetAddress", SearchFieldDataType.STRING).setSearchable(true),
new SearchField("city", SearchFieldDataType.STRING)
searchFieldList.add(new SearchField("Address", SearchFieldDataType.COMPLEX)
.setFields(new SearchField("StreetAddress", SearchFieldDataType.STRING).setSearchable(true),
new SearchField("City", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setFacetable(true)
.setSortable(true),
new SearchField("stateProvince", SearchFieldDataType.STRING)
new SearchField("StateProvince", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setFacetable(true)
.setSortable(true),
new SearchField("country", SearchFieldDataType.STRING)
new SearchField("Country", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setFacetable(true)
.setSortable(true),
new SearchField("postalCode", SearchFieldDataType.STRING)
new SearchField("PostalCode", SearchFieldDataType.STRING)
.setSearchable(true)
.setFilterable(true)
.setFacetable(true)
.setSortable(true)
));
.setSortable(true)));

// Prepare suggester.
SearchSuggester suggester = new SearchSuggester("sg", Collections.singletonList("hotelName"));
SearchSuggester suggester = new SearchSuggester("sg", "hotelName");
// Prepare SearchIndex with index name and search fields.
SearchIndex index = new SearchIndex("hotels").setFields(searchFieldList).setSuggesters(suggester);
SearchIndex index = new SearchIndex("hotels", searchFieldList).setSuggesters(suggester);
// Create an index
SEARCH_INDEX_CLIENT.createIndex(index);
```
Expand All @@ -457,8 +451,8 @@ your index if you already know the key. You could get the key from a query, for
information about it or navigate your customer to that document.

```java readme-sample-retrieveDocuments
Hotel hotel = SEARCH_CLIENT.getDocument("1", Hotel.class);
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", hotel.getId(), hotel.getName());
Map<String, Object> hotel = SEARCH_CLIENT.getDocument("1").getAdditionalProperties();
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", hotel.get("Id"), hotel.get("Name"));
```

### Adding documents to your index
Expand All @@ -468,9 +462,16 @@ There are [a few special rules for merging](https://learn.microsoft.com/rest/api
to be aware of.

```java readme-sample-batchDocumentsOperations
IndexDocumentsBatch<Hotel> batch = new IndexDocumentsBatch<>();
batch.addUploadActions(Collections.singletonList(new Hotel().setId("783").setName("Upload Inn")));
batch.addMergeActions(Collections.singletonList(new Hotel().setId("12").setName("Renovated Ranch")));
Map<String, Object> hotel = new LinkedHashMap<>();
hotel.put("Id", "783");
hotel.put("Name", "Upload Inn");

Map<String, Object> hotel2 = new LinkedHashMap<>();
hotel2.put("Id", "12");
hotel2.put("Name", "Renovated Ranch");
IndexDocumentsBatch batch = new IndexDocumentsBatch(
new IndexAction().setActionType(IndexActionType.UPLOAD).setAdditionalProperties(hotel),
new IndexAction().setActionType(IndexActionType.MERGE).setAdditionalProperties(hotel2));
SEARCH_CLIENT.indexDocuments(batch);
```

Expand All @@ -484,10 +485,10 @@ The examples so far have been using synchronous APIs, but we provide full suppor
to use [SearchAsyncClient](#create-a-searchclient).

```java readme-sample-searchWithAsyncClient
SEARCH_ASYNC_CLIENT.search("luxury")
SEARCH_ASYNC_CLIENT.search(new SearchOptions().setSearchText("luxury"))
.subscribe(result -> {
Hotel hotel = result.getDocument(Hotel.class);
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", hotel.getId(), hotel.getName());
Map<String, Object> hotel = result.getAdditionalProperties();
System.out.printf("This is hotelId %s, and this is hotel name %s.%n", hotel.get("Id"), hotel.get("Name"));
});
```

Expand Down Expand Up @@ -528,7 +529,7 @@ Any Search API operation that fails will throw an [`HttpResponseException`][Http

```java readme-sample-handleErrorsWithSyncClient
try {
Iterable<SearchResult> results = SEARCH_CLIENT.search("hotel");
Iterable<SearchResult> results = SEARCH_CLIENT.search(new SearchOptions().setSearchText("hotel"));
} catch (HttpResponseException ex) {
// The exception contains the HTTP status code and the detailed message
// returned from the search service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<!-- This file is generated by the /eng/scripts/linting_suppression_generator.py script. -->

<suppressions>
<suppress files="com.azure.search.documents.knowledgebases.SearchKnowledgeBaseAsyncClient.java" checks="JavadocPackageCheck" />
<suppress files="com.azure.search.documents.test.environment.models.Book.java" checks="MemberNameCheck" />
<suppress files="com.azure.search.documents.test.environment.models.LoudHotel.java" checks="MemberNameCheck" />
<suppress files="com.azure.search.documents.test.environment.models.Book.java" checks="MethodNameCheck" />
Expand Down
21 changes: 21 additions & 0 deletions sdk/search/azure-search-documents/customizations/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.azure</groupId>
<artifactId>azure-code-customization-parent</artifactId>
<version>1.0.0-beta.1</version> <!-- {x-version-update;com.azure:azure-code-customization-parent;current} -->
<relativePath>../../../parents/azure-code-customization-parent</relativePath>
</parent>

<name>Microsoft Azure AI Search client for Java</name>
<description>This package contains client functionality for Microsoft Azure AI Search</description>

<groupId>com.azure.tools</groupId>
<artifactId>azure-search-documents-autorest-customization</artifactId>
<version>1.0.0-beta.1</version>
<packaging>jar</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import com.azure.autorest.customization.Customization;
import com.azure.autorest.customization.LibraryCustomization;
import com.github.javaparser.ast.body.MethodDeclaration;
import org.slf4j.Logger;

/**
* Contains customizations for Azure AI Search code generation.
*/
public class SearchCustomizations extends Customization {
@Override
public void customize(LibraryCustomization libraryCustomization, Logger logger) {
// Make generated search APIs in SearchClient and SearchAsyncClient non-public
libraryCustomization.getClass("com.azure.search.documents", "SearchClient")
.customizeAst(ast -> ast.getClassByName("SearchClient").ifPresent(clazz ->
clazz.getMethodsByName("searchWithResponse").forEach(MethodDeclaration::setModifiers)));
libraryCustomization.getClass("com.azure.search.documents", "SearchAsyncClient")
.customizeAst(ast -> ast.getClassByName("SearchAsyncClient").ifPresent(clazz ->
clazz.getMethodsByName("searchWithResponse").forEach(MethodDeclaration::setModifiers)));
}
}
32 changes: 7 additions & 25 deletions sdk/search/azure-search-documents/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

<FindBugsFilter xmlns="https://github.com/spotbugs/filter/3.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
<Match>
<Bug pattern="BC_VACUOUS_INSTANCEOF" />
<Class name="com.azure.search.documents.KnowledgeSourceTests" />
</Match>
<Match>
<Bug pattern="DCN_NULLPOINTER_EXCEPTION" />
<Class name="com.azure.search.documents.KnowledgeSourceTests" />
</Match>
<Match>
<Bug pattern="DLS_DEAD_LOCAL_STORE" />
<Or>
<Class name="com.azure.search.documents.KnowledgeSourceTests" />
<Class name="com.azure.search.documents.ReadmeSamples" />
<Class name="com.azure.search.documents.SearchJavaDocCodeSnippets" />
</Or>
Expand All @@ -31,37 +26,24 @@
<Class name="com.azure.search.documents.KnowledgeSourceTests" />
</Match>
<Match>
<Bug pattern="EC_UNRELATED_TYPES" />
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" />
<Or>
<Class name="com.azure.search.documents.KnowledgeSourceTests" />
<Class name="com.azure.search.documents.indexes.IndexManagementTests" />
<Class name="com.azure.search.documents.indexes.IndexersManagementTests" />
<Class name="com.azure.search.documents.indexes.SkillsetManagementTests" />
</Or>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_STATIC_REP2" />
<Or>
<Class name="com.azure.search.documents.implementation.converters.IndexActionHelper" />
<Class name="com.azure.search.documents.implementation.converters.SearchResultHelper" />
<Class name="com.azure.search.documents.implementation.converters.SuggestResultHelper" />
</Or>
</Match>
<Match>
<Bug pattern="NP_BOOLEAN_RETURN_NULL" />
<Class name="com.azure.search.documents.indexes.models.EntityRecognitionSkill" />
<Bug pattern="NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS" />
<Class name="com.azure.search.documents.indexes.NonRestCallTests" />
</Match>
<Match>
<Bug pattern="SS_SHOULD_BE_STATIC" />
<Or>
<Class name="com.azure.search.documents.indexes.SearchIndexClientBuilderTests" />
<Class name="com.azure.search.documents.indexes.SearchIndexerClientBuilderTests" />
<Class name="com.azure.search.documents.indexes.models.SearchSuggester" />
<Class name="com.azure.search.documents.indexes.models.SynonymMap" />
</Or>
</Match>
<Match>
<Bug pattern="UC_USELESS_OBJECT" />
<Class name="com.azure.search.documents.indexes.SkillsetManagementTests" />
</Match>
<Match>
<Bug pattern="UWF_UNWRITTEN_FIELD" />
<Class name="com.azure.search.documents.test.environment.models.HotelWithUnsupportedField" />
</Match>
</FindBugsFilter>
Loading
Loading