Class SearchAsyncClient
Overview
Conceptually, a document is an entity in your index. Mapping this concept to more familiar database equivalents: a search index equates to a table, and documents are roughly equivalent to rows in a table. Documents exist only in an index, and are retrieved only through queries that target the documents collection (/docs) of an index. All operations performed on the collection such as uploading, merging, deleting, or querying documents take place in the context of a single index, so the URL format document operations will always include /indexes/[index name]/docs for a given index name.
This client provides an asynchronous API for accessing and performing operations on indexed documents. This client assists with searching your indexed documents, autocompleting partially typed search terms based on documents within the index, suggesting the most likely matching text in documents as a user types. The client provides operations for adding, updating, and deleting documents from an index.
Getting Started
Authenticating and building instances of this client are handled by SearchClientBuilder. This sample shows
you how to authenticate and create an instance of the client:
SearchAsyncClient searchAsyncClient = new SearchClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.indexName("{indexName}")
.buildAsyncClient();
For more information on authentication and building, see the SearchClientBuilder documentation.
Examples
The following examples all use a simple Hotel data set that you can import into your own index from the Azure portal. These are just a few of the basics - please check out our Samples for much more.
Upload a Document
The following sample uploads a new document to an index.
List<Hotel> hotels = new ArrayList<>();
hotels.add(new Hotel().setHotelId("100"));
hotels.add(new Hotel().setHotelId("200"));
hotels.add(new Hotel().setHotelId("300"));
searchAsyncClient.uploadDocuments(hotels).block();
For a synchronous sample see SearchClient.uploadDocuments(Iterable).
Merge a Document
The following sample merges documents in an index.
List<Hotel> hotels = new ArrayList<>();
hotels.add(new Hotel().setHotelId("100"));
hotels.add(new Hotel().setHotelId("200"));
searchAsyncClient.mergeDocuments(hotels).block();
For a synchronous sample see SearchClient.mergeDocuments(Iterable).
Delete a Document
The following sample deletes a document from an index.
SearchDocument documentId = new SearchDocument();
documentId.put("hotelId", "100");
searchAsyncClient.deleteDocuments(Collections.singletonList(documentId));
For a synchronous sample see SearchClient.deleteDocuments(Iterable).
Get a Document
The following sample gets a document from an index.
Hotel hotel = searchAsyncClient.getDocument("100", Hotel.class).block();
if (hotel != null) {
System.out.printf("Retrieved Hotel %s%n", hotel.getHotelId());
}
For a synchronous sample see SearchClient.getDocument(String, Class).
Search Documents
The following sample searches for documents within an index.
SearchDocument searchDocument = new SearchDocument();
searchDocument.put("hotelId", "8");
searchDocument.put("description", "budget");
searchDocument.put("descriptionFr", "motel");
SearchDocument searchDocument1 = new SearchDocument();
searchDocument1.put("hotelId", "9");
searchDocument1.put("description", "budget");
searchDocument1.put("descriptionFr", "motel");
List<SearchDocument> searchDocuments = new ArrayList<>();
searchDocuments.add(searchDocument);
searchDocuments.add(searchDocument1);
searchAsyncClient.uploadDocuments(searchDocuments);
SearchPagedFlux results = searchAsyncClient.search("SearchText");
results.getTotalCount().subscribe(total -> System.out.printf("There are %s results", total));
For a synchronous sample see SearchClient.search(String).
Make a Suggestion
The following sample suggests the most likely matching text in documents.
SuggestPagedFlux results = searchAsyncClient.suggest("searchText", "sg");
results.subscribe(item -> {
System.out.printf("The text '%s' was found.%n", item.getText());
});
For a synchronous sample see SearchClient.suggest(String, String).
Provide an Autocompletion
The following sample provides autocompletion for a partially typed query.
AutocompletePagedFlux results = searchAsyncClient.autocomplete("searchText", "sg");
results.subscribe(item -> {
System.out.printf("The text '%s' was found.%n", item.getText());
});
For a synchronous sample see SearchClient.autocomplete(String, String).
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionautocomplete(String searchText, String suggesterName) Autocompletes incomplete query terms based on input text and matching terms in the index.autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions) Autocompletes incomplete query terms based on input text and matching terms in the index.deleteDocuments(Iterable<?> documents) Deletes a collection of documents from the target index.Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Deletes a collection of documents from the target index.<T> Mono<T> getDocument(String key, Class<T> modelClass) Retrieves a document from the Azure AI Search index.Queries the number of documents in the search index.Queries the number of documents in the search index.<T> Mono<com.azure.core.http.rest.Response<T>> getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields) Retrieves a document from the Azure AI Search index.Gets the endpoint for the Azure AI Search service.Gets the name of the Azure AI Search index.indexDocuments(IndexDocumentsBatch<?> batch) Sends a batch of upload, merge, and/or delete actions to the search index.Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options) Sends a batch of upload, merge, and/or delete actions to the search index.mergeDocuments(Iterable<?> documents) Merges a collection of documents with existing documents in the target index.Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Merges a collection of documents with existing documents in the target index.mergeOrUploadDocuments(Iterable<?> documents) This action behaves like merge if a document with the given key already exists in the index.Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) This action behaves like merge if a document with the given key already exists in the index.Searches for documents in the Azure AI Search index.search(String searchText, SearchOptions searchOptions) Searches for documents in the Azure AI Search index.Suggests documents in the index that match the given partial query.suggest(String searchText, String suggesterName, SuggestOptions suggestOptions) Suggests documents in the index that match the given partial query.uploadDocuments(Iterable<?> documents) Uploads a collection of documents to the target index.Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Uploads a collection of documents to the target index.
-
Method Details
-
getIndexName
Gets the name of the Azure AI Search index.- Returns:
- the indexName value.
-
getEndpoint
Gets the endpoint for the Azure AI Search service.- Returns:
- the endpoint value.
-
uploadDocuments
Uploads a collection of documents to the target index.Code Sample
Upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); SEARCH_ASYNC_CLIENT.uploadDocuments(Collections.singletonList(searchDocument)) .subscribe(result -> { for (IndexingResult indexingResult : result.getResults()) { System.out.printf("Does document with key %s upload successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to upload to the target Index.- Returns:
- The result of the document indexing actions.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
uploadDocumentsWithResponse
public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Uploads a collection of documents to the target index.Code Sample
Upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.uploadDocumentsWithResponse(Collections.singletonList(searchDocument), null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (IndexingResult indexingResult : resultResponse.getValue().getResults()) { System.out.printf("Does document with key %s upload successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to upload to the target Index.options- Options that allow specifying document indexing behavior.- Returns:
- A response containing the result of the document indexing actions.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
mergeDocuments
Merges a collection of documents with existing documents in the target index.If the type of the document contains non-nullable primitive-typed properties, these properties may not merge correctly. If you do not set such a property, it will automatically take its default value (for example,
0forintor false forboolean), which will override the value of the property currently stored in the index, even if this was not your intent. For this reason, it is strongly recommended that you always declare primitive-typed properties with their class equivalents (for example, an integer property should be of typeIntegerinstead ofint).Code Sample
Merge dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelName", "merge"); SEARCH_ASYNC_CLIENT.mergeDocuments(Collections.singletonList(searchDocument)) .subscribe(result -> { for (IndexingResult indexingResult : result.getResults()) { System.out.printf("Does document with key %s merge successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to be merged- Returns:
- document index result
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
mergeDocumentsWithResponse
public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Merges a collection of documents with existing documents in the target index.If the type of the document contains non-nullable primitive-typed properties, these properties may not merge correctly. If you do not set such a property, it will automatically take its default value (for example,
0forintor false forboolean), which will override the value of the property currently stored in the index, even if this was not your intent. For this reason, it is strongly recommended that you always declare primitive-typed properties with their class equivalents (for example, an integer property should be of typeIntegerinstead ofint).Code Sample
Merge dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelName", "test"); searchAsyncClient.mergeDocumentsWithResponse(Collections.singletonList(searchDocument), null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (IndexingResult indexingResult : resultResponse.getValue().getResults()) { System.out.printf("Does document with key %s merge successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to be mergedoptions- Options that allow specifying document indexing behavior.- Returns:
- response containing the document index result.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
mergeOrUploadDocuments
This action behaves like merge if a document with the given key already exists in the index. If the document does not exist, it behaves like upload with a new document.If the type of the document contains non-nullable primitive-typed properties, these properties may not merge correctly. If you do not set such a property, it will automatically take its default value (for example,
0forintor false forboolean), which will override the value of the property currently stored in the index, even if this was not your intent. For this reason, it is strongly recommended that you always declare primitive-typed properties with their class equivalents (for example, an integer property should be of typeIntegerinstead ofint).Code Sample
Merge or upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); SEARCH_ASYNC_CLIENT.mergeOrUploadDocuments(Collections.singletonList(searchDocument)) .subscribe(result -> { for (IndexingResult indexingResult : result.getResults()) { System.out.printf("Does document with key %s mergeOrUpload successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to be merged, if exists, otherwise uploaded- Returns:
- document index result
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
mergeOrUploadDocumentsWithResponse
public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) This action behaves like merge if a document with the given key already exists in the index. If the document does not exist, it behaves like upload with a new document.If the type of the document contains non-nullable primitive-typed properties, these properties may not merge correctly. If you do not set such a property, it will automatically take its default value (for example,
0forintor false forboolean), which will override the value of the property currently stored in the index, even if this was not your intent. For this reason, it is strongly recommended that you always declare primitive-typed properties with their class equivalents (for example, an integer property should be of typeIntegerinstead ofint).Code Sample
Merge or upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.mergeOrUploadDocumentsWithResponse(Collections.singletonList(searchDocument), null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (IndexingResult indexingResult : resultResponse.getValue().getResults()) { System.out.printf("Does document with key %s mergeOrUpload successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to be merged, if exists, otherwise uploadedoptions- Options that allow specifying document indexing behavior.- Returns:
- document index result
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
deleteDocuments
Deletes a collection of documents from the target index.Code Sample
Delete dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); SEARCH_ASYNC_CLIENT.deleteDocuments(Collections.singletonList(searchDocument)) .subscribe(result -> { for (IndexingResult indexingResult : result.getResults()) { System.out.printf("Does document with key %s delete successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to delete from the target Index. Fields other than the key are ignored.- Returns:
- document index result.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
deleteDocumentsWithResponse
public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options) Deletes a collection of documents from the target index.Code Sample
Delete dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.deleteDocumentsWithResponse(Collections.singletonList(searchDocument), null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (IndexingResult indexingResult : resultResponse.getValue().getResults()) { System.out.printf("Does document with key %s delete successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
documents- collection of documents to delete from the target Index. Fields other than the key are ignored.options- Options that allow specifying document indexing behavior.- Returns:
- response containing the document index result.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
indexDocuments
Sends a batch of upload, merge, and/or delete actions to the search index.Code Sample
Index batch operation on dynamic SearchDocument.
SearchDocument searchDocument1 = new SearchDocument(); searchDocument1.put("hotelId", "1"); searchDocument1.put("hotelName", "test1"); SearchDocument searchDocument2 = new SearchDocument(); searchDocument2.put("hotelId", "2"); searchDocument2.put("hotelName", "test2"); IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>(); indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1)); indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2)); SEARCH_ASYNC_CLIENT.indexDocuments(indexDocumentsBatch) .subscribe(result -> { for (IndexingResult indexingResult : result.getResults()) { System.out.printf("Does document with key %s finish successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
batch- The batch of index actions- Returns:
- Response containing the status of operations for all actions in the batch.
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
indexDocumentsWithResponse
public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options) Sends a batch of upload, merge, and/or delete actions to the search index.Code Sample
Index batch operation on dynamic SearchDocument.
SearchDocument searchDocument1 = new SearchDocument(); searchDocument1.put("hotelId", "1"); searchDocument1.put("hotelName", "test1"); SearchDocument searchDocument2 = new SearchDocument(); searchDocument2.put("hotelId", "2"); searchDocument2.put("hotelName", "test2"); IndexDocumentsBatch<SearchDocument> indexDocumentsBatch = new IndexDocumentsBatch<>(); indexDocumentsBatch.addUploadActions(Collections.singletonList(searchDocument1)); indexDocumentsBatch.addDeleteActions(Collections.singletonList(searchDocument2)); searchAsyncClient.indexDocumentsWithResponse(indexDocumentsBatch, null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (IndexingResult indexingResult : resultResponse.getValue().getResults()) { System.out.printf("Does document with key %s finish successfully? %b%n", indexingResult.getKey(), indexingResult.isSucceeded()); } });- Parameters:
batch- The batch of index actionsoptions- Options that allow specifying document indexing behavior.- Returns:
- Response containing the status of operations for all actions in the batch
- Throws:
IndexBatchException- If an indexing action fails but other actions succeed and modify the state of the index. This can happen when the Search Service is under heavy indexing load. It is important to explicitly catch this exception and check the return valueIndexBatchException.getIndexingResults(). The indexing result reports the status of each indexing action in the batch, making it possible to determine the state of the index after a partial failure.- See Also:
-
getDocument
Retrieves a document from the Azure AI Search index.View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
SEARCH_ASYNC_CLIENT.getDocument("hotelId", SearchDocument.class) .subscribe(result -> { for (Map.Entry<String, Object> keyValuePair : result.entrySet()) { System.out.printf("Document key %s, Document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } });- Type Parameters:
T- Convert document to the generic type.- Parameters:
key- The key of the document to retrieve.modelClass- The model class converts to.- Returns:
- the document object
- See Also:
-
getDocumentWithResponse
public <T> Mono<com.azure.core.http.rest.Response<T>> getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields) Retrieves a document from the Azure AI Search index.View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
SEARCH_ASYNC_CLIENT.getDocumentWithResponse("hotelId", SearchDocument.class, null) .subscribe(resultResponse -> { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); for (Map.Entry<String, Object> keyValuePair : resultResponse.getValue().entrySet()) { System.out.printf("Document key %s, Document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } });- Type Parameters:
T- Convert document to the generic type.- Parameters:
key- The key of the document to retrieve.modelClass- The model class converts to.selectedFields- List of field names to retrieve for the document; Any field not retrieved will have null or default as its corresponding property value in the returned object.- Returns:
- a response containing the document object
- See Also:
-
getDocumentCount
Queries the number of documents in the search index.Code Sample
Get document count.
SEARCH_ASYNC_CLIENT.getDocumentCount() .subscribe(count -> System.out.printf("There are %d documents in service.", count));- Returns:
- the number of documents.
-
getDocumentCountWithResponse
Queries the number of documents in the search index.Code Sample
Get document count.
SEARCH_ASYNC_CLIENT.getDocumentCountWithResponse() .subscribe(countResponse -> { System.out.println("The status code of the response is " + countResponse.getStatusCode()); System.out.printf("There are %d documents in service.", countResponse.getValue()); });- Returns:
- response containing the number of documents.
-
search
Searches for documents in the Azure AI Search index.If
searchTextis set to null or"*"all documents will be matched, see simple query syntax in Azure AI Search for more information about search query syntax.The
SearchPagedFluxwill iterate through search result pages until all search results are returned. Each page is determined by the$skipand$topvalues and the Search service has a limit on the number of documents that can be skipped, more information about the$skiplimit can be found at Search Documents REST API and reading the$skipdescription. If the total number of results exceeds the$skiplimit theSearchPagedFluxwon't prevent you from exceeding the$skiplimit. To prevent exceeding the limit you can track the number of documents returned and stop requesting new pages when the limit is reached.Code Sample
Search text from documents in service.
SearchPagedFlux searchPagedFlux = SEARCH_ASYNC_CLIENT.search("searchText"); searchPagedFlux.getTotalCount().subscribe( count -> System.out.printf("There are around %d results.", count)); AtomicLong numberOfDocumentsReturned = new AtomicLong(); searchPagedFlux.byPage() .takeUntil(page -> { if (numberOfDocumentsReturned.addAndGet(page.getValue().size()) >= SEARCH_SKIP_LIMIT) { // Reached the $skip limit, stop requesting more documents. return true; } return false; }) .subscribe(resultResponse -> { for (SearchResult result: resultResponse.getValue()) { SearchDocument searchDocument = result.getDocument(SearchDocument.class); for (Map.Entry<String, Object> keyValuePair: searchDocument.entrySet()) { System.out.printf("Document key %s, document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } } });- Parameters:
searchText- A full-text search query expression.- Returns:
- A
SearchPagedFluxthat iterates overSearchResultobjects and provides access to theSearchPagedResponseobject for each page containing HTTP response and count, facet, and coverage information. - See Also:
-
search
Searches for documents in the Azure AI Search index.If
searchTextis set to null or"*"all documents will be matched, see simple query syntax in Azure AI Search for more information about search query syntax.The
SearchPagedFluxwill iterate through search result pages until all search results are returned. Each page is determined by the$skipand$topvalues and the Search service has a limit on the number of documents that can be skipped, more information about the$skiplimit can be found at Search Documents REST API and reading the$skipdescription. If the total number of results exceeds the$skiplimit theSearchPagedFluxwon't prevent you from exceeding the$skiplimit. To prevent exceeding the limit you can track the number of documents returned and stop requesting new pages when the limit is reached.Code Sample
Search text from documents in service with option.
SearchPagedFlux pagedFlux = SEARCH_ASYNC_CLIENT.search("searchText", new SearchOptions().setOrderBy("hotelId desc")); pagedFlux.getTotalCount().subscribe(count -> System.out.printf("There are around %d results.", count)); AtomicLong numberOfDocumentsReturned = new AtomicLong(); pagedFlux.byPage() .takeUntil(page -> { if (numberOfDocumentsReturned.addAndGet(page.getValue().size()) >= SEARCH_SKIP_LIMIT) { // Reached the $skip limit, stop requesting more documents. return true; } return false; }) .subscribe(searchResultResponse -> searchResultResponse.getValue().forEach(searchDocument -> { for (Map.Entry<String, Object> keyValuePair : searchDocument.getDocument(SearchDocument.class).entrySet()) { System.out.printf("Document key %s, document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } }));- Parameters:
searchText- A full-text search query expression.searchOptions- Parameters to further refine the search query- Returns:
- A
SearchPagedFluxthat iterates overSearchResultobjects and provides access to theSearchPagedResponseobject for each page containing HTTP response and count, facet, and coverage information. - See Also:
-
suggest
Suggests documents in the index that match the given partial query.Code Sample
Suggest text from documents in service.
SEARCH_ASYNC_CLIENT.suggest("searchText", "sg") .subscribe(results -> { for (Map.Entry<String, Object> keyValuePair: results.getDocument(SearchDocument.class).entrySet()) { System.out.printf("Document key %s, document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } });- Parameters:
searchText- The search text.suggesterName- The name of the suggester.- Returns:
- A
SuggestPagedFluxthat iterates overSuggestResultobjects and provides access to theSuggestPagedResponseobject for each page containing HTTP response and coverage information. - See Also:
-
suggest
public SuggestPagedFlux suggest(String searchText, String suggesterName, SuggestOptions suggestOptions) Suggests documents in the index that match the given partial query.Code Sample
Suggest text from documents in service with option.
SEARCH_ASYNC_CLIENT.suggest("searchText", "sg", new SuggestOptions().setOrderBy("hotelId desc")) .subscribe(results -> { for (Map.Entry<String, Object> keyValuePair: results.getDocument(SearchDocument.class).entrySet()) { System.out.printf("Document key %s, document value %s", keyValuePair.getKey(), keyValuePair.getValue()); } });- Parameters:
searchText- The search text.suggesterName- The name of the suggester.suggestOptions- Parameters to further refine the suggestion query.- Returns:
- A
SuggestPagedFluxthat iterates overSuggestResultobjects and provides access to theSuggestPagedResponseobject for each page containing HTTP response and coverage information. - See Also:
-
autocomplete
Autocompletes incomplete query terms based on input text and matching terms in the index.Code Sample
Autocomplete text from documents in service.
SEARCH_ASYNC_CLIENT.autocomplete("searchText", "sg") .subscribe(result -> System.out.printf("The complete term is %s", result.getText()));- Parameters:
searchText- search textsuggesterName- suggester name- Returns:
- auto complete result.
-
autocomplete
public AutocompletePagedFlux autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions) Autocompletes incomplete query terms based on input text and matching terms in the index.Code Sample
Autocomplete text from documents in service with option.
SEARCH_ASYNC_CLIENT.autocomplete("searchText", "sg", new AutocompleteOptions().setAutocompleteMode(AutocompleteMode.ONE_TERM_WITH_CONTEXT)) .subscribe(result -> System.out.printf("The complete term is %s", result.getText()) );- Parameters:
searchText- search textsuggesterName- suggester nameautocompleteOptions- autocomplete options- Returns:
- auto complete result.
-