Class SearchClient
- java.lang.Object
-
- com.azure.search.documents.SearchClient
-
public final class SearchClient extends Object
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.- See Also:
SearchClientBuilder
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AutocompletePagedIterable
autocomplete(String searchText, String suggesterName)
Autocompletes incomplete query terms based on input text and matching terms in the index.AutocompletePagedIterable
autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions, com.azure.core.util.Context context)
Autocompletes incomplete query terms based on input text and matching terms in the index.IndexDocumentsResult
deleteDocuments(Iterable<?> documents)
Deletes a collection of documents from the target index.com.azure.core.http.rest.Response<IndexDocumentsResult>
deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
Deletes a collection of documents from the target index.<T> T
getDocument(String key, Class<T> modelClass)
Retrieves a document from the Azure Cognitive Search index.long
getDocumentCount()
Queries the number of documents in the search index.com.azure.core.http.rest.Response<Long>
getDocumentCountWithResponse(com.azure.core.util.Context context)
Queries the number of documents in the search index.<T> com.azure.core.http.rest.Response<T>
getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields, com.azure.core.util.Context context)
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.IndexDocumentsResult
indexDocuments(IndexDocumentsBatch<?> batch)
Sends a batch of upload, merge, and/or delete actions to the search index.com.azure.core.http.rest.Response<IndexDocumentsResult>
indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options, com.azure.core.util.Context context)
Sends a batch of upload, merge, and/or delete actions to the search index.IndexDocumentsResult
mergeDocuments(Iterable<?> documents)
Merges a collection of documents with existing documents in the target index.com.azure.core.http.rest.Response<IndexDocumentsResult>
mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
Merges a collection of documents with existing documents in the target index.IndexDocumentsResult
mergeOrUploadDocuments(Iterable<?> documents)
This action behaves like merge if a document with the given key already exists in the index.com.azure.core.http.rest.Response<IndexDocumentsResult>
mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
This action behaves like merge if a document with the given key already exists in the index.SearchPagedIterable
search(String searchText)
Searches for documents in the Azure Cognitive Search index.SearchPagedIterable
search(String searchText, SearchOptions searchOptions, com.azure.core.util.Context context)
Searches for documents in the Azure Cognitive Search index.SuggestPagedIterable
suggest(String searchText, String suggesterName)
Suggests documents in the index that match the given partial query.SuggestPagedIterable
suggest(String searchText, String suggesterName, SuggestOptions suggestOptions, com.azure.core.util.Context context)
Suggests documents in the index that match the given partial query.IndexDocumentsResult
uploadDocuments(Iterable<?> documents)
Uploads a collection of documents to the target index.com.azure.core.http.rest.Response<IndexDocumentsResult>
uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
Uploads a collection of documents to the target index.
-
-
-
Method Detail
-
getIndexName
public String getIndexName()
Gets the name of the Azure Cognitive Search index.- Returns:
- the indexName value.
-
getEndpoint
public String getEndpoint()
Gets the endpoint for the Azure Cognitive Search service.- Returns:
- the endpoint value.
-
uploadDocuments
public IndexDocumentsResult uploadDocuments(Iterable<?> documents)
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"); IndexDocumentsResult result = searchClient.uploadDocuments(Collections.singletonList(searchDocument)); 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:
- document index result.
- Throws:
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 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:
- Add, update, or delete documents
-
uploadDocumentsWithResponse
public com.azure.core.http.rest.Response<IndexDocumentsResult> uploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
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"); Response<IndexDocumentsResult> resultResponse = searchClient.uploadDocumentsWithResponse( Collections.singletonList(searchDocument), null, new Context(key1, value1)); 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing the document index result.
- Throws:
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 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:
- Add, update, or delete documents
-
mergeDocuments
public IndexDocumentsResult mergeDocuments(Iterable<?> documents)
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
forint
or 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 typeInteger
instead ofint
).Code Sample
Merge dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelName", "merge"); IndexDocumentsResult result = searchClient.mergeDocuments(Collections.singletonList(searchDocument)); 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 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 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:
- Add, update, or delete documents
-
mergeDocumentsWithResponse
public com.azure.core.http.rest.Response<IndexDocumentsResult> mergeDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
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
forint
or 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 typeInteger
instead ofint
).Code Sample
Merge dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelName", "test"); Response<IndexDocumentsResult> resultResponse = searchClient.mergeDocumentsWithResponse( Collections.singletonList(searchDocument), null, new Context(key1, value1)); 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 merged.options
- Options that allow specifying document indexing behavior.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing the document index result.
- Throws:
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 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:
- Add, update, or delete documents
-
mergeOrUploadDocuments
public IndexDocumentsResult mergeOrUploadDocuments(Iterable<?> documents)
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
forint
or 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 typeInteger
instead ofint
).Code Sample
Merge or upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); IndexDocumentsResult result = searchClient.mergeOrUploadDocuments(Collections.singletonList(searchDocument)); 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 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 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:
- Add, update, or delete documents
-
mergeOrUploadDocumentsWithResponse
public com.azure.core.http.rest.Response<IndexDocumentsResult> mergeOrUploadDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
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
forint
or 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 typeInteger
instead ofint
).Code Sample
Merge or upload dynamic SearchDocument.
SearchDocument searchDocument = new SearchDocument(); searchDocument.put("hotelId", "1"); searchDocument.put("hotelName", "test"); Response<IndexDocumentsResult> resultResponse = searchClient.mergeOrUploadDocumentsWithResponse( Collections.singletonList(searchDocument), null, new Context(key1, value1)); 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing a document index result
- Throws:
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 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:
- Add, update, or delete documents
-
deleteDocuments
public IndexDocumentsResult deleteDocuments(Iterable<?> documents)
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"); IndexDocumentsResult result = searchClient.deleteDocuments(Collections.singletonList(searchDocument)); 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 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 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:
- Add, update, or delete documents
-
deleteDocumentsWithResponse
public com.azure.core.http.rest.Response<IndexDocumentsResult> deleteDocumentsWithResponse(Iterable<?> documents, IndexDocumentsOptions options, com.azure.core.util.Context context)
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"); Response<IndexDocumentsResult> resultResponse = searchClient.deleteDocumentsWithResponse( Collections.singletonList(searchDocument), null, new Context(key1, value1)); 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing a document index result.
- Throws:
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 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:
- Add, update, or delete documents
-
indexDocuments
public IndexDocumentsResult indexDocuments(IndexDocumentsBatch<?> batch)
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)); IndexDocumentsResult result = searchClient.indexDocuments(indexDocumentsBatch); 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 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 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:
- Add, update, or delete documents
-
indexDocumentsWithResponse
public com.azure.core.http.rest.Response<IndexDocumentsResult> indexDocumentsWithResponse(IndexDocumentsBatch<?> batch, IndexDocumentsOptions options, com.azure.core.util.Context context)
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)); Response<IndexDocumentsResult> resultResponse = searchClient.indexDocumentsWithResponse(indexDocumentsBatch, null, new Context(key1, value1)); 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- Response containing the status of operations for all actions in the batch
- Throws:
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 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:
- Add, update, or delete documents
-
getDocument
public <T> T getDocument(String key, Class<T> modelClass)
Retrieves a document from the Azure Cognitive Search index.View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
SearchDocument result = searchClient.getDocument("hotelId", SearchDocument.class); 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:
- document object
- See Also:
- Lookup document
-
getDocumentWithResponse
public <T> com.azure.core.http.rest.Response<T> getDocumentWithResponse(String key, Class<T> modelClass, List<String> selectedFields, com.azure.core.util.Context context)
Retrieves a document from the Azure Cognitive Search index.View naming rules for guidelines on constructing valid document keys.
Code Sample
Get dynamic SearchDocument.
Response<SearchDocument> resultResponse = searchClient.getDocumentWithResponse("hotelId", SearchDocument.class, null, new Context(key1, value1)); 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing a document object
- See Also:
- Lookup document
-
getDocumentCount
public long getDocumentCount()
Queries the number of documents in the search index.Code Sample
Get document count.
long count = searchClient.getDocumentCount(); System.out.printf("There are %d documents in service.", count);
- Returns:
- the number of documents.
-
getDocumentCountWithResponse
public com.azure.core.http.rest.Response<Long> getDocumentCountWithResponse(com.azure.core.util.Context context)
Queries the number of documents in the search index.Code Sample
Get document count.
Response<Long> countResponse = searchClient.getDocumentCountWithResponse(new Context(key1, value1)); System.out.println("The status code of the response is " + countResponse.getStatusCode()); System.out.printf("There are %d documents in service.", countResponse.getValue());
- Parameters:
context
- additional context that is passed through the Http pipeline during the service call- Returns:
- response containing the number of documents.
-
search
public SearchPagedIterable 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.Code Sample
Search text from documents in service.
SearchPagedIterable searchPagedIterable = searchClient.search("searchText"); System.out.printf("There are around %d results.", searchPagedIterable.getTotalCount()); for (SearchPagedResponse resultResponse: searchPagedIterable.iterableByPage()) { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); resultResponse.getValue().forEach(searchResult -> { for (Map.Entry<String, Object> keyValuePair: searchResult .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.- Returns:
- A
SearchPagedIterable
that iterates overSearchResult
objects and provides access to theSearchPagedResponse
object for each page containing HTTP response and count, facet, and coverage information. - See Also:
- Search documents
-
search
public SearchPagedIterable search(String searchText, SearchOptions searchOptions, com.azure.core.util.Context context)
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.Code Sample
Search text from documents in service with option.
SearchPagedIterable searchPagedIterable = searchClient.search("searchText", new SearchOptions().setOrderBy("hotelId desc"), new Context(key1, value1)); System.out.printf("There are around %d results.", searchPagedIterable.getTotalCount()); for (SearchPagedResponse resultResponse: searchPagedIterable.iterableByPage()) { System.out.println("The status code of the response is " + resultResponse.getStatusCode()); resultResponse.getValue().forEach(searchResult -> { for (Map.Entry<String, Object> keyValuePair: searchResult .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 querycontext
- additional context that is passed through the Http pipeline during the service call- Returns:
- A
SearchPagedIterable
that iterates overSearchResult
objects and provides access to theSearchPagedResponse
object for each page containing HTTP response and count, facet, and coverage information. - See Also:
- Search documents
-
suggest
public SuggestPagedIterable suggest(String searchText, String suggesterName)
Suggests documents in the index that match the given partial query.Code Sample
Suggest text from documents in service.
SuggestPagedIterable suggestPagedIterable = searchClient.suggest("searchText", "sg"); for (SuggestResult result: suggestPagedIterable) { 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
- 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 definition- Returns:
- A
SuggestPagedIterable
that iterates overSuggestResult
objects and provides access to theSuggestPagedResponse
object for each page containing HTTP response and coverage information. - See Also:
- Suggestions
-
suggest
public SuggestPagedIterable suggest(String searchText, String suggesterName, SuggestOptions suggestOptions, com.azure.core.util.Context context)
Suggests documents in the index that match the given partial query.Code Sample
Suggest text from documents in service with option.
SuggestPagedIterable suggestPagedIterable = searchClient.suggest("searchText", "sg", new SuggestOptions().setOrderBy("hotelId desc"), new Context(key1, value1)); for (SuggestResult result: suggestPagedIterable) { 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
- 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.context
- additional context that is passed through the Http pipeline during the service call- Returns:
- A
SuggestPagedIterable
that iterates overSuggestResult
objects and provides access to theSuggestPagedResponse
object for each page containing HTTP response and coverage information. - See Also:
- Suggestions
-
autocomplete
public AutocompletePagedIterable 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.
AutocompletePagedIterable autocompletePagedIterable = searchClient.autocomplete("searchText", "sg"); for (AutocompleteItem result: autocompletePagedIterable) { System.out.printf("The complete term is %s", result.getText()); }
- Parameters:
searchText
- search textsuggesterName
- suggester name- Returns:
- auto complete result.
-
autocomplete
public AutocompletePagedIterable autocomplete(String searchText, String suggesterName, AutocompleteOptions autocompleteOptions, com.azure.core.util.Context context)
Autocompletes incomplete query terms based on input text and matching terms in the index.Code Sample
Autocomplete text from documents in service with option.
AutocompletePagedIterable autocompletePagedIterable = searchClient.autocomplete("searchText", "sg", new AutocompleteOptions().setAutocompleteMode(AutocompleteMode.ONE_TERM_WITH_CONTEXT), new Context(key1, value1)); for (AutocompleteItem result: autocompletePagedIterable) { System.out.printf("The complete term is %s", result.getText()); }
- Parameters:
searchText
- search textsuggesterName
- suggester nameautocompleteOptions
- autocomplete optionscontext
- additional context that is passed through the HTTP pipeline during the service call- Returns:
- auto complete result.
-
-