SearchAsyncClient Class
- java.
lang. Object - com.
azure. search. documents. SearchAsyncClient
- com.
public final class SearchAsyncClient
This class provides a client that contains the operations for querying an index and uploading, merging, or deleting documents in an Azure Cognitive Search service.
Method Summary
Modifier and Type | Method and Description |
---|---|
Mono<T> |
getDocument(String key, Class<T> modelClass)
Retrieves a document from the Azure Cognitive Search index. |
Mono<Response<T>> |
getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields)
Retrieves a document from the Azure Cognitive Search index. |
Autocomplete |
autocomplete(String searchText, String suggesterName)
Autocompletes incomplete query terms based on input text and matching terms in the index. |
Autocomplete |
autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions)
Autocompletes incomplete query terms based on input text and matching terms in the index. |
Mono<Index |
deleteDocuments(Iterable<?> documents)
Deletes a collection of documents from the target index. |
Mono<Response<Index |
deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
Deletes a collection of documents from the target index. |
Mono<Long> |
getDocumentCount()
Queries the number of documents in the search index. |
Mono<Response<Long>> |
getDocumentCountWithResponse()
Queries the number of documents in the 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<Index |
indexDocuments(IndexDocumentsBatch<?> batch)
Sends a batch of upload, merge, and/or delete actions to the search index. |
Mono<Response<Index |
indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options)
Sends a batch of upload, merge, and/or delete actions to the search index. |
Mono<Index |
mergeDocuments(Iterable<?> documents)
Merges a collection of documents with existing documents in the target index. |
Mono<Response<Index |
mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
Merges a collection of documents with existing documents in the target index. |
Mono<Index |
mergeOrUploadDocuments(Iterable<?> documents)
This action behaves like merge if a document with the given key already exists in the index. |
Mono<Response<Index |
mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
This action behaves like merge if a document with the given key already exists in the index. |
Search |
search(String searchText)
Searches for documents in the Azure Cognitive Search index. |
Search |
search(String searchText, SearchOptions searchOptions)
Searches for documents in the Azure Cognitive Search index. |
Suggest |
suggest(String searchText, String suggesterName)
Suggests documents in the index that match the given partial query. |
Suggest |
suggest(String searchText, String suggesterName, SuggestOptions suggestOptions)
Suggests documents in the index that match the given partial query. |
Mono<Index |
uploadDocuments(Iterable<?> documents)
Uploads a collection of documents to the target index. |
Mono<Response<Index |
uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options)
Uploads a collection of documents to the target index. |
Methods inherited from java.lang.Object
Method Details
getDocument
public Mono
Retrieves a document from the Azure Cognitive 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());
}
});
Parameters:
Returns:
getDocumentWithResponse
public Mono<>
Retrieves a document from the Azure Cognitive 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());
}
});
Parameters:
Returns:
autocomplete
public AutocompletePagedFlux autocomplete(String searchText, String suggesterName)
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:
Returns:
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:
Returns:
deleteDocuments
public Mono
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:
Returns:
deleteDocumentsWithResponse
public Mono<>
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:
Returns:
getDocumentCount
public Mono
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:
getDocumentCountWithResponse
public Mono<>
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:
getEndpoint
public String getEndpoint()
Gets the endpoint for the Azure Cognitive Search service.
Returns:
getIndexName
public String getIndexName()
Gets the name of the Azure Cognitive Search index.
Returns:
indexDocuments
public Mono
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:
Returns:
indexDocumentsWithResponse
public Mono<>
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:
Returns:
mergeDocuments
public Mono
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, 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");
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:
Returns:
mergeDocumentsWithResponse
public Mono<>
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, 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());
}
});
Parameters:
Returns:
mergeOrUploadDocuments
public Mono
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, 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");
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:
Returns:
mergeOrUploadDocumentsWithResponse
public Mono<>
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, 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());
}
});
Parameters:
Returns:
search
public SearchPagedFlux search(String searchText)
Searches for documents in the Azure Cognitive Search index.
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.
The SearchPagedFlux will iterate through search result pages until all search results are returned. Each page is determined by the $skip
and $top
values and the Search service has a limit on the number of documents that can be skipped, more information about the $skip
limit can be found at Search Documents REST API and reading the $skip
description. If the total number of results exceeds the $skip
limit the SearchPagedFlux won't prevent you from exceeding the $skip
limit. 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:
Returns:
search
public SearchPagedFlux search(String searchText, SearchOptions searchOptions)
Searches for documents in the Azure Cognitive Search index.
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.
The SearchPagedFlux will iterate through search result pages until all search results are returned. Each page is determined by the $skip
and $top
values and the Search service has a limit on the number of documents that can be skipped, more information about the $skip
limit can be found at Search Documents REST API and reading the $skip
description. If the total number of results exceeds the $skip
limit the SearchPagedFlux won't prevent you from exceeding the $skip
limit. 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:
Returns:
suggest
public SuggestPagedFlux suggest(String searchText, String suggesterName)
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:
Returns:
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:
Returns:
uploadDocuments
public Mono
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:
Returns:
uploadDocumentsWithResponse
public Mono<>
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:
Returns:
Applies to
Azure SDK for Java
Feedback
https://aka.ms/ContentUserFeedback.
Bald verfügbar: Im Laufe des Jahres 2024 werden wir GitHub-Issues stufenweise als Feedbackmechanismus für Inhalte abbauen und durch ein neues Feedbacksystem ersetzen. Weitere Informationen finden Sie unterFeedback senden und anzeigen für