Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Nest/DSL/Filter/FilterContainer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using Nest.DSL.Visitor;
using Newtonsoft.Json;

Expand Down Expand Up @@ -48,6 +46,8 @@ public bool IsConditionless

IGeoDistanceRangeFilter IFilterContainer.GeoDistanceRange { get; set; }

IGeoHashCellFilter IFilterContainer.GeoHashCell { get; set; }

IGeoPolygonFilter IFilterContainer.GeoPolygon { get; set; }

IGeoShapeBaseFilter IFilterContainer.GeoShape { get; set; }
Expand Down
27 changes: 27 additions & 0 deletions src/Nest/DSL/Filter/FilterDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,33 @@ private FilterContainer _GeoDistance(PropertyPathMarker field, Action<GeoDistanc
return this.New(filter, f => f.GeoDistance = filter);
}

/// <summary>
/// By defining a geohash cell, only geopoints within this cell will match this filter
/// </summary>
public FilterContainer GeoHashCell(string field, Action<GeoHashCellFilterDescriptor> filterDescriptor)
{
return _GeoHashCell(field, filterDescriptor);
}

/// <summary>
/// By defining a geohash cell, only geopoints within this cell will match this filter
/// </summary>
public FilterContainer GeoHashCell(Expression<Func<T, object>> fieldDescriptor, Action<GeoHashCellFilterDescriptor> filterDescriptor)
{
return _GeoHashCell(fieldDescriptor, filterDescriptor);
}

private FilterContainer _GeoHashCell(PropertyPathMarker field, Action<GeoHashCellFilterDescriptor> filterDescriptor)
{
var filter = new GeoHashCellFilterDescriptor();
if (filterDescriptor != null)
filterDescriptor(filter);

IGeoHashCellFilter ff = filter;
ff.Field = field;
return New(filter, f => f.GeoHashCell = filter);
}

/// <summary>
/// Filters documents that exists within a range from a specific point:
/// </summary>
Expand Down
89 changes: 89 additions & 0 deletions src/Nest/DSL/Filter/GeoHashCellFilterDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface IGeoHashCellFilter : IFilter
{
PropertyPathMarker Field { get; set; }
string Location { get; set; }

[JsonProperty("distance")]
object Precision { get; set; }

[JsonProperty("unit")]
[JsonConverter(typeof(StringEnumConverter))]
GeoUnit? Unit { get; set; }

[JsonProperty("neighbors")]
bool Neighbors { get; set; }
}

public class GeoHashCellFilter : PlainFilter, IGeoHashCellFilter
{
protected internal override void WrapInContainer(IFilterContainer container)
{
container.GeoHashCell = this;
}

public PropertyPathMarker Field { get; set; }
public string Location { get; set; }
public object Precision { get; set; }
public GeoUnit? Unit { get; set; }
public bool Neighbors { get; set; }
}

public class GeoHashCellFilterDescriptor : FilterBase, IGeoHashCellFilter
{
PropertyPathMarker IGeoHashCellFilter.Field { get; set; }
string IGeoHashCellFilter.Location { get; set; }
object IGeoHashCellFilter.Precision { get; set; }
GeoUnit? IGeoHashCellFilter.Unit { get; set; }
bool IGeoHashCellFilter.Neighbors { get; set; }

bool IFilter.IsConditionless
{
get
{
var isConditionless = ((IGeoHashCellFilter) this).Location.IsNullOrEmpty();

return isConditionless;
}
}

public GeoHashCellFilterDescriptor Location(double lat, double lon)
{
var c = CultureInfo.InvariantCulture;
((IGeoHashCellFilter)this).Location = "{{ lat: {0}, lon: {1} }}".F(lat.ToString(c), lon.ToString(c));
return this;
}

public GeoHashCellFilterDescriptor Location(string geoHash)
{
((IGeoHashCellFilter)this).Location = geoHash;
return this;
}

public GeoHashCellFilterDescriptor Precision(int precision)
{
((IGeoHashCellFilter)this).Precision = precision;
return this;
}

public GeoHashCellFilterDescriptor Distance(double distance, GeoUnit unit)
{
((IGeoHashCellFilter)this).Precision = distance;
((IGeoHashCellFilter)this).Unit = unit;
return this;
}

public GeoHashCellFilterDescriptor Neighbors(bool neighbors)
{
((IGeoHashCellFilter)this).Neighbors = neighbors;
return this;
}

}
}
4 changes: 4 additions & 0 deletions src/Nest/DSL/Filter/IFilterContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public interface IFilterContainer
[JsonConverter(typeof(GeoDistanceFilterConverter))]
IGeoDistanceFilter GeoDistance { get; set; }

