Beispiel #1
0
        private static ISort GetSortingField(SortingField field)
        {
            ISort result;

            var geoSorting = field as GeoDistanceSortingField;

            if (geoSorting != null)
            {
                result = new GeoDistanceSort
                {
                    Field  = ElasticSearchHelper.ToElasticFieldName(field.FieldName),
                    Points = new[] { geoSorting.Location.ToGeoLocation() }
                };
            }
            else
            {
                result = new SortField
                {
                    Field        = ElasticSearchHelper.ToElasticFieldName(field.FieldName),
                    Order        = field.IsDescending ? SortOrder.Descending : SortOrder.Ascending,
                    Missing      = "_last",
                    UnmappedType = FieldType.Long,
                };
            }

            return(result);
        }
 public void PASS_Serialize()
 {
     GeoDistanceSort sort = new GeoDistanceSort("field", new CoordinatePoint(1.1, 2.2), DistanceUnitEnum.Kilometer);
     string json = JsonConvert.SerializeObject(sort);
     Assert.IsNotNull(json);
     string expectedJson = "{\"_geo_distance\":{\"field\":{\"lat\":1.1,\"lon\":2.2},\"unit\":\"km\"}}";
     Assert.AreEqual(expectedJson, json);
 }
 public void PASS_CreateSort()
 {
     GeoDistanceSort sort = new GeoDistanceSort("field", new CoordinatePoint(1.1, 2.2), DistanceUnitEnum.Kilometer);
     Assert.IsNotNull(sort);
     Assert.AreEqual("field", sort.Field);
     Assert.AreEqual(1.1, sort.CenterPoint.Latitude);
     Assert.AreEqual(2.2, sort.CenterPoint.Longitude);
     Assert.AreEqual(DistanceUnitEnum.Kilometer, sort.Unit);
 }
 public void FAIL_CreateSort_Unit()
 {
     try
     {
         GeoDistanceSort sort = new GeoDistanceSort("field", new CoordinatePoint(1.1, 2.2), null);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("unit", ex.ParamName);
     }
 }
 public void FAIL_CreateSort_CenterPoint()
 {
     try
     {
         GeoDistanceSort sort = new GeoDistanceSort("field", null, DistanceUnitEnum.Kilometer);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("centerPoint", ex.ParamName);
     }
 }
 public void FAIL_CreateSort_Field()
 {
     try
     {
         GeoDistanceSort sort = new GeoDistanceSort(null, new CoordinatePoint(1.1, 2.2), DistanceUnitEnum.Kilometer);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("field", ex.ParamName);
     }
 }
Beispiel #7
0
    public static void WriteGeoDistanceSort(Utf8JsonWriter writer, GeoDistanceSort geoDistanceSort, JsonSerializerOptions options, IElasticsearchClientSettings settings)
    {
        writer.WriteStartObject();
        writer.WritePropertyName("_geo_distance");
        writer.WriteStartObject();

        if (geoDistanceSort.Order.HasValue)
        {
            writer.WritePropertyName("order");
            JsonSerializer.Serialize(writer, geoDistanceSort.Order.Value, options);
        }

        if (geoDistanceSort.DistanceType.HasValue)
        {
            writer.WritePropertyName("distance_type");
            JsonSerializer.Serialize(writer, geoDistanceSort.DistanceType.Value, options);
        }

        if (geoDistanceSort.Mode.HasValue)
        {
            writer.WritePropertyName("mode");
            JsonSerializer.Serialize(writer, geoDistanceSort.Mode.Value, options);
        }

        if (geoDistanceSort.Unit.HasValue)
        {
            writer.WritePropertyName("unit");
            JsonSerializer.Serialize(writer, geoDistanceSort.Unit.Value, options);
        }

        if (geoDistanceSort.IgnoreUnmapped.HasValue)
        {
            writer.WritePropertyName("ignore_unmapped");
            JsonSerializer.Serialize(writer, geoDistanceSort.IgnoreUnmapped.Value, options);
        }

        if (geoDistanceSort.Field is not null && geoDistanceSort.GeoPoints is not null)
        {
            writer.WritePropertyName(settings.Inferrer.Field(geoDistanceSort.Field));
            JsonSerializer.Serialize(writer, geoDistanceSort.GeoPoints, options);
        }

        writer.WriteEndObject();
        writer.WriteEndObject();
    }
		private void LoadGeoDistanceSortLocation(GeoDistanceSort sort, JObject j)
		{
			var field = j.Properties().Where(p => !GeoDistanceSort.Params.Contains(p.Name)).FirstOrDefault();

			if (field != null)
			{
				sort.Field = field.Name;

				try
				{
					sort.PinLocation = field.Value.Value<string>();
				}
				catch { }

				try
				{
					sort.Points = field.Value.Value<IEnumerable<string>>();
				}
				catch { }
			}
		}
        public static GeoDistanceSort BuildGeoDistanceSort(DataModel.Search.Sort.GeoDistanceSort geoDistanceSort)
        {
            GeoDistanceSort.Builder builder = GeoDistanceSort.CreateBuilder();
            builder.SetFieldName(geoDistanceSort.FieldName);
            if (geoDistanceSort.Points != null)
            {
                builder.AddRangePoints(geoDistanceSort.Points);
            }

            builder.SetOrder(BuildSortOrder(geoDistanceSort.Order));


            builder.SetMode(BuildSortMode(geoDistanceSort.Mode));


            builder.SetDistanceType(BuildGeoDistanceType(geoDistanceSort.DistanceType));

            if (geoDistanceSort.NestedFilter != null)
            {
                builder.SetNestedFilter(BuildNestedFilter(geoDistanceSort.NestedFilter));
            }
            return(builder.Build());
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> fieldDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if (fieldDict.ContainsKey(SortTypeEnum.GeoDistance.ToString()))
                fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.First().Value.ToString());

            KeyValuePair<string, object> fieldKvp = fieldDict.FirstOrDefault(x => !_KnownFields.Contains(x.Key, StringComparer.OrdinalIgnoreCase));

            if (string.IsNullOrWhiteSpace(fieldKvp.Key))
                throw new RequiredPropertyMissingException("GeoPointField");

            string fieldName = fieldKvp.Key;
            CoordinatePoint centerPoint = CoordinatePointSerializer.DeserializeCoordinatePoint(fieldKvp.Value.ToString());
            DistanceUnitEnum unit = DistanceUnitEnum.Kilometer;
            unit = DistanceUnitEnum.Find(fieldDict.GetString(_UNIT));
            GeoDistanceSort sort = new GeoDistanceSort(fieldName, centerPoint, unit);

            sort.Reverse = fieldDict.GetBool(SortClauseSerializer._REVERSE, SortClauseSerializer._REVERSE_DEFAULT);
            if (fieldDict.ContainsKey(_MODE))
                sort.SortMode = SortModeEnum.Find(fieldDict.GetString(_MODE));
            sort.SortOrder = SortOrderEnum.Find(fieldDict.GetString(SortClauseSerializer._ORDER, SortClauseSerializer._ORDER_DEFAULT.ToString()));

            return sort;
        }