From 786f26431bbc32f84f176e9b1292f49a46467559 Mon Sep 17 00:00:00 2001 From: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> Date: Mon, 5 Jun 2023 10:57:57 -0400 Subject: [PATCH] Properly set Index and Search Analyzer names in FieldBuilder (#35222) --- .../implementation/util/FieldBuilder.java | 4 +- .../documents/indexes/FieldBuilderTests.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/FieldBuilder.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/FieldBuilder.java index 6cf005fb0b9ef..ee8ef609f07a6 100644 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/FieldBuilder.java +++ b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/implementation/util/FieldBuilder.java @@ -340,8 +340,8 @@ private static SearchField enrichWithAnnotation(SearchField searchField, Member if (hasAnalyzerName) { searchField.setAnalyzerName(LexicalAnalyzerName.fromString(analyzerName)); } else if (hasSearchAnalyzerName || hasIndexAnalyzerName) { - searchField.setAnalyzerName(LexicalAnalyzerName.fromString(searchAnalyzerName)); - searchField.setAnalyzerName(LexicalAnalyzerName.fromString(indexAnalyzerName)); + searchField.setSearchAnalyzerName(LexicalAnalyzerName.fromString(searchAnalyzerName)); + searchField.setIndexAnalyzerName(LexicalAnalyzerName.fromString(indexAnalyzerName)); } if (hasNormalizerName) { diff --git a/sdk/search/azure-search-documents/src/test/java/com/azure/search/documents/indexes/FieldBuilderTests.java b/sdk/search/azure-search-documents/src/test/java/com/azure/search/documents/indexes/FieldBuilderTests.java index 309fed997dead..ce554dc03d03d 100644 --- a/sdk/search/azure-search-documents/src/test/java/com/azure/search/documents/indexes/FieldBuilderTests.java +++ b/sdk/search/azure-search-documents/src/test/java/com/azure/search/documents/indexes/FieldBuilderTests.java @@ -32,6 +32,7 @@ import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -295,6 +296,42 @@ public static final class MissingFunctionalityNormalizer { public String rightTypeWrongFunctionality; } + @Test + public void onlyAnalyzerNameSetsOnlyAnalyzerName() { + List fields = SearchIndexClient.buildSearchFields(OnlyAnalyzerName.class, null); + + assertEquals(1, fields.size()); + + SearchField field = fields.get(0); + assertEquals("onlyAnalyzer", field.getAnalyzerName().toString()); + assertNull(field.getIndexAnalyzerName()); + assertNull(field.getSearchAnalyzerName()); + } + + @SuppressWarnings("unused") + public static final class OnlyAnalyzerName { + @SearchableField(analyzerName = "onlyAnalyzer") + public String onlyAnalyzer; + } + + @Test + public void indexAndSearchAnalyzersSetCorrectly() { + List fields = SearchIndexClient.buildSearchFields(IndexAndSearchAnalyzerNames.class, null); + + assertEquals(1, fields.size()); + + SearchField field = fields.get(0); + assertNull(field.getAnalyzerName()); + assertEquals("indexAnalyzer", field.getIndexAnalyzerName().toString()); + assertEquals("searchAnalyzer", field.getSearchAnalyzerName().toString()); + } + + @SuppressWarnings("unused") + public static final class IndexAndSearchAnalyzerNames { + @SearchableField(indexAnalyzerName = "indexAnalyzer", searchAnalyzerName = "searchAnalyzer") + public String indexAndSearchAnalyzer; + } + private void assertListFieldEquals(List expected, List actual) { assertEquals(expected.size(), actual.size()); for (int i = 0; i < expected.size(); i++) {