[JsonProperty(PropertyName = "geohash_cell")]
[JsonConverter(typeof(GeoHashCellFilterConverter))]
IGeoHashCellFilter GeoHashCell { get; set; }

[JsonProperty(PropertyName = "geo_distance_range")]
[JsonConverter(typeof(GeoDistanceRangeFilterConverter))]
IGeoDistanceRangeFilter GeoDistanceRange { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Nest/DSL/Visitor/DslPrettyPrintVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public virtual void Visit(IGeoDistanceFilter filter)
Write("geo_distance");
}

public virtual void Visit(IGeoHashCellFilter filter)
{
Write("geohash_cell");
}

public virtual void Visit(IGeoBoundingBoxFilter filter)
{
Write("geo_bounding_box");
Expand Down
10 changes: 5 additions & 5 deletions src/Nest/DSL/Visitor/QueryVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nest.DSL.Visitor
namespace Nest.DSL.Visitor
{
public interface IQueryVisitor
{
Expand Down Expand Up @@ -365,6 +361,10 @@ public virtual void Visit(IGeoDistanceFilter customFiltersScore)
{
}

public virtual void Visit(IGeoHashCellFilter filter)
{
}

public virtual void Visit(IGeoBoundingBoxFilter customFiltersScore)
{
}
Expand Down
5 changes: 4 additions & 1 deletion src/Nest/Domain/DSL/Filter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Nest
Expand Down Expand Up @@ -52,6 +51,10 @@ public static FilterContainer GeoDistance(string field, Action<GeoDistanceFilter
{
return new FilterDescriptor<T>().GeoDistance(field, filterDescriptor);
}
public static FilterContainer GeoHashCell(string field, Action<GeoHashCellFilterDescriptor> filterDescriptor)
{
return new FilterDescriptor<T>().GeoHashCell(field, filterDescriptor);
}
public static FilterContainer GeoDistanceRange(Expression<Func<T, object>> fieldDescriptor, Action<GeoDistanceRangeFilterDescriptor> filterDescriptor)
{
return new FilterDescriptor<T>().GeoDistanceRange(fieldDescriptor, filterDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public GeoPointMappingDescriptor<T> GeoHashPrecision(int geoHashPrecision)
return this;
}

public GeoPointMappingDescriptor<T> GeoHashPrefix(bool geoHashPrefix = true)
{
this._Mapping.GeoHashPrefix = geoHashPrefix;
return this;
}

public GeoPointMappingDescriptor<T> FieldData(Func<FieldDataNonStringMappingDescriptor, FieldDataNonStringMappingDescriptor> fieldDataSelector)
{
fieldDataSelector.ThrowIfNull("fieldDataSelector");
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/Domain/Mapping/Types/GeoPointMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class GeoPointMapping : IElasticType
[JsonProperty("geohash")]
public bool? IndexGeoHash { get; set; }

[JsonProperty("geohash_prefix")]
public bool? GeoHashPrefix { get; set; }

[JsonProperty("geohash_precision")]
public int? GeoHashPrecision { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
<Compile Include="DSL\Filter\GeoHashCellFilterDescriptor.cs" />
<Compile Include="DSL\PutSearchTemplateDescriptor.cs" />
<Compile Include="DSL\DeleteSearchTemplateDescriptor.cs" />
<Compile Include="DSL\GetSearchTemplateDescriptor.cs" />
Expand Down Expand Up @@ -820,6 +821,7 @@
<Compile Include="DSL\Filter\IdsFilterDescriptor.cs" />
<Compile Include="DSL\Filter\ExistsFilterDescriptor.cs" />
<Compile Include="Resolvers\Converters\CatThreadPoolRecordConverter.cs" />
<Compile Include="Resolvers\Converters\GeoHashCellFilterConverter.cs" />
<Compile Include="Resolvers\Converters\GeoShapeConverterBase.cs" />
<Compile Include="Resolvers\Converters\GetRepositoryResponseConverter.cs" />
<Compile Include="Resolvers\Converters\MappingTransformConverter.cs" />
Expand Down
101 changes: 101 additions & 0 deletions src/Nest/Resolvers/Converters/GeoHashCellFilterConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Nest.Resolvers.Converters
{
public class GeoHashCellFilterConverter : JsonConverter
{
public override bool CanRead { get { return true; } }
public override bool CanWrite { get { return true; } }

public override bool CanConvert(Type objectType)
{
return true; //only to be used with attribute or contract registration.
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var f = value as IGeoHashCellFilter;
if (f == null || (f.IsConditionless && !f.IsVerbatim)) return;

var contract = serializer.ContractResolver as SettingsContractResolver;
if (contract == null)
return;

var field = contract.Infer.PropertyPath(f.Field);
if (field.IsNullOrEmpty())
return;

writer.WriteStartObject();
{
WriteProperty(writer, f, field, f.Location);
WriteProperty(writer, f, "precision", f.Precision);
SerializeProperty(writer, serializer, f, "unit", f.Unit);
SerializeProperty(writer, serializer, f, "neighbors", f.Neighbors);

WriteProperty(writer, f, "_cache", f.Cache);
WriteProperty(writer, f, "_cache_key", f.CacheKey);
WriteProperty(writer, f, "_name", f.FilterName);
}

writer.WriteEndObject();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var j = JObject.Load(reader);
if (j == null || !j.HasValues)
return null;

IGeoHashCellFilter filter = new GeoHashCellFilterDescriptor();

foreach (var jv in j)
{
switch (jv.Key)
{
case "precision":
filter.Precision = jv.Value.Value<string>();
break;
case "neighbors":
filter.Neighbors = jv.Value.Value<bool>();
break;
case "unit":
filter.Unit = jv.Value.Value<string>().ToEnum<GeoUnit>();
break;
case "_cache":
filter.Cache = jv.Value.Value<bool>();
break;
case "_cache_key":
filter.CacheKey = jv.Value.Value<string>();
break;
case "_name":
filter.FilterName = jv.Value.Value<string>();
break;
default:
filter.Field = jv.Key;
filter.Location = jv.Value.Value<string>();
break;
}
}

return filter;
}

private static void SerializeProperty(JsonWriter writer, JsonSerializer serializer, IFilter filter, string field, object value)
{
if ((field.IsNullOrEmpty() || value == null))
return;
writer.WritePropertyName(field);
serializer.Serialize(writer, value);
}
private static void WriteProperty(JsonWriter writer, IFilter filter, string field, object value)
{
if ((field.IsNullOrEmpty() || value == null))
return;
writer.WritePropertyName(field);
writer.WriteValue(value);
}
}

}
2 changes: 2 additions & 0 deletions src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
<Compile Include="QueryParsers\Filter\GeoBoundingBoxFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoDistanceFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoDistanceRangeFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoHashCellFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoIndexShapeFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoPolygonFilterTests.cs" />
<Compile Include="QueryParsers\Filter\GeoShapeFilterTests.cs" />
Expand Down Expand Up @@ -395,6 +396,7 @@
<Compile Include="Search\Fields\FieldsTests.cs" />
<Compile Include="Search\Filter\Modes\ConditionlessFilterJson.cs" />
<Compile Include="Search\Filter\Modes\FilterModesTests.cs" />
<Compile Include="Search\Filter\Singles\GeoHashCellFilterJson.cs" />
<Compile Include="Search\Filter\Singles\TermsLookupFilterJson.cs" />
<Compile Include="Search\Filter\Singles\GeoIndexedShapeFilterJson.cs" />
<Compile Include="Search\Filter\Singles\GeoShapeFilterJson.cs" />
Expand Down
Loading