From 6b91075770d5136b9d961dd7c79bcc538b2d813f Mon Sep 17 00:00:00 2001 From: Srikanta Date: Fri, 3 Jul 2020 00:11:00 -0700 Subject: [PATCH 1/3] Remove search model builder classes --- sdk/search/azure-search-documents/README.md | 128 +++++++-------- .../indexes/models/ComplexFieldBuilder.java | 55 ------- .../models/SearchableFieldBuilder.java | 144 ---------------- .../indexes/models/SimpleFieldBuilder.java | 154 ------------------ .../azure/search/documents/ReadmeSamples.java | 54 +++--- .../documents/SearchJavaDocCodeSnippets.java | 18 +- .../documents/indexes/FieldBuilderTests.java | 69 +++++--- 7 files changed, 136 insertions(+), 486 deletions(-) delete mode 100644 sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/ComplexFieldBuilder.java delete mode 100644 sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchableFieldBuilder.java delete mode 100644 sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SimpleFieldBuilder.java diff --git a/sdk/search/azure-search-documents/README.md b/sdk/search/azure-search-documents/README.md index 3105eed5d246..c8872b406d94 100644 --- a/sdk/search/azure-search-documents/README.md +++ b/sdk/search/azure-search-documents/README.md @@ -102,7 +102,7 @@ The SDK provides three clients. To create a `SearchIndexClient/SearchIndexAsyncClient`, you will need the values of the Azure Cognitive Search service URL endpoint and admin key. - + ```Java SearchIndexClient searchIndexClient = new SearchIndexClientBuilder() .endpoint(endpoint) @@ -112,7 +112,7 @@ SearchIndexClient searchIndexClient = new SearchIndexClientBuilder() or - + ```Java SearchIndexAsyncClient searchIndexAsyncClient = new SearchIndexClientBuilder() .endpoint(endpoint) @@ -125,7 +125,7 @@ SearchIndexAsyncClient searchIndexAsyncClient = new SearchIndexClientBuilder() To create a `SearchIndexerClient/SearchIndexerAsyncClient`, you will need the values of the Azure Cognitive Search service URL endpoint and admin key. - + ```Java SearchIndexerClient searchIndexerClient = new SearchIndexerClientBuilder() .endpoint(endpoint) @@ -135,7 +135,7 @@ SearchIndexerClient searchIndexerClient = new SearchIndexerClientBuilder() or - + ```Java SearchIndexerAsyncClient searchIndexerAsyncClient = new SearchIndexerClientBuilder() .endpoint(endpoint) @@ -148,7 +148,7 @@ SearchIndexerAsyncClient searchIndexerAsyncClient = new SearchIndexerClientBuild Once you have the values of the Azure Cognitive Search service URL endpoint and admin key, you can create the `SearchClient/SearchAsyncClient` with an existing index name: - + ```Java SearchClient searchClient = new SearchClientBuilder() .endpoint(endpoint) @@ -159,7 +159,7 @@ SearchClient searchClient = new SearchClientBuilder() or - + ```Java SearchAsyncClient searchAsyncClient = new SearchClientBuilder() .endpoint(endpoint) @@ -174,7 +174,7 @@ To get running immediately, we're going to connect to a well-known sandbox Search service provided by Microsoft. This means you do not need an Azure subscription or Azure Cognitive Search service to try out this query. - + ```Java // We'll connect to the Azure Cognitive Search public sandbox and send a // query to its "nycjobs" index built from a public dataset of available jobs @@ -258,7 +258,7 @@ Let's explore them with a search for a "luxury" hotel. provide your own. Here we perform the search, enumerate over the results, and extract data using `SearchDocument`'s dictionary indexer. - + ```Java SearchPagedIterable searchResultsIterable = searchClient.search("luxury"); for (SearchResult searchResult: searchResultsIterable) { @@ -271,7 +271,7 @@ for (SearchResult searchResult: searchResultsIterable) { #### Use Java model class for search results Define a `Hotel` class. - + ```Java public class Hotel { private String id; @@ -298,7 +298,7 @@ public class Hotel { ``` And use them in place of `SearchDocument` when querying. - + ```Java SearchPagedIterable searchResultsIterable = searchClient.search("luxury"); for (SearchResult searchResult: searchResultsIterable) { @@ -317,7 +317,7 @@ is recommended. The `SearchOptions` provide powerful control over the behavior of our queries. Let's search for the top 5 luxury hotels with a good rating. - + ```Java int stars = 4; SearchOptions options = new SearchOptions() @@ -334,58 +334,54 @@ You can use the [`SearchIndexClient`](#Create-a-SearchIndexClient) to create a s defined using convenient `SimpleField`, `SearchableField`, or `ComplexField` classes. Indexes can also define suggesters, lexical analyzers, and more. - + ```Java - searchFieldList.add(new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false) - .setKey(true) - .setFilterable(true) - .setSortable(true) - .build()); - searchFieldList.add(new SearchableFieldBuilder("hotelName", false) - .setFilterable(true) - .setSortable(true) - .build()); - searchFieldList.add(new SearchableFieldBuilder("description", false) - .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE) - .build()); - searchFieldList.add(new SearchableFieldBuilder("tags", true) - .setKey(true) - .setFilterable(true) - .setFacetable(true) - .build()); - searchFieldList.add(new ComplexFieldBuilder("address", false) - .setFields(Arrays.asList( - new SearchableFieldBuilder("streetAddress", false).build(), - new SearchableFieldBuilder("city", false) - .setFilterable(true) - .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("stateProvince", false) - .setFilterable(true) - .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("country", false) - .setFilterable(true) - .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("postalCode", false) - .setFilterable(true) - .setFacetable(true) - .setSortable(true) - .build() - )) - .build()); - // Prepare suggester. - SearchSuggester suggester = new SearchSuggester("sg", Collections.singletonList("hotelName")); - // Prepare SearchIndex with index name and search fields. - SearchIndex index = new SearchIndex("hotels").setFields(searchFieldList).setSuggesters( - Collections.singletonList(suggester)); - // Create an index - searchIndexClient.createIndex(index); - } + List searchFieldList = new ArrayList<>(); + searchFieldList.add(new SearchField("hotelId", SearchFieldDataType.STRING) + .setKey(true) + .setFilterable(true) + .setSortable(true)); + + searchFieldList.add(new SearchField("hotelName", SearchFieldDataType.STRING) + .setFilterable(true) + .setSortable(true)); + + searchFieldList.add(new SearchField("description", SearchFieldDataType.STRING) + .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE)); + + searchFieldList.add(new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)) + .setKey(true) + .setFilterable(true) + .setFacetable(true)); + + searchFieldList.add(new SearchField("address", SearchFieldDataType.COMPLEX) + .setFields(Arrays.asList( + new SearchField("streetAddress", SearchFieldDataType.STRING), + new SearchField("city", SearchFieldDataType.STRING) + .setFilterable(true) + .setFacetable(true) + .setSortable(true), + new SearchField("stateProvince", SearchFieldDataType.STRING) + .setFilterable(true) + .setFacetable(true) + .setSortable(true), + new SearchField("country", SearchFieldDataType.STRING) + .setFilterable(true) + .setFacetable(true) + .setSortable(true), + new SearchField("postalCode", SearchFieldDataType.STRING) + .setFilterable(true) + .setFacetable(true) + .setSortable(true) + ))); + + // Prepare suggester. + SearchSuggester suggester = new SearchSuggester("sg", Collections.singletonList("hotelName")); + // Prepare SearchIndex with index name and search fields. + SearchIndex index = new SearchIndex("hotels").setFields(searchFieldList).setSuggesters( + Collections.singletonList(suggester)); + // Create an index + searchIndexClient.createIndex(index); } ``` @@ -396,7 +392,7 @@ you can retrieve a specific document from your index if you already know the key. You could get the key from a query, for example, and want to show more information about it or navigate your customer to that document. - + ```Java Hotel hotel = searchClient.getDocument("1", Hotel.class); System.out.printf("This is hotelId %s, and this is hotel name %s.%n", hotel.getId(), hotel.getName()); @@ -409,7 +405,7 @@ an index in a single batched request. There are [a few special rules for merging](https://docs.microsoft.com/rest/api/searchservice/addupdate-or-delete-documents#document-actions) to be aware of. - + ```Java IndexDocumentsBatch batch = new IndexDocumentsBatch(); batch.addUploadActions(new Hotel().setId("783").setName("Upload Inn")); @@ -428,7 +424,7 @@ with an `IndexDocumentsResult` for inspection. All of the examples so far have been using synchronous APIs, but we provide full support for async APIs as well. You'll need to use [SearchAsyncClient](#Create-a-SearchClient) - + ```Java searchAsyncClient.search("luxury") .subscribe(result -> { @@ -452,7 +448,7 @@ Any Search API operation that fails will throw a [`HttpResponseException`][HttpResponseException] with helpful [`Status codes`][status_codes]. Many of these errors are recoverable. - + ```Java try { Iterable results = searchClient.search("hotel"); diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/ComplexFieldBuilder.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/ComplexFieldBuilder.java deleted file mode 100644 index b558ba5ae42a..000000000000 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/ComplexFieldBuilder.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.search.documents.indexes.models; - -import java.util.List; - -/** - * A helper Field model to build a complex field which uses {@code SearchFieldDataType.EDM_COMPLEX_TYPE} or - * collection of {@code SearchFieldDataType.EDM_COMPLEX_TYPE}. - */ -public class ComplexFieldBuilder extends SearchFieldBase { - private List fields; - - /** - * Initializes a new instance of the {@link ComplexFieldBuilder} class. - * - * @param name The name of the field, which must be unique within the index or parent field. - * @param collection Whether the field is a collection of strings. - */ - public ComplexFieldBuilder(String name, boolean collection) { - super(name, collection ? SearchFieldDataType.collection(SearchFieldDataType.COMPLEX) - : SearchFieldDataType.COMPLEX); - } - - /** - * Gets a collection of {@link SimpleFieldBuilder} or {@link ComplexFieldBuilder} child fields. - * - * @return The list of sub-fields. - */ - public List getFields() { - return fields; - } - - /** - * Sets a collection of {@link SimpleFieldBuilder} or {@link ComplexFieldBuilder} child fields. - * - * @param fields The list of sub-fields. - * @return The {@link ComplexFieldBuilder} object itself. - */ - public ComplexFieldBuilder setFields(List fields) { - this.fields = fields; - return this; - } - - /** - * Convert ComplexField to {@link SearchField}. - * - * @return The {@link SearchField} object. - */ - public SearchField build() { - return new SearchField(super.getName(), super.getDataType()) - .setFields(fields); - } -} diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchableFieldBuilder.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchableFieldBuilder.java deleted file mode 100644 index 06426b799749..000000000000 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchableFieldBuilder.java +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.search.documents.indexes.models; - -import java.util.List; - -/** - * A helper Field model to build a searchable {@link SearchField}. - */ -public class SearchableFieldBuilder extends SimpleFieldBuilder { - private LexicalAnalyzerName analyzerName; - private LexicalAnalyzerName searchAnalyzerName; - private LexicalAnalyzerName indexAnalyzerName; - private List synonymMapNames; - - /** - * Initializes a new instance of the {@link SearchableFieldBuilder} class. - * - * @param name The name of the field, which must be unique within the index or parent field. - * @param collection Whether the field is a collection of strings. - * @throws NullPointerException when {@code name} is null. - */ - public SearchableFieldBuilder(String name, boolean collection) { - super(name, SearchFieldDataType.STRING, collection); - } - - /** - * Gets the name of the language analyzer. This property cannot be set when either {@code searchAnalyzer} or - * {@code indexAnalyzer} are set. Once the analyzer is chosen, it cannot be changed for the field in the index. - * - * @return The {@link LexicalAnalyzerName} used for analyzer. - */ - public LexicalAnalyzerName getAnalyzerName() { - return analyzerName; - } - - /** - * Sets the name of the language analyzer. This property cannot be set when either {@code searchAnalyzer} or - * {@code indexAnalyzer} are set. Once the analyzer is chosen, it cannot be changed for the field in the index. - * - * @param analyzerName The {@link LexicalAnalyzerName} used for analyzer. - * @return The SearchableFieldBuilder object itself. - */ - public SearchableFieldBuilder setAnalyzerName(LexicalAnalyzerName analyzerName) { - this.analyzerName = analyzerName; - return this; - } - - /** - * Gets the name of the language analyzer for searching. This property must be set together with - * {@code indexAnalyzer}, and cannot be set when {@code analyzer} is set. Once the analyzer is chosen, it cannot be - * changed for the field in the index. - * - * @return The {@link LexicalAnalyzerName} used for search analyzer. - */ - public LexicalAnalyzerName getSearchAnalyzerName() { - return searchAnalyzerName; - } - - /** - * Sets the name of the language analyzer for searching. This property must be set together with - * {@code indexAnalyzer}, and cannot be set when {@code analyzer} is set. Once the analyzer is chosen, it cannot be - * changed for the field in the index. - * - * @param searchAnalyzerName The {@link LexicalAnalyzerName} used for search analyzer. - * @return The SearchableField object itself. - */ - public SearchableFieldBuilder setSearchAnalyzerName(LexicalAnalyzerName searchAnalyzerName) { - this.searchAnalyzerName = searchAnalyzerName; - return this; - } - - /** - * Gets the name of the language analyzer for indexing. This property must be set together with - * {@code searchAnalyzer}, and cannot be set when {@code analyzer} is set. Once the analyzer is chosen, it cannot be - * changed for the field in the index. - * - * @return The {@link LexicalAnalyzerName} used for index analyzer. - */ - public LexicalAnalyzerName getIndexAnalyzerName() { - return indexAnalyzerName; - } - - /** - * Gets the name of the language analyzer for indexing. This property must be set together with - * {@code searchAnalyzer}, and cannot be set when {@code analyzer} is set. Once the analyzer is chosen, it cannot be - * changed for the field in the index. - * - * @param indexAnalyzerName The {@link LexicalAnalyzerName} used for index analyzer. - * @return The SearchableField object itself. - */ - public SearchableFieldBuilder setIndexAnalyzerName(LexicalAnalyzerName indexAnalyzerName) { - this.indexAnalyzerName = indexAnalyzerName; - return this; - } - - /** - * Gets a list of names of synonym maps to associate with this field. - * Currently, only one synonym map per field is supported. - * - * Assigning a synonym map to a field ensures that query terms targeting that field are expanded at query-time using - * the rules in the synonym map. This attribute can be changed on existing fields. - * - * @return List of names of synonym maps to associate with this field. - */ - public List getSynonymMapNames() { - return synonymMapNames; - } - - /** - * Sets a list of names of synonym maps to associate with this field. - * Currently, only one synonym map per field is supported. - * - * Assigning a synonym map to a field ensures that query terms targeting that field are expanded at query-time using - * the rules in the synonym map. This attribute can be changed on existing fields. - * - * @param synonymMapNames list of names of synonym maps to associate with this field. - * @return The SearchableField object itself. - */ - public SearchableFieldBuilder setSynonymMapNames(List synonymMapNames) { - this.synonymMapNames = synonymMapNames; - return this; - } - - /** - * Convert SearchableField to {@link SearchField}. - * - * @return The {@link SearchField} object. - */ - public SearchField build() { - return new SearchField(super.getName(), super.getDataType()) - .setSearchable(true) - .setKey(super.isKey()) - .setSortable(super.isSortable()) - .setFilterable(super.isFilterable()) - .setHidden(super.isHidden()) - .setFacetable(super.isFacetable()) - .setAnalyzerName(this.analyzerName) - .setSearchAnalyzerName(this.searchAnalyzerName) - .setIndexAnalyzerName(this.indexAnalyzerName) - .setSynonymMapNames(this.synonymMapNames); - } -} diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SimpleFieldBuilder.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SimpleFieldBuilder.java deleted file mode 100644 index 905335b57b7c..000000000000 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SimpleFieldBuilder.java +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.search.documents.indexes.models; - -/** - * A helper Field model to build a simple {@link SearchField}. - */ -public class SimpleFieldBuilder extends SearchFieldBase { - private boolean key; - private boolean facetable; - private boolean sortable; - private boolean filterable; - private boolean hidden; - - /** - * Initializes a new instance of the {@link SimpleFieldBuilder} class. - * - * @param name The name of the field, which must be unique within the index or parent field. - * @param dataType The {@link SearchFieldDataType} of the {@link SearchField}. - * @param collection boolean field to indicate whether the dataType is collection. - * @throws NullPointerException when {@code name} is null. - */ - public SimpleFieldBuilder(String name, SearchFieldDataType dataType, boolean collection) { - super(name, collection ? SearchFieldDataType.collection(dataType) : dataType); - } - - /** - * Gets whether the field is the key field. - * - * @return An {@link SearchIndex} must have exactly one key field of type {@code DataType.EDM_STRING}. - */ - public boolean isKey() { - return key; - } - - /** - * Sets whether the field is the key field. The default is false. - * - * @param key boolean to indicate whether the field is key field or not. - * @return The SimpleField object itself. - */ - public SimpleFieldBuilder setKey(boolean key) { - this.key = key; - return this; - } - - /** - * Gets a value indicating whether to enable the field can be referenced in {@code $orderby} expressions. - * By default Azure Cognitive Search sorts results by score, but in many experiences users may want to sort by - * fields in the documents. - * - * @return The boolean to indicate whether the field is sortable or not. - */ - public boolean isSortable() { - return sortable; - } - - /** - * Sets a value indicating whether to enable the field can be referenced in {@code $orderby} expressions. - * The default is false. - * By default Azure Cognitive Search sorts results by score, but in many experiences users may want to sort by - * fields in the documents. - * - * @param sortable The boolean to indicate whether the field is sortable or not. - * @return The SimpleField object itself. - */ - public SimpleFieldBuilder setSortable(boolean sortable) { - this.sortable = sortable; - return this; - } - - /** - * Gets or sets a value indicating whether the field can be referenced in {@code $filter} queries. - * - * @return The boolean to indicate whether the field is filterable or not. - */ - public boolean isFilterable() { - return filterable; - } - - /** - * Gets or sets a value indicating whether the field can be referenced in {@code $filter} queries. - * The default is false. - * - * @param filterable The boolean to indicate whether the field is filterable or not. - * @return The SimpleField object itself. - */ - public SimpleFieldBuilder setFilterable(boolean filterable) { - this.filterable = filterable; - return this; - } - - /** - * Gets whether the field is returned in search results. - * - * @return The boolean to indicate whether the field is hidden or not. - */ - public boolean isHidden() { - return hidden; - } - - /** - * Sets whether the field is returned in search results. The default is false. - * A key field where {@code key} is true must have this property set to false. - * - * @param hidden The boolean to indicate whether the field is hidden or not. - * @return The SimpleField object itself. - */ - public SimpleFieldBuilder setHidden(boolean hidden) { - this.hidden = hidden; - return this; - } - - /** - * Gets a value indicating whether the field can be retrieved in facet queries. - * Facets are used in presentation of search results that include hit counts by categories. - * For example, in a search for digital cameras, facets might include branch, megapixels, price, etc. - * - * @return The boolean to indicate whether the field is facetable or not. - */ - public boolean isFacetable() { - return facetable; - } - - /** - * Sets a value indicating whether the field can be retrieved in facet queries. The default is false. - * Facets are used in presentation of search results that include hit counts by categories. - * For example, in a search for digital cameras, facets might include branch, megapixels, price, etc. - * - * @param facetable The boolean to indicate whether the field is facetable or not. - * @return The SimpleField object itself. - */ - public SimpleFieldBuilder setFacetable(boolean facetable) { - this.facetable = facetable; - return this; - } - - /** - * Convert SimpleField to {@link SearchField}. - * - * @return The {@link SearchField} object. - */ - public SearchField build() { - return new SearchField(super.getName(), super.getDataType()) - .setKey(key) - .setSearchable(false) - .setSortable(sortable) - .setFilterable(filterable) - .setHidden(hidden) - .setFacetable(facetable) - .setHidden(hidden); - } -} diff --git a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java index 3d684fd1ae87..0c18d364f895 100644 --- a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java +++ b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java @@ -14,15 +14,12 @@ import com.azure.search.documents.indexes.SearchIndexerAsyncClient; import com.azure.search.documents.indexes.SearchIndexerClient; import com.azure.search.documents.indexes.SearchIndexerClientBuilder; -import com.azure.search.documents.indexes.models.ComplexFieldBuilder; import com.azure.search.documents.indexes.models.IndexDocumentsBatch; import com.azure.search.documents.indexes.models.LexicalAnalyzerName; import com.azure.search.documents.indexes.models.SearchField; import com.azure.search.documents.indexes.models.SearchFieldDataType; import com.azure.search.documents.indexes.models.SearchIndex; import com.azure.search.documents.indexes.models.SearchSuggester; -import com.azure.search.documents.indexes.models.SearchableFieldBuilder; -import com.azure.search.documents.indexes.models.SimpleFieldBuilder; import com.azure.search.documents.models.SearchOptions; import com.azure.search.documents.models.SearchResult; import com.azure.search.documents.util.SearchPagedIterable; @@ -221,50 +218,45 @@ public void batchDocumentsOperations() { } public void createIndex() { - // Prepare SearchFields with SimpleFieldBuilder, SearchableFieldBuilder and ComplexFieldBuilder. List searchFieldList = new ArrayList<>(); - searchFieldList.add(new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false) + searchFieldList.add(new SearchField("hotelId", SearchFieldDataType.STRING) .setKey(true) .setFilterable(true) - .setSortable(true) - .build()); - searchFieldList.add(new SearchableFieldBuilder("hotelName", false) + .setSortable(true)); + + searchFieldList.add(new SearchField("hotelName", SearchFieldDataType.STRING) .setFilterable(true) - .setSortable(true) - .build()); - searchFieldList.add(new SearchableFieldBuilder("description", false) - .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE) - .build()); - searchFieldList.add(new SearchableFieldBuilder("tags", true) + .setSortable(true)); + + searchFieldList.add(new SearchField("description", SearchFieldDataType.STRING) + .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE)); + + searchFieldList.add(new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)) .setKey(true) .setFilterable(true) - .setFacetable(true) - .build()); - searchFieldList.add(new ComplexFieldBuilder("address", false) + .setFacetable(true)); + + searchFieldList.add(new SearchField("address", SearchFieldDataType.COMPLEX) .setFields(Arrays.asList( - new SearchableFieldBuilder("streetAddress", false).build(), - new SearchableFieldBuilder("city", false) + new SearchField("streetAddress", SearchFieldDataType.STRING), + new SearchField("city", SearchFieldDataType.STRING) .setFilterable(true) .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("stateProvince", false) + .setSortable(true), + new SearchField("stateProvince", SearchFieldDataType.STRING) .setFilterable(true) .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("country", false) + .setSortable(true), + new SearchField("country", SearchFieldDataType.STRING) .setFilterable(true) .setFacetable(true) - .setSortable(true) - .build(), - new SearchableFieldBuilder("postalCode", false) + .setSortable(true), + new SearchField("postalCode", SearchFieldDataType.STRING) .setFilterable(true) .setFacetable(true) .setSortable(true) - .build() - )) - .build()); + ))); + // Prepare suggester. SearchSuggester suggester = new SearchSuggester("sg", Collections.singletonList("hotelName")); // Prepare SearchIndex with index name and search fields. diff --git a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java index 62c6b34a4ab3..bbc064b44fcc 100644 --- a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java +++ b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java @@ -33,8 +33,6 @@ import com.azure.search.documents.indexes.models.SearchIndexerStatus; import com.azure.search.documents.indexes.models.SearchServiceStatistics; import com.azure.search.documents.indexes.models.SearchSuggester; -import com.azure.search.documents.indexes.models.SearchableFieldBuilder; -import com.azure.search.documents.indexes.models.SimpleFieldBuilder; import com.azure.search.documents.indexes.models.SynonymMap; import com.azure.search.documents.models.AutocompleteItem; import com.azure.search.documents.models.AutocompleteMode; @@ -789,8 +787,8 @@ public void createSearchIndexClientFromBuilder() { public void createSearchIndex() { // BEGIN: com.azure.search.documents.indexes.SearchIndexClient.createIndex#SearchIndex List searchFields = Arrays.asList( - new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false).setKey(true).build(), - new SearchableFieldBuilder("hotelName", false).build() + new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), + new SearchField("hotelName", SearchFieldDataType.STRING) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); SearchIndex indexFromService = searchIndexClient.createIndex(searchIndex); @@ -805,8 +803,8 @@ public void createSearchIndex() { public void createSearchIndexWithResponse() { // BEGIN: com.azure.search.documents.indexes.SearchIndexClient.createIndexWithResponse#SearchIndex-Context List searchFields = Arrays.asList( - new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false).setKey(true).build(), - new SearchableFieldBuilder("hotelName", false).build() + new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), + new SearchField("hotelName", SearchFieldDataType.STRING) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); @@ -1199,8 +1197,8 @@ public void createSearchIndexAsyncClientFromBuilder() { public void createSearchIndexAsync() { // BEGIN: com.azure.search.documents.indexes.SearchIndexAsyncClient.createIndex#SearchIndex List searchFields = Arrays.asList( - new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false).setKey(true).build(), - new SearchableFieldBuilder("hotelName", false).build() + new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), + new SearchField("hotelName", SearchFieldDataType.STRING) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); searchIndexAsyncClient.createIndex(searchIndex) @@ -1216,8 +1214,8 @@ public void createSearchIndexAsync() { public void createSearchIndexWithResponseAsync() { // BEGIN: com.azure.search.documents.indexes.SearchIndexAsyncClient.createIndexWithResponse#SearchIndex List searchFields = Arrays.asList( - new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false).setKey(true).build(), - new SearchableFieldBuilder("hotelName", false).build() + new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), + new SearchField("hotelName", SearchFieldDataType.STRING) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); 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 11377b669a94..a0b60ca93ac7 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 @@ -4,11 +4,8 @@ package com.azure.search.documents.indexes; import com.azure.search.documents.TestHelpers; -import com.azure.search.documents.indexes.models.ComplexFieldBuilder; import com.azure.search.documents.indexes.models.SearchField; import com.azure.search.documents.indexes.models.SearchFieldDataType; -import com.azure.search.documents.indexes.models.SearchableFieldBuilder; -import com.azure.search.documents.indexes.models.SimpleFieldBuilder; import com.azure.search.documents.test.environment.models.HotelAnalyzerException; import com.azure.search.documents.test.environment.models.HotelCircularDependencies; import com.azure.search.documents.test.environment.models.HotelSearchException; @@ -54,8 +51,16 @@ public void hotelCircularDependencies() { public void hotelWithEmptySynonymMaps() { // We cannot put null in the annotation. So no need to test null case. List actualFields = FieldBuilder.build(HotelWithEmptyInSynonymMaps.class); - List expectedFields = Collections.singletonList(new SearchableFieldBuilder("tags", true) - .setSynonymMapNames(Arrays.asList("asynonymMaps", "maps")).build()); + + List expectedFields = Collections.singletonList(new SearchField("tags", + SearchFieldDataType.collection(SearchFieldDataType.STRING)) + .setSearchable(true) + .setKey(false) + .setFilterable(false) + .setSortable(false) + .setFacetable(false) + .setSynonymMapNames(Arrays.asList("asynonymMaps", "maps"))); + assertListFieldEquals(expectedFields, actualFields); } @@ -95,43 +100,55 @@ private void assertExceptionMassageAndDataType(Exception exception, SearchFieldD } private List buildHotelCircularDependenciesModel() { - SearchField homeAddress = new ComplexFieldBuilder("homeAddress", false).setFields(buildHotelInAddress()).build(); - SearchField billingAddress = new ComplexFieldBuilder("billingAddress", false).setFields(buildHotelInAddress()).build(); + SearchField homeAddress = new SearchField("homeAddress", SearchFieldDataType.COMPLEX) + .setFields(buildHotelInAddress()); + SearchField billingAddress = new SearchField("billingAddress", SearchFieldDataType.COMPLEX) + .setFields(buildHotelInAddress()); return Arrays.asList(homeAddress, billingAddress); } private List buildHotelInAddress() { - SearchField hotel = new ComplexFieldBuilder("hotel", false).build(); + SearchField hotel = new SearchField("hotel", SearchFieldDataType.COMPLEX); return Collections.singletonList(hotel); } private List buildHotelWithArrayModel() { - SearchField hotelId = new SimpleFieldBuilder("hotelId", SearchFieldDataType.STRING, false).setKey(true) - .setSortable(true).build(); - SearchField tags = new SearchableFieldBuilder("tags", true).build(); + SearchField hotelId = new SearchField("hotelId", SearchFieldDataType.STRING) + .setKey(true) + .setSortable(true) + .setSearchable(false) + .setFacetable(false) + .setFilterable(false); + SearchField tags = new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)) + .setKey(false) + .setSearchable(true) + .setSortable(false) + .setFilterable(false) + .setFacetable(false); return Arrays.asList(hotelId, tags); } private List buildHotelAddressField() { - SearchField streetAddress = new SimpleFieldBuilder("streetAddress", SearchFieldDataType.STRING, false).setFacetable(true) - .setKey(true).build(); - SearchField city = new SearchableFieldBuilder("city", false).setFilterable(true).build(); - SearchField stateProvince = new SearchableFieldBuilder("stateProvince", false).build(); - SearchField country = new SearchableFieldBuilder("country", false) - .setSynonymMapNames(Arrays.asList("America -> USA", "USA -> US")).build(); - SearchField postalCode = new SimpleFieldBuilder("postalCode", SearchFieldDataType.STRING, false).build(); + SearchField streetAddress = new SearchField("streetAddress", SearchFieldDataType.STRING).setFacetable(true) + .setKey(true); + SearchField city = new SearchField("city", SearchFieldDataType.STRING).setFilterable(true); + SearchField stateProvince = new SearchField("stateProvince", SearchFieldDataType.STRING); + SearchField country = + new SearchField("country", SearchFieldDataType.STRING).setSynonymMapNames(Arrays.asList("America -> USA", + "USA -> US")); + SearchField postalCode = new SearchField("postalCode", SearchFieldDataType.STRING); return Arrays.asList(streetAddress, city, stateProvince, country, postalCode); } private List buildHotelRoomField() { - SearchField description = new SimpleFieldBuilder("description", SearchFieldDataType.STRING, false).build(); - SearchField descriptionFr = new SimpleFieldBuilder("descriptionFr", SearchFieldDataType.STRING, false).build(); - SearchField type = new SimpleFieldBuilder("type", SearchFieldDataType.STRING, false).build(); - SearchField baseRate = new SimpleFieldBuilder("baseRate", SearchFieldDataType.DOUBLE, false).build(); - SearchField bedOptions = new SimpleFieldBuilder("bedOptions", SearchFieldDataType.STRING, false).build(); - SearchField sleepsCount = new SimpleFieldBuilder("sleepsCount", SearchFieldDataType.INT32, false).build(); - SearchField smokingAllowed = new SimpleFieldBuilder("smokingAllowed", SearchFieldDataType.BOOLEAN, false).build(); - SearchField tags = new SimpleFieldBuilder("tags", SearchFieldDataType.STRING, true).build(); + SearchField description = new SearchField("description", SearchFieldDataType.STRING); + SearchField descriptionFr = new SearchField("descriptionFr", SearchFieldDataType.STRING); + SearchField type = new SearchField("type", SearchFieldDataType.STRING); + SearchField baseRate = new SearchField("baseRate", SearchFieldDataType.DOUBLE); + SearchField bedOptions = new SearchField("bedOptions", SearchFieldDataType.STRING); + SearchField sleepsCount = new SearchField("sleepsCount", SearchFieldDataType.INT32); + SearchField smokingAllowed = new SearchField("smokingAllowed", SearchFieldDataType.BOOLEAN); + SearchField tags = new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)); return Arrays.asList(description, descriptionFr, type, baseRate, bedOptions, sleepsCount, smokingAllowed, tags); } From ec85021c01eb8660241a87aad30c2ade0451c58e Mon Sep 17 00:00:00 2001 From: Srikanta Date: Fri, 3 Jul 2020 00:20:30 -0700 Subject: [PATCH 2/3] Update codesnippets and readme samples --- sdk/search/azure-search-documents/README.md | 14 +++++++++----- .../com/azure/search/documents/ReadmeSamples.java | 12 ++++++++---- .../documents/SearchJavaDocCodeSnippets.java | 8 ++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/sdk/search/azure-search-documents/README.md b/sdk/search/azure-search-documents/README.md index c8872b406d94..d12e7080c7b7 100644 --- a/sdk/search/azure-search-documents/README.md +++ b/sdk/search/azure-search-documents/README.md @@ -334,7 +334,7 @@ You can use the [`SearchIndexClient`](#Create-a-SearchIndexClient) to create a s defined using convenient `SimpleField`, `SearchableField`, or `ComplexField` classes. Indexes can also define suggesters, lexical analyzers, and more. - + ```Java List searchFieldList = new ArrayList<>(); searchFieldList.add(new SearchField("hotelId", SearchFieldDataType.STRING) @@ -343,33 +343,37 @@ classes. Indexes can also define suggesters, lexical analyzers, and more. .setSortable(true)); searchFieldList.add(new SearchField("hotelName", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setSortable(true)); - searchFieldList.add(new SearchField("description", SearchFieldDataType.STRING) + .setSearchable(true) .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE)); - searchFieldList.add(new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)) + .setSearchable(true) .setKey(true) .setFilterable(true) .setFacetable(true)); - searchFieldList.add(new SearchField("address", SearchFieldDataType.COMPLEX) .setFields(Arrays.asList( - new SearchField("streetAddress", SearchFieldDataType.STRING), + new SearchField("streetAddress", SearchFieldDataType.STRING).setSearchable(true), new SearchField("city", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("stateProvince", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("country", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("postalCode", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true) diff --git a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java index 0c18d364f895..fb69c91bd066 100644 --- a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java +++ b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/ReadmeSamples.java @@ -225,33 +225,37 @@ public void createIndex() { .setSortable(true)); searchFieldList.add(new SearchField("hotelName", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setSortable(true)); - searchFieldList.add(new SearchField("description", SearchFieldDataType.STRING) + .setSearchable(true) .setAnalyzerName(LexicalAnalyzerName.EU_LUCENE)); - searchFieldList.add(new SearchField("tags", SearchFieldDataType.collection(SearchFieldDataType.STRING)) + .setSearchable(true) .setKey(true) .setFilterable(true) .setFacetable(true)); - searchFieldList.add(new SearchField("address", SearchFieldDataType.COMPLEX) .setFields(Arrays.asList( - new SearchField("streetAddress", SearchFieldDataType.STRING), + new SearchField("streetAddress", SearchFieldDataType.STRING).setSearchable(true), new SearchField("city", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("stateProvince", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("country", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true), new SearchField("postalCode", SearchFieldDataType.STRING) + .setSearchable(true) .setFilterable(true) .setFacetable(true) .setSortable(true) diff --git a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java index bbc064b44fcc..4a368b1b36a0 100644 --- a/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java +++ b/sdk/search/azure-search-documents/src/samples/java/com/azure/search/documents/SearchJavaDocCodeSnippets.java @@ -788,7 +788,7 @@ public void createSearchIndex() { // BEGIN: com.azure.search.documents.indexes.SearchIndexClient.createIndex#SearchIndex List searchFields = Arrays.asList( new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), - new SearchField("hotelName", SearchFieldDataType.STRING) + new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); SearchIndex indexFromService = searchIndexClient.createIndex(searchIndex); @@ -804,7 +804,7 @@ public void createSearchIndexWithResponse() { // BEGIN: com.azure.search.documents.indexes.SearchIndexClient.createIndexWithResponse#SearchIndex-Context List searchFields = Arrays.asList( new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), - new SearchField("hotelName", SearchFieldDataType.STRING) + new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); @@ -1198,7 +1198,7 @@ public void createSearchIndexAsync() { // BEGIN: com.azure.search.documents.indexes.SearchIndexAsyncClient.createIndex#SearchIndex List searchFields = Arrays.asList( new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), - new SearchField("hotelName", SearchFieldDataType.STRING) + new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); searchIndexAsyncClient.createIndex(searchIndex) @@ -1215,7 +1215,7 @@ public void createSearchIndexWithResponseAsync() { // BEGIN: com.azure.search.documents.indexes.SearchIndexAsyncClient.createIndexWithResponse#SearchIndex List searchFields = Arrays.asList( new SearchField("hotelId", SearchFieldDataType.STRING).setKey(true), - new SearchField("hotelName", SearchFieldDataType.STRING) + new SearchField("hotelName", SearchFieldDataType.STRING).setSearchable(true) ); SearchIndex searchIndex = new SearchIndex("searchIndex", searchFields); From 13b2de187fd254a19111cf65890341ad63eac03c Mon Sep 17 00:00:00 2001 From: Srikanta Date: Thu, 9 Jul 2020 17:23:47 -0700 Subject: [PATCH 3/3] Remove SearchFieldBase --- .../indexes/models/SearchFieldBase.java | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchFieldBase.java diff --git a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchFieldBase.java b/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchFieldBase.java deleted file mode 100644 index 9c86b3c628e3..000000000000 --- a/sdk/search/azure-search-documents/src/main/java/com/azure/search/documents/indexes/models/SearchFieldBase.java +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.search.documents.indexes.models; - -import com.azure.core.util.CoreUtils; -import com.azure.core.util.logging.ClientLogger; - -import java.util.Objects; - -/** - * Base field type for helper classes to more easily create a {@link SearchIndex}. - */ -public abstract class SearchFieldBase { - private final ClientLogger logger = new ClientLogger(SearchFieldBase.class); - private final String name; - private final SearchFieldDataType dataType; - - /** - * Initializes a new instance of the {@link SearchFieldBase} class. - * @param name The name of the field, which must be unique within the index or parent field. - * @param dataType The data type of the field. - */ - protected SearchFieldBase(String name, SearchFieldDataType dataType) { - if (CoreUtils.isNullOrEmpty(name)) { - throw logger.logExceptionAsError(new IllegalArgumentException("The name of the field cannot be null")); - } - this.dataType = Objects.requireNonNull(dataType, "'dataType' cannot be null."); - this.name = name; - } - - /** - * Get the name of the field. - * - * @return The name of the field. - */ - public String getName() { - return name; - } - - /** - * Get the {@link SearchFieldDataType} of the field. - * - * @return The data type of the field. - */ - public SearchFieldDataType getDataType() { - return dataType; - } -}