diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/Utility.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/Utility.java index 78cd47269fb2..bb80b2eabeda 100644 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/Utility.java +++ b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/Utility.java @@ -40,6 +40,10 @@ import reactor.core.publisher.Mono; import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; @@ -169,6 +173,14 @@ public static synchronized String formatCoordinate(double coordinate) { return COORDINATE_FORMATTER.format(coordinate); } + public static String readSynonymsFromFile(Path filePath) { + try { + return new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8); + } catch (IOException ex) { + throw new ClientLogger(Utility.class).logExceptionAsError(new UncheckedIOException(ex)); + } + } + private Utility() { } } diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SynonymMap.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SynonymMap.java index 925b9d462cd5..c0ed2c3afeec 100644 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SynonymMap.java +++ b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SynonymMap.java @@ -159,4 +159,17 @@ public SynonymMap setETag(String eTag) { this.eTag = eTag; return this; } + + /** + * Creates a new instance of SynonymMap with synonyms read from the passed file. + * + * @param name The name of the synonym map. + * @param filePath The path to the file where the formatted synonyms are read. + * @return A SynonymMap. + * @throws java.io.UncheckedIOException If reading {@code filePath} fails. + */ + public static SynonymMap createFromFile(String name, java.nio.file.Path filePath) { + String synonyms = com.azure.search.documents.implementation.util.Utility.readSynonymsFromFile(filePath); + return new SynonymMap(name, synonyms); + } } diff --git a/sdk/search/azure-search-documents/swagger/src/main/java/SearchServiceCustomizations.java b/sdk/search/azure-search-documents/swagger/src/main/java/SearchServiceCustomizations.java index 62131c6c4849..2d7275786699 100644 --- a/sdk/search/azure-search-documents/swagger/src/main/java/SearchServiceCustomizations.java +++ b/sdk/search/azure-search-documents/swagger/src/main/java/SearchServiceCustomizations.java @@ -193,6 +193,20 @@ private void customizeSynonymMap(ClassCustomization classCustomization) { .setDescription("Constructor of {@link SynonymMap}.") .setParam("name", "The name of the synonym map.") .setParam("synonyms", "A series of synonym rules in the specified synonym map format. The rules must be separated by newlines."); + + classCustomization.addMethod(joinWithNewline( + "/**", + " * Creates a new instance of SynonymMap with synonyms read from the passed file.", + " *", + " * @param name The name of the synonym map.", + " * @param filePath The path to the file where the formatted synonyms are read.", + " * @return A SynonymMap.", + " * @throws java.io.UncheckedIOException If reading {@code filePath} fails.", + " */", + "public static SynonymMap createFromFile(String name, java.nio.file.Path filePath) {", + " String synonyms = com.azure.search.documents.implementation.util.Utility.readSynonymsFromFile(filePath);", + " return new SynonymMap(name, synonyms);", + "}")); } private void customizeSearchResourceEncryptionKey(ClassCustomization keyCustomization,