SearchIndexClient.java
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.documents.indexes;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.indexes.models.AnalyzeTextOptions;
import com.azure.search.documents.indexes.models.AnalyzedTokenInfo;
import com.azure.search.documents.indexes.models.FieldBuilderOptions;
import com.azure.search.documents.indexes.models.LexicalAnalyzerName;
import com.azure.search.documents.indexes.models.LexicalTokenizerName;
import com.azure.search.documents.indexes.models.SearchField;
import com.azure.search.documents.indexes.models.SearchIndex;
import com.azure.search.documents.indexes.models.SearchIndexStatistics;
import com.azure.search.documents.indexes.models.SearchServiceStatistics;
import com.azure.search.documents.indexes.models.SynonymMap;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
/**
* This class provides a client that contains the operations for creating, getting, listing, updating, or deleting
* indexes or synonym map and analyzing text in an Azure Cognitive Search service.
*
* @see SearchIndexClientBuilder
*/
@ServiceClient(builder = SearchIndexClientBuilder.class)
public final class SearchIndexClient {
private final SearchIndexAsyncClient asyncClient;
SearchIndexClient(SearchIndexAsyncClient searchIndexAsyncClient) {
this.asyncClient = searchIndexAsyncClient;
}
/**
* Gets the {@link HttpPipeline} powering this client.
*
* @return the pipeline.
*/
HttpPipeline getHttpPipeline() {
return this.asyncClient.getHttpPipeline();
}
/**
* Gets the endpoint for the Azure Cognitive Search service.
*
* @return the endpoint value.
*/
public String getEndpoint() {
return this.asyncClient.getEndpoint();
}
/**
* Initializes a new {@link SearchClient} using the given Index name and the same configuration as the
* SearchServiceClient.
*
* @param indexName the name of the Index for the client
* @return a {@link SearchClient} created from the service client configuration
*/
public SearchClient getSearchClient(String indexName) {
return asyncClient.getSearchClientBuilder(indexName).buildClient();
}
/**
* Creates a new Azure Cognitive Search index
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create search index named "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createIndex#SearchIndex -->
* <pre>
* List<SearchField> searchFields = Arrays.asList(
* new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true),
* new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true)
* );
* SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields);
* SearchIndex indexFromService = searchIndexClient.createIndex(searchIndex);
* System.out.printf("The index name is %s. The ETag of index is %s.%n", indexFromService.getName(),
* indexFromService.getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createIndex#SearchIndex -->
*
* @param index definition of the index to create
* @return the created Index.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SearchIndex createIndex(SearchIndex index) {
return createIndexWithResponse(index, Context.NONE).getValue();
}
/**
* Creates a new Azure Cognitive Search index
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create search index named "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createIndexWithResponse#SearchIndex-Context -->
* <pre>
* List<SearchField> searchFields = Arrays.asList(
* new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true),
* new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true)
* );
* SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields);
*
* Response<SearchIndex> indexFromServiceResponse =
* searchIndexClient.createIndexWithResponse(searchIndex, new Context(key1, value1));
* System.out.printf("The status code of the response is %s. The index name is %s.%n",
* indexFromServiceResponse.getStatusCode(), indexFromServiceResponse.getValue().getName());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createIndexWithResponse#SearchIndex-Context -->
*
* @param index definition of the index to create
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the created Index.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SearchIndex> createIndexWithResponse(SearchIndex index, Context context) {
return asyncClient.createIndexWithResponse(index, context).block();
}
/**
* Retrieves an index definition from the Azure Cognitive Search.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get search index with name "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getIndex#String -->
* <pre>
* SearchIndex indexFromService =
* searchIndexClient.getIndex("searchIndex");
* System.out.printf("The index name is %s. The ETag of index is %s.%n", indexFromService.getName(),
* indexFromService.getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getIndex#String -->
*
* @param indexName the name of the index to retrieve
* @return the Index.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SearchIndex getIndex(String indexName) {
return getIndexWithResponse(indexName, Context.NONE).getValue();
}
/**
* Retrieves an index definition from the Azure Cognitive Search.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get search index with "searchIndex. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getIndexWithResponse#String-Context -->
* <pre>
* Response<SearchIndex> indexFromServiceResponse =
* searchIndexClient.getIndexWithResponse("searchIndex", new Context(key1, value1));
*
* System.out.printf("The status code of the response is %s. The index name is %s.%n",
* indexFromServiceResponse.getStatusCode(), indexFromServiceResponse.getValue().getName());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getIndexWithResponse#String-Context -->
*
* @param indexName the name of the index to retrieve
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the Index.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SearchIndex> getIndexWithResponse(String indexName, Context context) {
return asyncClient.getIndexWithResponse(indexName, context).block();
}
/**
* Returns statistics for the given index, including a document count and storage usage.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get search index "searchIndex" statistics. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getIndexStatistics#String -->
* <pre>
* SearchIndexStatistics statistics = searchIndexClient.getIndexStatistics("searchIndex");
* System.out.printf("There are %d documents and storage size of %d available in 'searchIndex'.%n",
* statistics.getDocumentCount(), statistics.getStorageSize());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getIndexStatistics#String -->
*
* @param indexName the name of the index for which to retrieve statistics
* @return the index statistics result.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SearchIndexStatistics getIndexStatistics(String indexName) {
return getIndexStatisticsWithResponse(indexName, Context.NONE).getValue();
}
/**
* Returns statistics for the given index, including a document count and storage usage.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get search index "searchIndex" statistics. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getIndexStatisticsWithResponse#String-Context -->
* <pre>
* Response<SearchIndexStatistics> statistics = searchIndexClient.getIndexStatisticsWithResponse("searchIndex",
* new Context(key1, value1));
* System.out.printf("The status code of the response is %s.%n"
* + "There are %d documents and storage size of %d available in 'searchIndex'.%n",
* statistics.getStatusCode(), statistics.getValue().getDocumentCount(),
* statistics.getValue().getStorageSize());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getIndexStatisticsWithResponse#String-Context -->
*
* @param indexName the name of the index for which to retrieve statistics
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the index statistics result.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SearchIndexStatistics> getIndexStatisticsWithResponse(String indexName, Context context) {
return asyncClient.getIndexStatisticsWithResponse(indexName, context).block();
}
/**
* Lists all indexes available for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all search indexes. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listIndexes -->
* <pre>
* PagedIterable<SearchIndex> indexes = searchIndexClient.listIndexes();
* for (SearchIndex index: indexes) {
* System.out.printf("The index name is %s. The ETag of index is %s.%n", index.getName(),
* index.getETag());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listIndexes -->
*
* @return the list of indexes.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<SearchIndex> listIndexes() {
return listIndexes(Context.NONE);
}
/**
* Lists all indexes available for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all search indexes. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listIndexesWithResponse#Context -->
* <pre>
* PagedIterable<SearchIndex> indexes = searchIndexClient.listIndexes(new Context(key1, value1));
* System.out.println("The status code of the response is"
* + indexes.iterableByPage().iterator().next().getStatusCode());
* for (SearchIndex index: indexes) {
* System.out.printf("The index name is %s. The ETag of index is %s.%n", index.getName(), index.getETag());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listIndexesWithResponse#Context -->
*
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return the list of indexes.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<SearchIndex> listIndexes(Context context) {
return new PagedIterable<>(asyncClient.listIndexes(context));
}
/**
* Lists all index names for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all search indexes names. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listIndexNames -->
* <pre>
* PagedIterable<String> indexes = searchIndexClient.listIndexNames();
* for (String indexName: indexes) {
* System.out.printf("The index name is %s.%n", indexName);
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listIndexNames -->
*
* @return the list of index names.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> listIndexNames() {
return listIndexNames(Context.NONE);
}
/**
* Lists all indexes names for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all search indexes names. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listIndexNames#Context -->
* <pre>
* PagedIterable<String> indexes = searchIndexClient.listIndexNames(new Context(key1, value1));
* System.out.println("The status code of the response is"
* + indexes.iterableByPage().iterator().next().getStatusCode());
* for (String indexName: indexes) {
* System.out.printf("The index name is %s.%n", indexName);
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listIndexNames#Context -->
*
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return the list of index names.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> listIndexNames(Context context) {
return new PagedIterable<>(asyncClient.listIndexNames(context));
}
/**
* Creates a new Azure Cognitive Search index or updates an index if it already exists.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create or update search index named "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndex#SearchIndex -->
* <pre>
* SearchIndex indexFromService = searchIndexClient.getIndex("searchIndex");
* indexFromService.setSuggesters(Collections.singletonList(new SearchSuggester("sg",
* Collections.singletonList("hotelName"))));
* SearchIndex updatedIndex = searchIndexClient.createOrUpdateIndex(indexFromService);
* System.out.printf("The index name is %s. The suggester name of index is %s.%n", updatedIndex.getName(),
* updatedIndex.getSuggesters().get(0).getName());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndex#SearchIndex -->
*
* @param index the definition of the index to create or update
* @return the index that was created or updated.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SearchIndex createOrUpdateIndex(SearchIndex index) {
return createOrUpdateIndexWithResponse(index, false, false, Context.NONE).getValue();
}
/**
* Creates a new Azure Cognitive Search index or updates an index if it already exists.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create or update search index named "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndexWithResponse#SearchIndex-boolean-boolean-Context -->
* <pre>
* SearchIndex indexFromService = searchIndexClient.getIndex("searchIndex");
* indexFromService.setSuggesters(Collections.singletonList(new SearchSuggester("sg",
* Collections.singletonList("hotelName"))));
* Response<SearchIndex> updatedIndexResponse = searchIndexClient.createOrUpdateIndexWithResponse(indexFromService, true,
* false, new Context(key1, value1));
* System.out.printf("The status code of the normal response is %s.%n"
* + "The index name is %s. The ETag of index is %s.%n", updatedIndexResponse.getStatusCode(),
* updatedIndexResponse.getValue().getName(), updatedIndexResponse.getValue().getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndexWithResponse#SearchIndex-boolean-boolean-Context -->
*
* @param index the {@link SearchIndex} to create or update
* @param allowIndexDowntime allows new analyzers, tokenizers, token filters, or char filters to be added to an
* index by taking the index offline for at least a few seconds. This temporarily causes indexing and query requests
* to fail. Performance and write availability of the index can be impaired for several minutes after the index is
* updated, or longer for very large indexes.
* @param onlyIfUnchanged {@code true} to update if the {@code index} is the same as the current service value.
* {@code false} to always update existing value.
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the Index that was created or updated.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SearchIndex> createOrUpdateIndexWithResponse(SearchIndex index, boolean allowIndexDowntime,
boolean onlyIfUnchanged, Context context) {
return asyncClient.createOrUpdateIndexWithResponse(index, allowIndexDowntime, onlyIfUnchanged, context).block();
}
/**
* Deletes an Azure Cognitive Search index and all the documents it contains.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Delete search index with name "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteIndex#String -->
* <pre>
* searchIndexClient.deleteIndex("searchIndex");
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteIndex#String -->
*
* @param indexName the name of the index to delete
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteIndex(String indexName) {
deleteIndexWithResponse(new SearchIndex(indexName), false, Context.NONE);
}
/**
* Deletes an Azure Cognitive Search index and all the documents it contains.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Delete search index with name "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteIndexWithResponse#SearchIndex-boolean-Context -->
* <pre>
* SearchIndex indexFromService = searchIndexClient.getIndex("searchIndex");
* Response<Void> deleteResponse = searchIndexClient.deleteIndexWithResponse(indexFromService, true,
* new Context(key1, value1));
* System.out.printf("The status code of the response is %d.%n", deleteResponse.getStatusCode());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteIndexWithResponse#SearchIndex-boolean-Context -->
*
* @param index the Search {@link SearchIndex} to delete.
* @param onlyIfUnchanged {@code true} to delete if the {@code index} is the same as the current service value.
* {@code false} to always delete existing value.
* @param context additional context that is passed through the Http pipeline during the service call
* @return a response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> deleteIndexWithResponse(SearchIndex index, boolean onlyIfUnchanged, Context context) {
String etag = onlyIfUnchanged ? index.getETag() : null;
return asyncClient.deleteIndexWithResponse(index.getName(), etag, context).block();
}
/**
* Shows how an analyzer breaks text into tokens.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Analyzer text with LexicalTokenizerName "Classic" in search index "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.analyzeText#String-AnalyzeTextOptions -->
* <pre>
* PagedIterable<AnalyzedTokenInfo> tokenInfos = searchIndexClient.analyzeText("searchIndex",
* new AnalyzeTextOptions("The quick brown fox", LexicalTokenizerName.CLASSIC));
* for (AnalyzedTokenInfo tokenInfo : tokenInfos) {
* System.out.printf("The token emitted by the analyzer is %s.%n", tokenInfo.getToken());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.analyzeText#String-AnalyzeTextOptions -->
*
* @param indexName the name of the index for which to test an analyzer
* @param analyzeTextOptions the text and analyzer or analysis components to test. Requires to provide either {@link
* LexicalTokenizerName} or {@link LexicalAnalyzerName}.
* @return analyze result.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<AnalyzedTokenInfo> analyzeText(String indexName, AnalyzeTextOptions analyzeTextOptions) {
return analyzeText(indexName, analyzeTextOptions, Context.NONE);
}
/**
* Shows how an analyzer breaks text into tokens.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Analyzer text response with LexicalTokenizerName "Classic" in search index "searchIndex". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.analyzeText#String-AnalyzeTextOptions-Context -->
* <pre>
* PagedIterable<AnalyzedTokenInfo> tokenInfos = searchIndexClient.analyzeText("searchIndex",
* new AnalyzeTextOptions("The quick brown fox", LexicalTokenizerName.CLASSIC), new Context(key1, value1));
* System.out.println("The status code of the response is "
* + tokenInfos.iterableByPage().iterator().next().getStatusCode());
* for (AnalyzedTokenInfo tokenInfo : tokenInfos) {
* System.out.printf("The token emitted by the analyzer is %s.%n", tokenInfo.getToken());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.analyzeText#String-AnalyzeTextOptions-Context -->
*
* @param indexName the name of the index for which to test an analyzer
* @param analyzeTextOptions the text and analyzer or analysis components to test. Requires to provide either {@link
* LexicalTokenizerName} or {@link LexicalAnalyzerName}.
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return analyze result.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<AnalyzedTokenInfo> analyzeText(String indexName, AnalyzeTextOptions analyzeTextOptions,
Context context) {
return new PagedIterable<>(asyncClient.analyzeText(indexName, analyzeTextOptions, context));
}
/**
* Creates a new Azure Cognitive Search synonym map.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create synonym map named "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createSynonymMap#SynonymMap -->
* <pre>
* SynonymMap synonymMap = new SynonymMap("synonymMap",
* "United States, United States of America, USA\nWashington, Wash. => WA");
* SynonymMap synonymMapFromService = searchIndexClient.createSynonymMap(synonymMap);
* System.out.printf("The synonym map name is %s. The ETag of synonym map is %s.%n",
* synonymMapFromService.getName(), synonymMapFromService.getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createSynonymMap#SynonymMap -->
*
* @param synonymMap the definition of the synonym map to create
* @return the created {@link SynonymMap}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SynonymMap createSynonymMap(SynonymMap synonymMap) {
return createSynonymMapWithResponse(synonymMap, Context.NONE).getValue();
}
/**
* Creates a new Azure Cognitive Search synonym map.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create synonym map named "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createSynonymMapWithResponse#SynonymMap-Context -->
* <pre>
* SynonymMap synonymMap = new SynonymMap("synonymMap",
* "United States, United States of America, USA\nWashington, Wash. => WA");
* Response<SynonymMap> synonymMapFromService = searchIndexClient.createSynonymMapWithResponse(synonymMap,
* new Context(key1, value1));
* System.out.printf("The status code of the response is %d.%n"
* + "The synonym map name is %s. The ETag of synonym map is %s.%n", synonymMapFromService.getStatusCode(),
* synonymMapFromService.getValue().getName(), synonymMapFromService.getValue().getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createSynonymMapWithResponse#SynonymMap-Context -->
*
* @param synonymMap the definition of the synonym map to create
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the created SynonymMap.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SynonymMap> createSynonymMapWithResponse(SynonymMap synonymMap, Context context) {
return asyncClient.createSynonymMapWithResponse(synonymMap, context).block();
}
/**
* Retrieves a synonym map definition.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get synonym map with name "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getSynonymMap#String -->
* <pre>
* SynonymMap synonymMapFromService =
* searchIndexClient.getSynonymMap("synonymMap");
* System.out.printf("The synonym map is %s. The ETag of synonym map is %s.%n", synonymMapFromService.getName(),
* synonymMapFromService.getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getSynonymMap#String -->
*
* @param synonymMapName name of the synonym map to retrieve
* @return the {@link SynonymMap} definition
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SynonymMap getSynonymMap(String synonymMapName) {
return getSynonymMapWithResponse(synonymMapName, Context.NONE).getValue();
}
/**
* Retrieves a synonym map definition.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get synonym map with name "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getSynonymMapWithResponse#String-Context -->
* <pre>
* Response<SynonymMap> synonymMapFromService =
* searchIndexClient.getSynonymMapWithResponse("synonymMap", new Context(key1, value1));
* System.out.printf("The status code of the response is %d.%n"
* + "The synonym map name is %s. The ETag of synonym map is %s.%n", synonymMapFromService.getStatusCode(),
* synonymMapFromService.getValue().getName(), synonymMapFromService.getValue().getETag());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getSynonymMapWithResponse#String-Context -->
*
* @param synonymMapName name of the synonym map to retrieve
* @param context a context that is passed through the HTTP pipeline during the service call
* @return a response containing the SynonymMap.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SynonymMap> getSynonymMapWithResponse(String synonymMapName, Context context) {
return asyncClient.getSynonymMapWithResponse(synonymMapName, context).block();
}
/**
* Lists all synonym maps available for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all synonym maps. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listSynonymMaps -->
* <pre>
* PagedIterable<SynonymMap> synonymMaps = searchIndexClient.listSynonymMaps();
* for (SynonymMap synonymMap: synonymMaps) {
* System.out.printf("The synonymMap name is %s. The ETag of synonymMap is %s.%n", synonymMap.getName(),
* synonymMap.getETag());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listSynonymMaps -->
*
* @return the list of synonym maps.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<SynonymMap> listSynonymMaps() {
return listSynonymMaps(Context.NONE);
}
/**
* Lists all synonym maps available for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all synonym maps. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapsWithResponse#Context -->
* <pre>
* PagedIterable<SynonymMap> synonymMaps = searchIndexClient.listSynonymMaps(new Context(key1, value1));
* System.out.println("The status code of the response is"
* + synonymMaps.iterableByPage().iterator().next().getStatusCode());
* for (SynonymMap index: synonymMaps) {
* System.out.printf("The index name is %s. The ETag of index is %s.%n", index.getName(), index.getETag());
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapsWithResponse#Context -->
*
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return the list of synonym map names.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<SynonymMap> listSynonymMaps(Context context) {
return new PagedIterable<>(asyncClient.listSynonymMaps(context));
}
/**
* Lists all synonym maps names for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all synonym map names. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapNames -->
* <pre>
* PagedIterable<String> synonymMaps = searchIndexClient.listSynonymMapNames();
* for (String synonymMap: synonymMaps) {
* System.out.printf("The synonymMap name is %s.%n", synonymMap);
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapNames -->
*
* @return the list of synonym maps.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> listSynonymMapNames() {
return listSynonymMapNames(Context.NONE);
}
/**
* Lists all synonym maps names for an Azure Cognitive Search service.
*
* <p><strong>Code Sample</strong></p>
*
* <p> List all synonym map names. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapNamesWithResponse#Context -->
* <pre>
* PagedIterable<String> synonymMaps = searchIndexClient.listIndexNames(new Context(key1, value1));
* System.out.println("The status code of the response is"
* + synonymMaps.iterableByPage().iterator().next().getStatusCode());
* for (String synonymMapNames: synonymMaps) {
* System.out.printf("The synonymMap name is %s.%n", synonymMapNames);
* }
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listSynonymMapNamesWithResponse#Context -->
*
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return the list of synonym map names.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> listSynonymMapNames(Context context) {
return new PagedIterable<>(asyncClient.listSynonymMapNames(context));
}
/**
* Creates a new Azure Cognitive Search synonym map or updates a synonym map if it already exists.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create or update synonym map named "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateSynonymMap#SynonymMap -->
* <pre>
* SynonymMap synonymMap = searchIndexClient.getSynonymMap("searchIndex");
* synonymMap.setSynonyms("United States, United States of America, USA, America\nWashington, Wash. => WA");
* SynonymMap updatedSynonymMap = searchIndexClient.createOrUpdateSynonymMap(synonymMap);
* System.out.printf("The synonym map name is %s. The synonyms are %s.%n", updatedSynonymMap.getName(),
* updatedSynonymMap.getSynonyms());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateSynonymMap#SynonymMap -->
*
* @param synonymMap the definition of the synonym map to create or update
* @return the synonym map that was created or updated.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SynonymMap createOrUpdateSynonymMap(SynonymMap synonymMap) {
return createOrUpdateSynonymMapWithResponse(synonymMap, false, Context.NONE).getValue();
}
/**
* Creates a new Azure Cognitive Search synonym map or updates a synonym map if it already exists.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Create or update synonym map named "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateSynonymMapWithResponse#SynonymMap-boolean-Context -->
* <pre>
* SynonymMap synonymMap = searchIndexClient.getSynonymMap("searchIndex");
* synonymMap.setSynonyms("United States, United States of America, USA, America\nWashington, Wash. => WA");
* Response<SynonymMap> updatedSynonymMap =
* searchIndexClient.createOrUpdateSynonymMapWithResponse(synonymMap, true,
* new Context(key1, value1));
* System.out.printf("The status code of the normal response is %s.%n"
* + "The synonym map name is %s. The synonyms are %s.%n", updatedSynonymMap.getStatusCode(),
* updatedSynonymMap.getValue().getName(), updatedSynonymMap.getValue().getSynonyms());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateSynonymMapWithResponse#SynonymMap-boolean-Context -->
*
* @param synonymMap the definition of the synonym map to create or update
* @param onlyIfUnchanged {@code true} to update if the {@code synonymMap} is the same as the current service value.
* {@code false} to always update existing value.
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the synonym map that was created or updated.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SynonymMap> createOrUpdateSynonymMapWithResponse(SynonymMap synonymMap,
boolean onlyIfUnchanged, Context context) {
return asyncClient.createOrUpdateSynonymMapWithResponse(synonymMap, onlyIfUnchanged, context)
.block();
}
/**
* Deletes an Azure Cognitive Search synonym map.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Delete synonym map with name "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteSynonymMap#String -->
* <pre>
* searchIndexClient.deleteSynonymMap("synonymMap");
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteSynonymMap#String -->
*
* @param synonymMapName the name of the synonym map to delete
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteSynonymMap(String synonymMapName) {
deleteSynonymMapWithResponse(new SynonymMap(synonymMapName), false, Context.NONE);
}
/**
* Deletes an Azure Cognitive Search synonym map.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Delete synonym map with name "synonymMap". </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteSynonymMapWithResponse#SynonymMap-boolean-Context -->
* <pre>
* SynonymMap synonymMap = searchIndexClient.getSynonymMap("synonymMap");
* Response<Void> response = searchIndexClient.deleteSynonymMapWithResponse(synonymMap, true,
* new Context(key1, value1));
* System.out.println("The status code of the response is" + response.getStatusCode());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteSynonymMapWithResponse#SynonymMap-boolean-Context -->
*
* @param synonymMap the {@link SynonymMap} to delete.
* @param onlyIfUnchanged {@code true} to delete if the {@code synonymMap} is the same as the current service value.
* {@code false} to always delete existing value.
* @param context additional context that is passed through the Http pipeline during the service call
* @return a response signalling completion.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> deleteSynonymMapWithResponse(SynonymMap synonymMap, boolean onlyIfUnchanged,
Context context) {
String etag = onlyIfUnchanged ? synonymMap.getETag() : null;
return asyncClient.deleteSynonymMapWithResponse(synonymMap.getName(), etag, context).block();
}
/**
* Returns service level statistics for a search service, including service counters and limits.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get service statistics. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getServiceStatistics -->
* <pre>
* SearchServiceStatistics serviceStatistics = searchIndexClient.getServiceStatistics();
* System.out.printf("There are %s search indexes in your service.%n",
* serviceStatistics.getCounters().getIndexCounter());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getServiceStatistics -->
*
* @return the search service statistics result.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public SearchServiceStatistics getServiceStatistics() {
return getServiceStatisticsWithResponse(Context.NONE).getValue();
}
/**
* Returns service level statistics for a search service, including service counters and limits.
*
* <p><strong>Code Sample</strong></p>
*
* <p> Get service statistics. </p>
*
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getServiceStatisticsWithResponse#Context -->
* <pre>
* Response<SearchServiceStatistics> serviceStatistics =
* searchIndexClient.getServiceStatisticsWithResponse(new Context(key1, value1));
* System.out.printf("The status code of the response is %s.%nThere are %s search indexes in your service.%n",
* serviceStatistics.getStatusCode(),
* serviceStatistics.getValue().getCounters().getIndexCounter());
* </pre>
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getServiceStatisticsWithResponse#Context -->
*
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return the search service statistics result.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<SearchServiceStatistics> getServiceStatisticsWithResponse(Context context) {
return asyncClient.getServiceStatisticsWithResponse(context).block();
}
/**
* Convenience method to convert a {@link Class Class's} {@link Field Fields} and {@link Method Methods} into {@link
* SearchField SearchFields} to help aid the creation of a {@link SearchField} which represents the {@link Class}.
*
* @param model The model {@link Class} that will have {@link SearchField SearchFields} generated from its
* structure.
* @param options Configuration used to determine generation of the {@link SearchField SearchFields}.
* @return A list {@link SearchField SearchFields} which represent the model {@link Class}.
*/
public static List<SearchField> buildSearchFields(Class<?> model, FieldBuilderOptions options) {
return SearchIndexAsyncClient.buildSearchFields(model, options);
}
}