|
| 1 | +using System; |
| 2 | +using System.Runtime.Serialization; |
| 3 | +using Elasticsearch.Net.Utf8Json; |
| 4 | +using Elasticsearch.Net.Utf8Json.Internal; |
| 5 | + |
| 6 | +namespace Nest |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Boosts the relevance score of documents closer to a provided origin date or point. For example, you can use this query to give |
| 10 | + /// more weight to documents closer to a certain date or location. |
| 11 | + /// You can use the distance_feature query to find the nearest neighbors to a location. You can also use the query in a bool |
| 12 | + /// search’s should filter to add boosted relevance scores to the bool query’s scores. |
| 13 | + /// </summary> |
| 14 | + [JsonFormatter(typeof(DistanceFeatureQueryFormatter))] |
| 15 | + [InterfaceDataContract] |
| 16 | + public interface IDistanceFeatureQuery : IFieldNameQuery |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Date or point of origin used to calculate distances. |
| 20 | + // If the field value is a date or date_nanos field, the origin value must be a date. Date Math, such as now-1h, is supported. |
| 21 | + // If the field value is a geo_point field, the origin value must be a geopoint. |
| 22 | + /// </summary> |
| 23 | + [DataMember(Name = "origin")] |
| 24 | + Union<GeoLocation, DateMath> Origin { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Distance from the origin at which relevance scores receive half of the boost value. |
| 28 | + // If the field value is a date or date_nanos field, the pivot value must be a time unit, such as 1h or 10d. |
| 29 | + // If the field value is a geo_point field, the pivot value must be a distance unit, such as 1km or 12m. |
| 30 | + /// </summary> |
| 31 | + [DataMember(Name = "pivot")] |
| 32 | + Union<Distance, Time> Pivot { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + /// <inheritdoc cref="IDistanceFeatureQuery" /> |
| 36 | + public class DistanceFeatureQuery : FieldNameQueryBase, IDistanceFeatureQuery |
| 37 | + { |
| 38 | + protected override bool Conditionless => IsConditionless(this); |
| 39 | + |
| 40 | + internal static bool IsConditionless(IDistanceFeatureQuery q) => q.Field.IsConditionless() || q.Origin == null && q.Pivot == null; |
| 41 | + |
| 42 | + internal override void InternalWrapInContainer(IQueryContainer container) => container.DistanceFeature = this; |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + public Union<GeoLocation, DateMath> Origin { get; set; } |
| 46 | + |
| 47 | + /// <inheritdoc /> |
| 48 | + public Union<Distance, Time> Pivot { get; set; } |
| 49 | + } |
| 50 | + |
| 51 | + public class DistanceFeatureQueryDescriptor<T> |
| 52 | + : FieldNameQueryDescriptorBase<DistanceFeatureQueryDescriptor<T>, IDistanceFeatureQuery, T> |
| 53 | + , IDistanceFeatureQuery where T : class |
| 54 | + { |
| 55 | + Union<GeoLocation, DateMath> IDistanceFeatureQuery.Origin { get; set; } |
| 56 | + |
| 57 | + Union<Distance, Time> IDistanceFeatureQuery.Pivot { get; set; } |
| 58 | + |
| 59 | + protected override bool Conditionless => DistanceFeatureQuery.IsConditionless(this); |
| 60 | + |
| 61 | + /// <inheritdoc cref="IDistanceFeatureQuery.Origin" /> |
| 62 | + public DistanceFeatureQueryDescriptor<T> Origin(DateMath origin) => |
| 63 | + Assign(origin, (a, v) => a.Origin = v); |
| 64 | + |
| 65 | + /// <inheritdoc cref="IDistanceFeatureQuery.Origin" /> |
| 66 | + public DistanceFeatureQueryDescriptor<T> Origin(GeoLocation origin) => |
| 67 | + Assign(origin, (a, v) => a.Origin = v); |
| 68 | + |
| 69 | + /// <inheritdoc cref="IDistanceFeatureQuery.Pivot" /> |
| 70 | + public DistanceFeatureQueryDescriptor<T> Pivot(Time pivot) => |
| 71 | + Assign(pivot, (a, v) => a.Pivot = v); |
| 72 | + |
| 73 | + /// <inheritdoc cref="IDistanceFeatureQuery.Pivot" /> |
| 74 | + public DistanceFeatureQueryDescriptor<T> Pivot(Distance pivot) => |
| 75 | + Assign(pivot, (a, v) => a.Pivot = v); |
| 76 | + } |
| 77 | + |
| 78 | + internal class DistanceFeatureQueryFormatter : IJsonFormatter<IDistanceFeatureQuery> |
| 79 | + { |
| 80 | + private static readonly UnionFormatter<GeoLocation, DateMath> OriginUnionFormatter = new UnionFormatter<GeoLocation, DateMath> (); |
| 81 | + private static readonly UnionFormatter<Distance, Time> PivotUnionFormatter = new UnionFormatter<Distance, Time>(); |
| 82 | + |
| 83 | + public void Serialize(ref JsonWriter writer, IDistanceFeatureQuery value, IJsonFormatterResolver formatterResolver) |
| 84 | + { |
| 85 | + if (value == null) |
| 86 | + { |
| 87 | + writer.WriteNull(); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + writer.WriteBeginObject(); |
| 92 | + |
| 93 | + writer.WritePropertyName("field"); |
| 94 | + var fieldFormatter = formatterResolver.GetFormatter<Field>(); |
| 95 | + fieldFormatter.Serialize(ref writer, value.Field, formatterResolver); |
| 96 | + writer.WriteValueSeparator(); |
| 97 | + |
| 98 | + writer.WritePropertyName("origin"); |
| 99 | + OriginUnionFormatter.Serialize(ref writer, value.Origin, formatterResolver); |
| 100 | + writer.WriteValueSeparator(); |
| 101 | + |
| 102 | + writer.WritePropertyName("pivot"); |
| 103 | + PivotUnionFormatter.Serialize(ref writer, value.Pivot, formatterResolver); |
| 104 | + |
| 105 | + writer.WriteValueSeparator(); |
| 106 | + |
| 107 | + if (value.Boost.HasValue) |
| 108 | + { |
| 109 | + writer.WritePropertyName("boost"); |
| 110 | + writer.WriteDouble(value.Boost.Value); |
| 111 | + } |
| 112 | + |
| 113 | + writer.WriteEndObject(); |
| 114 | + } |
| 115 | + |
| 116 | + private static readonly AutomataDictionary Fields = new AutomataDictionary |
| 117 | + { |
| 118 | + { "field", 0 }, |
| 119 | + { "origin", 1 }, |
| 120 | + { "pivot", 2 }, |
| 121 | + { "boost", 3 } |
| 122 | + }; |
| 123 | + |
| 124 | + public IDistanceFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) |
| 125 | + { |
| 126 | + if (reader.ReadIsNull()) |
| 127 | + return null; |
| 128 | + |
| 129 | + var query = new DistanceFeatureQuery(); |
| 130 | + var count = 0; |
| 131 | + while (reader.ReadIsInObject(ref count)) |
| 132 | + { |
| 133 | + if (Fields.TryGetValue(reader.ReadPropertyNameSegmentRaw(), out var value)) |
| 134 | + { |
| 135 | + switch (value) |
| 136 | + { |
| 137 | + case 0: |
| 138 | + query.Field = formatterResolver.GetFormatter<Field>().Deserialize(ref reader, formatterResolver); |
| 139 | + break; |
| 140 | + case 1: |
| 141 | + query.Origin = OriginUnionFormatter.Deserialize(ref reader, formatterResolver); |
| 142 | + break; |
| 143 | + case 2: |
| 144 | + query.Pivot = PivotUnionFormatter.Deserialize(ref reader, formatterResolver); |
| 145 | + break; |
| 146 | + case 3: |
| 147 | + query.Boost = reader.ReadDouble(); |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + else |
| 152 | + reader.ReadNextBlock(); |
| 153 | + } |
| 154 | + |
| 155 | + return query; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments