在 ES 的 GEO 相关查询中,你会使用到“距离”这个概念,Nest 提供了 Distance 类来帮助你创建“距离”。
通过 Distance 构造函数
1 2
| var unitComposed = new Distance(25); var unitComposedWithUnits = new Distance(25, Nest.DistanceUnit.Meters);
|
也可以使用指定距离单位的字符串,如下:
1 2 3
| Expect("25m") .WhenSerializing(unitComposed) .WhenSerializing(unitComposedWithUnits);
|
隐式转换
可以将距离单位字符串分配给 Distance,从而隐式转换为新的 Distance 实例。如果未指定DistanceUnit,则默认距离单位为“米”
以下两者等效:
1 2 3 4 5 6
| Distance distanceString = "25"; Distance distanceStringWithUnits = "25m";
Expect("25m") .WhenSerializing(distanceString) .WhenSerializing(distanceStringWithUnits);
|
支持的单位
Metric 公制单位
mm(毫米)
1
| Expect("2mm").WhenSerializing(new Distance(2, Nest.DistanceUnit.Millimeters));
|
cm(厘米)
1
| Expect("123.456cm").WhenSerializing(new Distance(123.456, Nest.DistanceUnit.Centimeters));
|
m(米)
1
| Expect("400m").WhenSerializing(new Distance(400, Nest.DistanceUnit.Meters));
|
km(公里)
1
| Expect("0.1km").WhenSerializing(new Distance(0.1, Nest.DistanceUnit.Kilometers));
|
Imperial 英制单位
in(Inches)
1
| Expect("43.23in").WhenSerializing(new Distance(43.23, Nest.DistanceUnit.Inch));
|
ft(Feet)
1
| Expect("3.33ft").WhenSerializing(new Distance(3.33, Nest.DistanceUnit.Feet));
|
yd(Yards)
1
| Expect("9yd").WhenSerializing(new Distance(9, Nest.DistanceUnit.Yards));
|
mi(Miles)
1
| Expect("0.62mi").WhenSerializing(new Distance(0.62, Nest.DistanceUnit.Miles));
|
nmi or NM (Nautical Miles)
1
| Expect("45.5nmi").WhenSerializing(new Distance(45.5, Nest.DistanceUnit.NauticalMiles));
|