| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Search.Documents.Indexes.Models |
| | 10 | | { |
| | 11 | | public partial class SearchIndex |
| | 12 | | { |
| | 13 | | private IList<SearchField> _fields; |
| | 14 | |
|
| | 15 | | [CodeGenMember("etag")] |
| | 16 | | private string _etag; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="SearchIndex"/> class. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="name">The name of the index.</param> |
| | 22 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 23 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception> |
| 9 | 24 | | public SearchIndex(string name) |
| | 25 | | { |
| 9 | 26 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| | 27 | |
|
| 9 | 28 | | Name = name; |
| | 29 | |
|
| 9 | 30 | | Analyzers = new ChangeTrackingList<LexicalAnalyzer>(); |
| 9 | 31 | | CharFilters = new ChangeTrackingList<CharFilter>(); |
| 9 | 32 | | Fields = new List<SearchField>(); |
| 9 | 33 | | ScoringProfiles = new ChangeTrackingList<ScoringProfile>(); |
| 9 | 34 | | Suggesters = new ChangeTrackingList<SearchSuggester>(); |
| 9 | 35 | | TokenFilters = new ChangeTrackingList<TokenFilter>(); |
| 9 | 36 | | Tokenizers = new ChangeTrackingList<LexicalTokenizer>(); |
| 9 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a new instance of the <see cref="SearchIndex"/> class. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="name">The name of the index.</param> |
| | 43 | | /// <param name="fields">Fields to add to the index.</param> |
| | 44 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 45 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="fields"/> is null.</excep |
| 1 | 46 | | public SearchIndex(string name, IEnumerable<SearchField> fields) |
| | 47 | | { |
| 1 | 48 | | Argument.AssertNotNullOrEmpty(name, nameof(name)); |
| 1 | 49 | | Argument.AssertNotNull(fields, nameof(fields)); |
| | 50 | |
|
| 1 | 51 | | Name = name; |
| | 52 | |
|
| 1 | 53 | | Analyzers = new ChangeTrackingList<LexicalAnalyzer>(); |
| 1 | 54 | | CharFilters = new ChangeTrackingList<CharFilter>(); |
| 1 | 55 | | Fields = fields.ToList(); |
| 1 | 56 | | ScoringProfiles = new ChangeTrackingList<ScoringProfile>(); |
| 1 | 57 | | Suggesters = new ChangeTrackingList<SearchSuggester>(); |
| 1 | 58 | | TokenFilters = new ChangeTrackingList<TokenFilter>(); |
| 1 | 59 | | Tokenizers = new ChangeTrackingList<LexicalTokenizer>(); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Gets the name of the index. |
| | 64 | | /// </summary> |
| | 65 | | [CodeGenMember("name")] |
| 30 | 66 | | public string Name { get; } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Gets the analyzers for the index. |
| | 70 | | /// </summary> |
| 22 | 71 | | public IList<LexicalAnalyzer> Analyzers { get; } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Gets the character filters for the index. |
| | 75 | | /// </summary> |
| 12 | 76 | | public IList<CharFilter> CharFilters { get; } |
| | 77 | |
|
| | 78 | | #if EXPERIMENTAL_FIELDBUILDER |
| | 79 | | /// <summary> |
| | 80 | | /// Gets or sets the fields in the index. |
| | 81 | | /// Use <see cref="FieldBuilder"/> to define fields based on a model class, |
| | 82 | | /// or <see cref="SimpleField"/>, <see cref="SearchableField"/>, and <see cref="ComplexField"/> to manually defi |
| | 83 | | /// Index fields have many constraints that are not validated with <see cref="SearchField"/> until the index is |
| | 84 | | /// </summary> |
| | 85 | | /// <example> |
| | 86 | | /// You can create fields from a model class using <see cref="FieldBuilder"/>: |
| | 87 | | /// <code snippet="Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex"> |
| | 88 | | /// SearchIndex index = new SearchIndex("hotels") |
| | 89 | | /// { |
| | 90 | | /// Fields = new FieldBuilder().Build(typeof(Hotel)), |
| | 91 | | /// Suggesters = |
| | 92 | | /// { |
| | 93 | | /// // Suggest query terms from the hotelName field. |
| | 94 | | /// new SearchSuggester("sg", "hotelName") |
| | 95 | | /// } |
| | 96 | | /// }; |
| | 97 | | /// </code> |
| | 98 | | /// For this reason, <see cref="Fields"/> is settable. In scenarios when the model is not known or cannot be mod |
| | 99 | | /// also create fields manually using helper classes: |
| | 100 | | /// <code snippet="Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex"> |
| | 101 | | /// SearchIndex index = new SearchIndex("hotels") |
| | 102 | | /// { |
| | 103 | | /// Fields = |
| | 104 | | /// { |
| | 105 | | /// new SimpleField("hotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable = true |
| | 106 | | /// new SearchableField("hotelName") { IsFilterable = true, IsSortable = true }, |
| | 107 | | /// new SearchableField("description") { AnalyzerName = LexicalAnalyzerName.EnLucene }, |
| | 108 | | /// new SearchableField("tags", collection: true) { IsFilterable = true, IsFacetable = true }, |
| | 109 | | /// new ComplexField("address") |
| | 110 | | /// { |
| | 111 | | /// Fields = |
| | 112 | | /// { |
| | 113 | | /// new SearchableField("streetAddress"), |
| | 114 | | /// new SearchableField("city") { IsFilterable = true, IsSortable = true, IsFacetable |
| | 115 | | /// new SearchableField("stateProvince") { IsFilterable = true, IsSortable = true, IsF |
| | 116 | | /// new SearchableField("country") { IsFilterable = true, IsSortable = true, IsFacetab |
| | 117 | | /// new SearchableField("postalCode") { IsFilterable = true, IsSortable = true, IsFace |
| | 118 | | /// } |
| | 119 | | /// } |
| | 120 | | /// }, |
| | 121 | | /// Suggesters = |
| | 122 | | /// { |
| | 123 | | /// // Suggest query terms from the hotelName field. |
| | 124 | | /// new SearchSuggester("sg", "hotelName") |
| | 125 | | /// } |
| | 126 | | /// }; |
| | 127 | | /// </code> |
| | 128 | | /// </example> |
| | 129 | | #else |
| | 130 | | /// <summary> |
| | 131 | | /// Gets or sets the fields in the index. |
| | 132 | | /// Use <see cref="SimpleField"/>, <see cref="SearchableField"/>, and <see cref="ComplexField"/> to manually def |
| | 133 | | /// Index fields have many constraints that are not validated with <see cref="SearchField"/> until the index is |
| | 134 | | /// </summary> |
| | 135 | | /// <example> |
| | 136 | | /// You can create fields manually using helper classes: |
| | 137 | | /// <code snippet="Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex"> |
| | 138 | | /// SearchIndex index = new SearchIndex("hotels") |
| | 139 | | /// { |
| | 140 | | /// Fields = |
| | 141 | | /// { |
| | 142 | | /// new SimpleField("hotelId", SearchFieldDataType.String) { IsKey = true, IsFilterable = true |
| | 143 | | /// new SearchableField("hotelName") { IsFilterable = true, IsSortable = true }, |
| | 144 | | /// new SearchableField("description") { AnalyzerName = LexicalAnalyzerName.EnLucene }, |
| | 145 | | /// new SearchableField("tags", collection: true) { IsFilterable = true, IsFacetable = true }, |
| | 146 | | /// new ComplexField("address") |
| | 147 | | /// { |
| | 148 | | /// Fields = |
| | 149 | | /// { |
| | 150 | | /// new SearchableField("streetAddress"), |
| | 151 | | /// new SearchableField("city") { IsFilterable = true, IsSortable = true, IsFacetable |
| | 152 | | /// new SearchableField("stateProvince") { IsFilterable = true, IsSortable = true, IsF |
| | 153 | | /// new SearchableField("country") { IsFilterable = true, IsSortable = true, IsFacetab |
| | 154 | | /// new SearchableField("postalCode") { IsFilterable = true, IsSortable = true, IsFace |
| | 155 | | /// } |
| | 156 | | /// } |
| | 157 | | /// }, |
| | 158 | | /// Suggesters = |
| | 159 | | /// { |
| | 160 | | /// // Suggest query terms from the hotelName field. |
| | 161 | | /// new SearchSuggester("sg", "hotelName") |
| | 162 | | /// } |
| | 163 | | /// }; |
| | 164 | | /// </code> |
| | 165 | | /// </example> |
| | 166 | | #endif |
| | 167 | | public IList<SearchField> Fields |
| | 168 | | { |
| 87 | 169 | | get => _fields; |
| | 170 | | set |
| | 171 | | { |
| 33 | 172 | | _fields = value ?? throw new ArgumentNullException(nameof(value), $"{nameof(Fields)} cannot be null. To |
| 32 | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Gets the scoring profiles for the index. |
| | 178 | | /// </summary> |
| 36 | 179 | | public IList<ScoringProfile> ScoringProfiles { get; } |
| | 180 | |
|
| | 181 | | /// <summary> |
| | 182 | | /// Gets the suggesters for the index. |
| | 183 | | /// </summary> |
| 42 | 184 | | public IList<SearchSuggester> Suggesters { get; } |
| | 185 | |
|
| | 186 | | /// <summary> |
| | 187 | | /// Gets the token filters for the index. |
| | 188 | | /// </summary> |
| 12 | 189 | | public IList<TokenFilter> TokenFilters { get; } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Gets the tokenizers for the index. |
| | 193 | | /// </summary> |
| 12 | 194 | | public IList<LexicalTokenizer> Tokenizers { get; } |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// The <see cref="Azure.ETag"/> of the <see cref="SearchIndex"/>. |
| | 198 | | /// </summary> |
| | 199 | | public ETag? ETag |
| | 200 | | { |
| 5 | 201 | | get => _etag is null ? (ETag?)null : new ETag(_etag); |
| 0 | 202 | | set => _etag = value?.ToString(); |
| | 203 | | } |
| | 204 | | } |
| | 205 | | } |