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