public final class SearchAsyncClient extends Object
SearchClientBuilder
Modifier and Type | Method and Description |
---|---|
AutocompletePagedFlux |
autocomplete(String searchText,
String suggesterName)
Autocompletes incomplete query terms based on input text and matching terms in the index.
|
AutocompletePagedFlux |
autocomplete(String searchText,
String suggesterName,
AutocompleteOptions autocompleteOptions)
Autocompletes incomplete query terms based on input text and matching terms in the index.
|
Mono<IndexDocumentsResult> |
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 Cognitive Search index.
|
Mono<Long> |
getDocumentCount()
Queries the number of documents in the search index.
|
Mono<com.azure.core.http.rest.Response<Long>> |
getDocumentCountWithResponse()
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 Cognitive Search index.
|
String |
getEndpoint()
Gets the endpoint for the Azure Cognitive Search service.
|
String |
getIndexName()
Gets the name of the Azure Cognitive Search index.
|
Mono<IndexDocumentsResult> |
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.
|
Mono<IndexDocumentsResult> |
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.
|
Mono<IndexDocumentsResult> |
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.
|
SearchPagedFlux |
search(String searchText)
Searches for documents in the Azure Cognitive Search index.
|
SearchPagedFlux |
search(String searchText,
SearchOptions searchOptions)
Searches for documents in the Azure Cognitive Search index.
|
SuggestPagedFlux |
suggest(String searchText,
String suggesterName)
Suggests documents in the index that match the given partial query.
|
SuggestPagedFlux |
suggest(String searchText,
String suggesterName,
SuggestOptions suggestOptions)
Suggests documents in the index that match the given partial query.
|
Mono<IndexDocumentsResult> |
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.
|
public String getIndexName()
public String getEndpoint()
public Mono<IndexDocumentsResult> uploadDocuments(Iterable<?> documents)
Code Sample
Upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.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()); } });
documents
- collection of documents to upload to the target Index.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
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()); } });
documents
- collection of documents to upload to the target Index.options
- Options that allow specifying document indexing behavior.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<IndexDocumentsResult> mergeDocuments(Iterable<?> documents)
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, 0
for int
or false for boolean
), 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
type Integer
instead of int
).
Code Sample
Merge dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelName", "merge"); searchAsyncClient.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()); } });
documents
- collection of documents to be mergedIndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
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, 0
for int
or false for boolean
), 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
type Integer
instead of int
).
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()); } });
documents
- collection of documents to be mergedoptions
- Options that allow specifying document indexing behavior.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<IndexDocumentsResult> mergeOrUploadDocuments(Iterable<?> documents)
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, 0
for int
or false for boolean
), 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
type Integer
instead of int
).
Code Sample
Merge or upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.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()); } });
documents
- collection of documents to be merged, if exists, otherwise uploadedIndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
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, 0
for int
or false for boolean
), 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
type Integer
instead of int
).
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()); } });
documents
- collection of documents to be merged, if exists, otherwise uploadedoptions
- Options that allow specifying document indexing behavior.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<IndexDocumentsResult> deleteDocuments(Iterable<?> documents)
Code Sample
Delete dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); searchAsyncClient.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()); } });
documents
- collection of documents to delete from the target Index. Fields other than the key are ignored.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
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()); } });
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.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<IndexDocumentsResult> indexDocuments(IndexDocumentsBatch<?> batch)
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.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()); } });
batch
- The batch of index actionsIndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public Mono<com.azure.core.http.rest.Response<IndexDocumentsResult>> indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options)
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()); } });
batch
- The batch of index actionsoptions
- Options that allow specifying document indexing behavior.IndexBatchException
- If some of the indexing actions fail 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 value IndexBatchException.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.public <T> Mono<T> getDocument(String key, Class<T> modelClass)
View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
searchAsyncClient.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()); } });
T
- Convert document to the generic type.key
- The key of the document to retrieve.modelClass
- The model class converts to.public <T> Mono<com.azure.core.http.rest.Response<T>> getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields)
View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
searchAsyncClient.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()); } });
T
- Convert document to the generic type.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.public Mono<Long> getDocumentCount()
Code Sample
Get document count.
searchAsyncClient.getDocumentCount() .subscribe(count -> System.out.printf("There are %d documents in service.", count));
public Mono<com.azure.core.http.rest.Response<Long>> getDocumentCountWithResponse()
Code Sample
Get document count.
searchAsyncClient.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()); });
public SearchPagedFlux search(String searchText)
If searchText
is set to null or "*"
all documents will be matched, see
simple query
syntax in Azure Cognitive Search for more information about search query syntax.
Code Sample
Search text from documents in service.
SearchPagedFlux searchPagedFlux = searchAsyncClient.search("searchText"); searchPagedFlux.getTotalCount().subscribe( count -> System.out.printf("There are around %d results.", count) ); searchPagedFlux.byPage() .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()); } } });
searchText
- A full-text search query expression.SearchPagedFlux
that iterates over SearchResult
objects and provides access to the
SearchPagedResponse
object for each page containing HTTP response and count, facet, and coverage
information.public SearchPagedFlux search(String searchText, SearchOptions searchOptions)
If searchText
is set to null or "*"
all documents will be matched, see
simple query
syntax in Azure Cognitive Search for more information about search query syntax.
Code Sample
Search text from documents in service with option.
SearchPagedFlux pagedFlux = searchAsyncClient.search("searchText", new SearchOptions().setOrderBy("hotelId desc")); pagedFlux.getTotalCount().subscribe(count -> System.out.printf("There are around %d results.", count)); pagedFlux.byPage() .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()); } }));
searchText
- A full-text search query expression.searchOptions
- Parameters to further refine the search querySearchPagedFlux
that iterates over SearchResult
objects and provides access to the
SearchPagedResponse
object for each page containing HTTP response and count, facet, and coverage
information.public SuggestPagedFlux suggest(String searchText, String suggesterName)
Code Sample
Suggest text from documents in service.
searchAsyncClient.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()); } });
searchText
- The search text on which to base suggestionssuggesterName
- The name of the suggester as specified in the suggesters collection that's part of the index
definitionSuggestPagedFlux
that iterates over SuggestResult
objects and provides access to the
SuggestPagedResponse
object for each page containing HTTP response and coverage information.public SuggestPagedFlux suggest(String searchText, String suggesterName, SuggestOptions suggestOptions)
Code Sample
Suggest text from documents in service with option.
searchAsyncClient.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()); } });
searchText
- The search text on which to base suggestionssuggesterName
- The name of the suggester as specified in the suggesters collection that's part of the index
definitionsuggestOptions
- Parameters to further refine the suggestion query.SuggestPagedFlux
that iterates over SuggestResult
objects and provides access to the
SuggestPagedResponse
object for each page containing HTTP response and coverage information.public AutocompletePagedFlux autocomplete(String searchText, String suggesterName)
Code Sample
Autocomplete text from documents in service.
searchAsyncClient.autocomplete("searchText", "sg") .subscribe(result -> System.out.printf("The complete term is %s", result.getText()));
searchText
- search textsuggesterName
- suggester namepublic AutocompletePagedFlux autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions)
Code Sample
Autocomplete text from documents in service with option.
searchAsyncClient.autocomplete("searchText", "sg", new AutocompleteOptions().setAutocompleteMode(AutocompleteMode.ONE_TERM_WITH_CONTEXT)) .subscribe(result -> System.out.printf("The complete term is %s", result.getText()) );
searchText
- search textsuggesterName
- suggester nameautocompleteOptions
- autocomplete optionsCopyright © 2021 Microsoft Corporation. All rights reserved.