-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement distance_feature query #3983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6849a40
Implement distance_feature query
codebrain f0e67ff
Address PR comments
codebrain d9a27f4
Change docs reference
codebrain c1e9f9e
Update src/Nest/QueryDsl/Specialized/DistanceFeature/DistanceFeatureQ…
codebrain 329e9fd
Update src/Nest/QueryDsl/Specialized/DistanceFeature/DistanceFeatureQ…
codebrain b229475
Update src/Nest/QueryDsl/Specialized/DistanceFeature/DistanceFeatureQ…
codebrain 1816cbf
Add test for distance property type
codebrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
codebrain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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> | ||
codebrain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)) | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.