Skip to content

Commit 68fa950

Browse files
authored
Add Aliasing to SearchIndexClient and SearchIndexAsyncClient (Azure#27459)
Add Aliasing to SearchIndexClient and SearchIndexAsyncClient
1 parent 67b8952 commit 68fa950

22 files changed

+2733
-56
lines changed

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/SearchIndexAsyncClient.java

Lines changed: 323 additions & 50 deletions
Large diffs are not rendered by default.

sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/SearchIndexClient.java

Lines changed: 254 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.azure.search.documents.indexes.models.FieldBuilderOptions;
1616
import com.azure.search.documents.indexes.models.LexicalAnalyzerName;
1717
import com.azure.search.documents.indexes.models.LexicalTokenizerName;
18+
import com.azure.search.documents.indexes.models.SearchAlias;
1819
import com.azure.search.documents.indexes.models.SearchField;
1920
import com.azure.search.documents.indexes.models.SearchIndex;
2021
import com.azure.search.documents.indexes.models.SearchIndexStatistics;
@@ -405,7 +406,7 @@ public Response<SearchIndex> createOrUpdateIndexWithResponse(SearchIndex index,
405406
*/
406407
@ServiceMethod(returns = ReturnType.SINGLE)
407408
public void deleteIndex(String indexName) {
408-
deleteIndexWithResponse(new SearchIndex(indexName), false, Context.NONE);
409+
asyncClient.deleteIndexWithResponse(indexName, null, Context.NONE).block();
409410
}
410411

411412
/**
@@ -771,7 +772,7 @@ public Response<SynonymMap> createOrUpdateSynonymMapWithResponse(SynonymMap syno
771772
*/
772773
@ServiceMethod(returns = ReturnType.SINGLE)
773774
public void deleteSynonymMap(String synonymMapName) {
774-
deleteSynonymMapWithResponse(new SynonymMap(synonymMapName), false, Context.NONE);
775+
asyncClient.deleteSynonymMapWithResponse(synonymMapName, null, Context.NONE).block();
775776
}
776777

777778
/**
@@ -863,4 +864,255 @@ public static List<SearchField> buildSearchFields(Class<?> model, FieldBuilderOp
863864
return SearchIndexAsyncClient.buildSearchFields(model, options);
864865
}
865866

867+
/**
868+
* Creates a new Azure Cognitive Search alias.
869+
*
870+
* <p><strong>Code Sample</strong></p>
871+
*
872+
* <p> Create the search alias named "my-alias". </p>
873+
*
874+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createAlias#SearchAlias -->
875+
* <pre>
876+
* SearchAlias searchAlias = searchIndexClient.createAlias&#40;new SearchAlias&#40;&quot;my-alias&quot;,
877+
* Collections.singletonList&#40;&quot;index-to-alias&quot;&#41;&#41;&#41;;
878+
* System.out.printf&#40;&quot;Created alias '%s' that aliases index '%s'.&quot;, searchAlias.getName&#40;&#41;,
879+
* searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
880+
* </pre>
881+
* <!-- end com.azure.search.documents.indexes.SearchClient.createAlias#SearchAlias -->
882+
*
883+
* @param alias definition of the alias to create.
884+
* @return the created alias.
885+
*/
886+
public SearchAlias createAlias(SearchAlias alias) {
887+
return createAliasWithResponse(alias, Context.NONE).getValue();
888+
}
889+
890+
/**
891+
* Creates a new Azure Cognitive Search alias.
892+
*
893+
* <p><strong>Code Sample</strong></p>
894+
*
895+
* <p> Create the search alias named "my-alias". </p>
896+
*
897+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createAliasWithResponse#SearchAlias-Context -->
898+
* <pre>
899+
* Response&lt;SearchAlias&gt; response = searchIndexClient.createAliasWithResponse&#40;new SearchAlias&#40;&quot;my-alias&quot;,
900+
* Collections.singletonList&#40;&quot;index-to-alias&quot;&#41;&#41;, new Context&#40;key1, value1&#41;&#41;;
901+
*
902+
* System.out.printf&#40;&quot;Response status code %d. Created alias '%s' that aliases index '%s'.&quot;,
903+
* response.getStatusCode&#40;&#41;, response.getValue&#40;&#41;.getName&#40;&#41;, response.getValue&#40;&#41;.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
904+
* </pre>
905+
* <!-- end com.azure.search.documents.indexes.SearchClient.createAliasWithResponse#SearchAlias-Context -->
906+
*
907+
* @param alias definition of the alias to create.
908+
* @param context additional context that is passed through the HTTP pipeline during the service call
909+
* @return the created alias.
910+
*/
911+
public Response<SearchAlias> createAliasWithResponse(SearchAlias alias, Context context) {
912+
return asyncClient.createAliasWithResponse(alias, context).block();
913+
}
914+
915+
/**
916+
* Creates or updates an Azure Cognitive Search alias.
917+
*
918+
* <p><strong>Code Sample</strong></p>
919+
*
920+
* <p> Create then update the search alias named "my-alias". </p>
921+
*
922+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateAlias#SearchAlias -->
923+
* <pre>
924+
* SearchAlias searchAlias = searchIndexClient.createOrUpdateAlias&#40;
925+
* new SearchAlias&#40;&quot;my-alias&quot;, Collections.singletonList&#40;&quot;index-to-alias&quot;&#41;&#41;&#41;;
926+
*
927+
* System.out.printf&#40;&quot;Created alias '%s' that aliases index '%s'.&quot;, searchAlias.getName&#40;&#41;,
928+
* searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
929+
*
930+
* searchAlias = searchIndexClient.createOrUpdateAlias&#40;new SearchAlias&#40;searchAlias.getName&#40;&#41;,
931+
* Collections.singletonList&#40;&quot;new-index-to-alias&quot;&#41;&#41;&#41;;
932+
*
933+
* System.out.printf&#40;&quot;Updated alias '%s' to aliases index '%s'.&quot;, searchAlias.getName&#40;&#41;,
934+
* searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
935+
* </pre>
936+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateAlias#SearchAlias -->
937+
*
938+
* @param alias definition of the alias to create or update.
939+
* @return the created or updated alias.
940+
*/
941+
public SearchAlias createOrUpdateAlias(SearchAlias alias) {
942+
return createOrUpdateAliasWithResponse(alias, false, Context.NONE).getValue();
943+
}
944+
945+
/**
946+
* Creates or updates an Azure Cognitive Search alias.
947+
*
948+
* <p><strong>Code Sample</strong></p>
949+
*
950+
* <p> Create then update the search alias named "my-alias". </p>
951+
*
952+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateAliasWithResponse#SearchAlias-boolean-Context -->
953+
* <pre>
954+
* Response&lt;SearchAlias&gt; response = searchIndexClient.createOrUpdateAliasWithResponse&#40;
955+
* new SearchAlias&#40;&quot;my-alias&quot;, Collections.singletonList&#40;&quot;index-to-alias&quot;&#41;&#41;, false, new Context&#40;key1, value1&#41;&#41;;
956+
*
957+
* System.out.printf&#40;&quot;Response status code %d. Created alias '%s' that aliases index '%s'.&quot;,
958+
* response.getStatusCode&#40;&#41;, response.getValue&#40;&#41;.getName&#40;&#41;, response.getValue&#40;&#41;.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
959+
*
960+
* response = searchIndexClient.createOrUpdateAliasWithResponse&#40;
961+
* new SearchAlias&#40;response.getValue&#40;&#41;.getName&#40;&#41;, Collections.singletonList&#40;&quot;new-index-to-alias&quot;&#41;&#41;
962+
* .setETag&#40;response.getValue&#40;&#41;.getETag&#40;&#41;&#41;, true, new Context&#40;key1, value1&#41;&#41;;
963+
*
964+
* System.out.printf&#40;&quot;Response status code %d. Updated alias '%s' that aliases index '%s'.&quot;,
965+
* response.getStatusCode&#40;&#41;, response.getValue&#40;&#41;.getName&#40;&#41;, response.getValue&#40;&#41;.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
966+
* </pre>
967+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateAliasWithResponse#SearchAlias-boolean-Context -->
968+
*
969+
* @param alias definition of the alias to create or update.
970+
* @param onlyIfUnchanged only update the alias if the eTag matches the alias on the service.
971+
* @param context additional context that is passed through the HTTP pipeline during the service call
972+
* @return the created or updated alias.
973+
*/
974+
public Response<SearchAlias> createOrUpdateAliasWithResponse(SearchAlias alias, boolean onlyIfUnchanged,
975+
Context context) {
976+
return asyncClient.createOrUpdateAliasWithResponse(alias, onlyIfUnchanged ? alias.getETag() : null, context)
977+
.block();
978+
}
979+
980+
/**
981+
* Gets the Azure Cognitive Search alias.
982+
*
983+
* <p><strong>Code Sample</strong></p>
984+
*
985+
* <p> Get the search alias named "my-alias". </p>
986+
*
987+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getAlias#String -->
988+
* <pre>
989+
* SearchAlias searchAlias = searchIndexClient.getAlias&#40;&quot;my-alias&quot;&#41;;
990+
*
991+
* System.out.printf&#40;&quot;Retrieved alias '%s' that aliases index '%s'.&quot;, searchAlias.getName&#40;&#41;,
992+
* searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
993+
* </pre>
994+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getAlias#String -->
995+
*
996+
* @param aliasName name of the alias to get.
997+
* @return the retrieved alias.
998+
*/
999+
public SearchAlias getAlias(String aliasName) {
1000+
return getAliasWithResponse(aliasName, Context.NONE).getValue();
1001+
}
1002+
1003+
/**
1004+
* Gets the Azure Cognitive Search alias.
1005+
*
1006+
* <p><strong>Code Sample</strong></p>
1007+
*
1008+
* <p> Get the search alias named "my-alias". </p>
1009+
*
1010+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.getAliasWithResponse#String-Context -->
1011+
* <pre>
1012+
* Response&lt;SearchAlias&gt; response = searchIndexClient.getAliasWithResponse&#40;&quot;my-alias&quot;, new Context&#40;key1, value1&#41;&#41;;
1013+
*
1014+
* System.out.printf&#40;&quot;Response status code %d. Retrieved alias '%s' that aliases index '%s'.&quot;,
1015+
* response.getStatusCode&#40;&#41;, response.getValue&#40;&#41;.getName&#40;&#41;, response.getValue&#40;&#41;.getIndexes&#40;&#41;.get&#40;0&#41;&#41;;
1016+
* </pre>
1017+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.getAliasWithResponse#String-Context -->
1018+
*
1019+
* @param aliasName name of the alias to get.
1020+
* @param context additional context that is passed through the HTTP pipeline during the service call
1021+
* @return the retrieved alias.
1022+
*/
1023+
public Response<SearchAlias> getAliasWithResponse(String aliasName, Context context) {
1024+
return asyncClient.getAliasWithResponse(aliasName, context).block();
1025+
}
1026+
1027+
/**
1028+
* Deletes the Azure Cognitive Search alias.
1029+
*
1030+
* <p><strong>Code Sample</strong></p>
1031+
*
1032+
* <p> Delete the search alias named "my-alias". </p>
1033+
*
1034+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteAlias#String -->
1035+
* <pre>
1036+
* searchIndexClient.deleteAlias&#40;&quot;my-alias&quot;&#41;;
1037+
*
1038+
* System.out.println&#40;&quot;Deleted alias 'my-alias'.&quot;&#41;;
1039+
* </pre>
1040+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteAlias#String -->
1041+
*
1042+
* @param aliasName name of the alias to delete.
1043+
*/
1044+
public void deleteAlias(String aliasName) {
1045+
asyncClient.deleteAliasWithResponse(aliasName, null, Context.NONE).block();
1046+
}
1047+
1048+
/**
1049+
* Deletes the Azure Cognitive Search alias.
1050+
*
1051+
* <p><strong>Code Sample</strong></p>
1052+
*
1053+
* <p> Delete the search alias named "my-alias". </p>
1054+
*
1055+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.deleteAliasWithResponse#SearchAlias-boolean-Context -->
1056+
* <pre>
1057+
* SearchAlias searchAlias = searchIndexClient.getAlias&#40;&quot;my-alias&quot;&#41;;
1058+
*
1059+
* Response&lt;Void&gt; response = searchIndexClient.deleteAliasWithResponse&#40;searchAlias, true,
1060+
* new Context&#40;key1, value1&#41;&#41;;
1061+
*
1062+
* System.out.printf&#40;&quot;Response status code %d. Deleted alias 'my-alias'.&quot;, response.getStatusCode&#40;&#41;&#41;;
1063+
* </pre>
1064+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.deleteAliasWithResponse#SearchAlias-boolean-Context -->
1065+
*
1066+
* @param alias the alias to delete.
1067+
* @param onlyIfUnchanged only delete the alias if the eTag matches the alias on the service.
1068+
* @param context additional context that is passed through the HTTP pipeline during the service call
1069+
* @return a response indicating the alias has been deleted.
1070+
*/
1071+
public Response<Void> deleteAliasWithResponse(SearchAlias alias, boolean onlyIfUnchanged, Context context) {
1072+
return asyncClient.deleteAliasWithResponse(alias.getName(), onlyIfUnchanged ? alias.getETag() : null, context)
1073+
.block();
1074+
}
1075+
1076+
/**
1077+
* Lists all aliases in the Azure Cognitive Search service.
1078+
*
1079+
* <p><strong>Code Sample</strong></p>
1080+
*
1081+
* <p> List aliases </p>
1082+
*
1083+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listAliases -->
1084+
* <pre>
1085+
* searchIndexClient.listAliases&#40;&#41;
1086+
* .forEach&#40;searchAlias -&gt; System.out.printf&#40;&quot;Listed alias '%s' that aliases index '%s'.&quot;,
1087+
* searchAlias.getName&#40;&#41;, searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;&#41;;
1088+
* </pre>
1089+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listAliases -->
1090+
*
1091+
* @return a list of aliases in the service.
1092+
*/
1093+
public PagedIterable<SearchAlias> listAliases() {
1094+
return listAliases(Context.NONE);
1095+
}
1096+
1097+
/**
1098+
* Lists all aliases in the Azure Cognitive Search service.
1099+
*
1100+
* <p><strong>Code Sample</strong></p>
1101+
*
1102+
* <p> List aliases </p>
1103+
*
1104+
* <!-- src_embed com.azure.search.documents.indexes.SearchIndexClient.listAliases#Context -->
1105+
* <pre>
1106+
* searchIndexClient.listAliases&#40;new Context&#40;key1, value1&#41;&#41;
1107+
* .forEach&#40;searchAlias -&gt; System.out.printf&#40;&quot;Listed alias '%s' that aliases index '%s'.&quot;,
1108+
* searchAlias.getName&#40;&#41;, searchAlias.getIndexes&#40;&#41;.get&#40;0&#41;&#41;&#41;;
1109+
* </pre>
1110+
* <!-- end com.azure.search.documents.indexes.SearchIndexClient.listAliases#Context -->
1111+
*
1112+
* @param context additional context that is passed through the HTTP pipeline during the service call
1113+
* @return a list of aliases in the service.
1114+
*/
1115+
public PagedIterable<SearchAlias> listAliases(Context context) {
1116+
return new PagedIterable<>(asyncClient.listAliases(context));
1117+
}
8661118
}

0 commit comments

Comments
 (0)