-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement distance_feature query (#3983)
Implement distance_feature query (cherry picked from commit e58db5b)
- Loading branch information
Showing
9 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/Nest/QueryDsl/Specialized/DistanceFeature/DistanceFeatureQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using Elasticsearch.Net.Utf8Json; | ||
using Elasticsearch.Net.Utf8Json.Internal; | ||
|
||
namespace Nest | ||
{ | ||
/// <summary> | ||
/// Boosts the relevance score of documents closer to a provided origin date or point. For example, you can use this query to give | ||
/// more weight to documents closer to a certain date or location. | ||
/// You can use the distance_feature query to find the nearest neighbors to a location. You can also use the query in a bool | ||
/// search’s should filter to add boosted relevance scores to the bool query’s scores. | ||
/// </summary> | ||
[JsonFormatter(typeof(DistanceFeatureQueryFormatter))] | ||
[InterfaceDataContract] | ||
public interface IDistanceFeatureQuery : IFieldNameQuery | ||
{ | ||
/// <summary> | ||
/// Date or point of origin used to calculate distances. | ||
// 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. | ||
// If the field value is a geo_point field, the origin value must be a geopoint. | ||
/// </summary> | ||
[DataMember(Name = "origin")] | ||
Union<GeoLocation, DateMath> Origin { get; set; } | ||
|
||
/// <summary> | ||
/// Distance from the origin at which relevance scores receive half of the boost value. | ||
// If the field value is a date or date_nanos field, the pivot value must be a time unit, such as 1h or 10d. | ||
// If the field value is a geo_point field, the pivot value must be a distance unit, such as 1km or 12m. | ||
/// </summary> | ||
[DataMember(Name = "pivot")] | ||
Union<Distance, Time> Pivot { get; set; } | ||
} | ||
|
||
/// <inheritdoc cref="IDistanceFeatureQuery" /> | ||
public class DistanceFeatureQuery : FieldNameQueryBase, IDistanceFeatureQuery | ||
{ | ||
protected override bool Conditionless => IsConditionless(this); | ||
|
||
internal static bool IsConditionless(IDistanceFeatureQuery q) => q.Field.IsConditionless() || q.Origin == null && q.Pivot == null; | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer container) => container.DistanceFeature = this; | ||
|
||
/// <inheritdoc /> | ||
public Union<GeoLocation, DateMath> Origin { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public Union<Distance, Time> Pivot { get; set; } | ||
} | ||
|
||
public class DistanceFeatureQueryDescriptor<T> | ||
: FieldNameQueryDescriptorBase<DistanceFeatureQueryDescriptor<T>, IDistanceFeatureQuery, T> | ||
, IDistanceFeatureQuery where T : class | ||
{ | ||
Union<GeoLocation, DateMath> IDistanceFeatureQuery.Origin { get; set; } | ||
|
||
Union<Distance, Time> IDistanceFeatureQuery.Pivot { get; set; } | ||
|
||
protected override bool Conditionless => DistanceFeatureQuery.IsConditionless(this); | ||
|
||
/// <inheritdoc cref="IDistanceFeatureQuery.Origin" /> | ||
public DistanceFeatureQueryDescriptor<T> Origin(DateMath origin) => | ||
Assign(origin, (a, v) => a.Origin = v); | ||
|
||
/// <inheritdoc cref="IDistanceFeatureQuery.Origin" /> | ||
public DistanceFeatureQueryDescriptor<T> Origin(GeoLocation origin) => | ||
Assign(origin, (a, v) => a.Origin = v); | ||
|
||
/// <inheritdoc cref="IDistanceFeatureQuery.Pivot" /> | ||
public DistanceFeatureQueryDescriptor<T> Pivot(Time pivot) => | ||
Assign(pivot, (a, v) => a.Pivot = v); | ||
|
||
/// <inheritdoc cref="IDistanceFeatureQuery.Pivot" /> | ||
public DistanceFeatureQueryDescriptor<T> Pivot(Distance pivot) => | ||
Assign(pivot, (a, v) => a.Pivot = v); | ||
} | ||
|
||
internal class DistanceFeatureQueryFormatter : IJsonFormatter<IDistanceFeatureQuery> | ||
{ | ||
private static readonly UnionFormatter<GeoLocation, DateMath> OriginUnionFormatter = new UnionFormatter<GeoLocation, DateMath> (); | ||
private static readonly UnionFormatter<Distance, Time> PivotUnionFormatter = new UnionFormatter<Distance, Time>(); | ||
|
||
public void Serialize(ref JsonWriter writer, IDistanceFeatureQuery value, IJsonFormatterResolver formatterResolver) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNull(); | ||
return; | ||
} | ||
|
||
writer.WriteBeginObject(); | ||
|
||
writer.WritePropertyName("field"); | ||
var fieldFormatter = formatterResolver.GetFormatter<Field>(); | ||
fieldFormatter.Serialize(ref writer, value.Field, formatterResolver); | ||
writer.WriteValueSeparator(); | ||
|
||
writer.WritePropertyName("origin"); | ||
OriginUnionFormatter.Serialize(ref writer, value.Origin, formatterResolver); | ||
writer.WriteValueSeparator(); | ||
|
||
writer.WritePropertyName("pivot"); | ||
PivotUnionFormatter.Serialize(ref writer, value.Pivot, formatterResolver); | ||
|
||
writer.WriteValueSeparator(); | ||
|
||
if (value.Boost.HasValue) | ||
{ | ||
writer.WritePropertyName("boost"); | ||
writer.WriteDouble(value.Boost.Value); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
private static readonly AutomataDictionary Fields = new AutomataDictionary | ||
{ | ||
{ "field", 0 }, | ||
{ "origin", 1 }, | ||
{ "pivot", 2 }, | ||
{ "boost", 3 } | ||
}; | ||
|
||
public IDistanceFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) | ||
{ | ||
if (reader.ReadIsNull()) | ||
return null; | ||
|
||
var query = new DistanceFeatureQuery(); | ||
var count = 0; | ||
while (reader.ReadIsInObject(ref count)) | ||
{ | ||
if (Fields.TryGetValue(reader.ReadPropertyNameSegmentRaw(), out var value)) | ||
{ | ||
switch (value) | ||
{ | ||
case 0: | ||
query.Field = formatterResolver.GetFormatter<Field>().Deserialize(ref reader, formatterResolver); | ||
break; | ||
case 1: | ||
query.Origin = OriginUnionFormatter.Deserialize(ref reader, formatterResolver); | ||
break; | ||
case 2: | ||
query.Pivot = PivotUnionFormatter.Deserialize(ref reader, formatterResolver); | ||
break; | ||
case 3: | ||
query.Boost = reader.ReadDouble(); | ||
break; | ||
} | ||
} | ||
else | ||
reader.ReadNextBlock(); | ||
} | ||
|
||
return query; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Tests/Tests/QueryDsl/Specialized/DistanceFeature/DistanceFeatureTimeQueryUsageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Nest; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
namespace Tests.QueryDsl.Specialized.DistanceFeature | ||
{ | ||
/** | ||
* Boosts the relevance score of documents closer to a provided origin date or point. For example, you can use this query to give | ||
* more weight to documents closer to a certain date. | ||
*/ | ||
[SkipVersion("<7.2.0", "Implemented in version 7.2.0")] | ||
public class DistanceFeatureTimeQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public DistanceFeatureTimeQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IDistanceFeatureQuery>(a => a.DistanceFeature) | ||
{ | ||
q => | ||
{ | ||
q.Field = null; | ||
q.Origin = null; | ||
q.Pivot = null; | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new DistanceFeatureQuery | ||
{ | ||
Boost = 1.1, | ||
Field = Infer.Field<Project>(f => f.StartedOn), | ||
Origin = DateMath.FromString("now"), | ||
Pivot = new Time("7d") | ||
}; | ||
|
||
protected override object QueryJson => | ||
new { distance_feature = new { boost = 1.1, field = "startedOn", origin = "now", pivot = "7d" } }; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.DistanceFeature(rf => rf | ||
.Boost(1.1) | ||
.Field(f => f.StartedOn) | ||
.Origin(DateMath.FromString("now")) | ||
.Pivot(new Time("7d")) | ||
); | ||
} | ||
|
||
/** | ||
* You can use the distance_feature query to find the nearest neighbors to a location. You can also use the query in a bool | ||
* search’s should filter to add boosted relevance scores to the bool query’s scores. | ||
*/ | ||
[SkipVersion("<7.2.0", "Implemented in version 7.2.0")] | ||
public class DistanceFeatureDistanceQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public DistanceFeatureDistanceQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IDistanceFeatureQuery>(a => a.DistanceFeature) | ||
{ | ||
q => | ||
{ | ||
q.Field = null; | ||
q.Origin = null; | ||
q.Pivot = null; | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new DistanceFeatureQuery() | ||
{ | ||
Boost = 1.1, | ||
Field = Infer.Field<Project>(f => f.StartedOn), | ||
Origin = new GeoLocation(70, -70), | ||
Pivot = new Distance(100, DistanceUnit.Miles) | ||
}; | ||
|
||
protected override object QueryJson => | ||
new { distance_feature = new { boost = 1.1, field = "startedOn", origin = new { lat = 70.0, lon = -70.0 }, pivot = "100mi" } }; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.DistanceFeature(rf => rf | ||
.Boost(1.1) | ||
.Field(f => f.StartedOn) | ||
.Origin(new GeoLocation(70, -70)) | ||
.Pivot(new Distance(100, DistanceUnit.Miles)) | ||
); | ||
} | ||
} |