| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Search.Documents.Indexes.Models; |
| | 6 | |
|
| | 7 | | namespace Azure.Search.Documents.Indexes |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Attributes a simple field using a primitive type or a collection of a primitive type. |
| | 11 | | /// </summary> |
| | 12 | | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] |
| | 13 | | public class SimpleFieldAttribute : Attribute, ISearchFieldAttribute |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets or sets whether the field is the key field. The default is false. |
| | 17 | | /// A <see cref="SearchIndex"/> must have exactly one key field of type <see cref="SearchFieldDataType.String"/> |
| | 18 | | /// </summary> |
| 5971 | 19 | | public bool IsKey { get; set; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Gets or sets whether the field is returned in search results. The default is false. |
| | 23 | | /// A key field where <see cref="IsKey"/> is true must have this property set to false. |
| | 24 | | /// </summary> |
| 7008 | 25 | | public bool IsHidden { get; set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets or sets a value indicating whether the field can be referenced in <c>$filter</c> queries. The default i |
| | 29 | | /// </summary> |
| | 30 | | /// <remarks> |
| | 31 | | /// Filterable differs from searchable in how strings are handled. Fields of type <see cref="SearchFieldDataType |
| | 32 | | /// For example, if you set such a field <c>f</c> to "sunny day", <c>$filter=f eq 'sunny'</c> will find no match |
| | 33 | | /// </remarks> |
| 8451 | 34 | | public bool IsFilterable { get; set; } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets or sets a value indicating whether the field can be retrieved in facet queries. The default is false. |
| | 38 | | /// </summary> |
| | 39 | | /// <remarks> |
| | 40 | | /// Facets are used in presentation of search results that include hit counts by categories. |
| | 41 | | /// For example, in a search for digital cameras, facets might include branch, megapixels, price, etc. |
| | 42 | | /// </remarks> |
| 7661 | 43 | | public bool IsFacetable { get; set; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets or sets a value indicating whether to enable the field can be referenced in <c>$orderby</c> expressions |
| | 47 | | /// </summary> |
| | 48 | | /// <remarks> |
| | 49 | | /// By default Azure Cognitive Search sorts results by score, but in many experiences users may want to sort by |
| | 50 | | /// </remarks> |
| 6885 | 51 | | public bool IsSortable { get; set; } |
| | 52 | |
|
| | 53 | | /// <inheritdoc/> |
| 2475 | 54 | | void ISearchFieldAttribute.SetField(SearchField field) => SetField(field); |
| | 55 | |
|
| | 56 | | private protected void SetField(SearchField field) |
| | 57 | | { |
| 5200 | 58 | | field.IsKey = IsKey; |
| 5200 | 59 | | field.IsHidden = IsHidden; |
| 5200 | 60 | | field.IsFilterable = IsFilterable; |
| 5200 | 61 | | field.IsFacetable = IsFacetable; |
| 5200 | 62 | | field.IsSortable = IsSortable; |
| | 63 | |
|
| | 64 | | // Only set the field if not already set (e.g. both SimpleFieldAttribute and SearchableFieldAttribute on sam |
| 5200 | 65 | | if (!field.IsSearchable.HasValue) |
| | 66 | | { |
| | 67 | | // Use a SearchableFieldAttribute instead, which will override this property. |
| | 68 | | // The service will return Searchable == false for all non-searchable simple types. |
| 769 | 69 | | field.IsSearchable = false; |
| | 70 | | } |
| 5200 | 71 | | } |
| | 72 | | } |
| | 73 | | } |