| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Models |
| | 6 | | { |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Linq; |
| | 10 | | using Common; |
| | 11 | | using Spatial; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Represents a parameter value to be used in scoring functions (for example, referencePointParameter). |
| | 15 | | /// </summary> |
| | 16 | | public class ScoringParameter |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the ScoringParameter class with the given name and string values. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="name">Name of the scoring parameter.</param> |
| | 22 | | /// <param name="values">Values of the scoring parameter.</param> |
| 22 | 23 | | public ScoringParameter(string name, IEnumerable<string> values) |
| | 24 | | { |
| 22 | 25 | | Throw.IfArgumentNull(name, "name"); |
| 22 | 26 | | Throw.IfArgumentNull(values, "values"); |
| | 27 | |
|
| 22 | 28 | | Name = name; |
| 22 | 29 | | Values = values.ToList(); // Deep copy. |
| 22 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Initializes a new instance of the ScoringParameter class with the given name and GeographyPoint value. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="name">Name of the scoring parameter.</param> |
| | 36 | | /// <param name="value">Value of the scoring parameter.</param> |
| 8 | 37 | | public ScoringParameter(string name, GeographyPoint value) : this(name, ToLonLatStrings(value)) { } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Gets the name of the scoring parameter. |
| | 41 | | /// </summary> |
| 22 | 42 | | public string Name { get; } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets the values of the scoring parameter. |
| | 46 | | /// </summary> |
| 22 | 47 | | public IEnumerable<string> Values{ get; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Returns the scoring parameter in a format that can be used in a Search API request. |
| | 51 | | /// </summary> |
| | 52 | | /// <returns> |
| | 53 | | /// The scoring parameter as a colon-separated name-value pair (for example, mylocation:-122.2,44.8) |
| | 54 | | /// </returns> |
| | 55 | | public override string ToString() => |
| 70 | 56 | | $"{Name}-{Values.Select(v => EscapeValue(v)).ToCommaSeparatedString()}"; |
| | 57 | |
|
| | 58 | | private static string EscapeValue(string value) |
| | 59 | | { |
| 48 | 60 | | value = value?.Replace("'", "''"); |
| 48 | 61 | | return $"'{value}'"; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | private static IEnumerable<string> ToLonLatStrings(GeographyPoint point) |
| | 65 | | { |
| 4 | 66 | | if (point == null) |
| | 67 | | { |
| 0 | 68 | | yield break; |
| | 69 | | } |
| | 70 | |
|
| 4 | 71 | | yield return point.Longitude.ToString(CultureInfo.InvariantCulture); |
| 4 | 72 | | yield return point.Latitude.ToString(CultureInfo.InvariantCulture); |
| 4 | 73 | | } |
| | 74 | | } |
| | 75 | | } |