Skip to content

Feature/context suggester #978

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 3 commits into from
Oct 8, 2014
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
11 changes: 11 additions & 0 deletions src/Nest/DSL/Suggest/CompletionSuggestDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ public interface ICompletionSuggester : ISuggester
{
[JsonProperty(PropertyName = "fuzzy")]
IFuzzySuggester Fuzzy { get; set; }

[JsonProperty("context")]
IDictionary<string, object> Context { get; set; }
}

public class CompletionSuggester : Suggester, ICompletionSuggester
{
public IFuzzySuggester Fuzzy { get; set; }
public IDictionary<string, object> Context { get; set; }
}

public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T>, ICompletionSuggester where T : class
Expand All @@ -24,6 +28,8 @@ public class CompletionSuggestDescriptor<T> : BaseSuggestDescriptor<T>, IComplet

IFuzzySuggester ICompletionSuggester.Fuzzy { get; set; }

IDictionary<string, object> ICompletionSuggester.Context { get; set; }

public CompletionSuggestDescriptor<T> Size(int size)
{
Self.Size = size;
Expand Down Expand Up @@ -59,6 +65,11 @@ public CompletionSuggestDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, Fuzz
return this;
}

public CompletionSuggestDescriptor<T> Context(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> contextDictionary)
{
Self.Context = contextDictionary(new FluentDictionary<string, object>());
return this;
}

}
}
47 changes: 47 additions & 0 deletions src/Nest/DSL/Suggest/Context/CategorySuggestDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Nest
{
public interface ICategorySuggestContext : ISuggestContext
{
[JsonProperty("default")]
IEnumerable<string> Default { get; set; }
}

[JsonObject]
public class CategorySuggestContext : ICategorySuggestContext
{
public string Type { get { return "category"; } }
public PropertyPathMarker Path { get; set; }
public IEnumerable<string> Default { get; set; }
}

public class CategorySuggestDescriptor<T>
where T : class
{
internal CategorySuggestContext _Context = new CategorySuggestContext();

public CategorySuggestDescriptor<T> Path(string path)
{
this._Context.Path = path;
return this;
}

public CategorySuggestDescriptor<T> Path(Expression<Func<T, object>> objectPath)
{
this._Context.Path = objectPath;
return this;
}

public CategorySuggestDescriptor<T> Default(params string[] defaults)
{
this._Context.Default = defaults;
return this;
}
}
}
78 changes: 78 additions & 0 deletions src/Nest/DSL/Suggest/Context/GeoLocationSuggestDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Nest
{
[JsonObject]
public interface IGeoLocationSuggestContext : ISuggestContext
{
[JsonProperty("precision")]
IEnumerable<string> Precision { get; set; }

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

[JsonProperty("default")]
object Default { get; set; }
}

[JsonObject]
public class GeoLocationSuggestContext : IGeoLocationSuggestContext
{
public string Type { get { return "geo"; } }

public IEnumerable<string> Precision { get; set; }

public bool Neighbors { get; set; }

public PropertyPathMarker Path { get; set; }

public object Default { get; set; }
}

public class GeoLocationSuggestDescriptor<T>
where T : class
{
internal GeoLocationSuggestContext _Context = new GeoLocationSuggestContext();

public GeoLocationSuggestDescriptor<T> Precision(params string[] precisions)
{
this._Context.Precision = precisions;
return this;
}

public GeoLocationSuggestDescriptor<T> Neighbors(bool neighbors = true)
{
this._Context.Neighbors = neighbors;
return this;
}

public GeoLocationSuggestDescriptor<T> Path(string path)
{
this._Context.Path = path;
return this;
}

public GeoLocationSuggestDescriptor<T> Path(Expression<Func<T, object>> objectPath)
{
this._Context.Path = objectPath;
return this;
}

public GeoLocationSuggestDescriptor<T> Default(LatLon geoPoint)
{
this._Context.Default = geoPoint;
return this;
}

public GeoLocationSuggestDescriptor<T> Default(string geoHash)
{
this._Context.Default = geoHash;
return this;
}
}
}
18 changes: 18 additions & 0 deletions src/Nest/DSL/Suggest/Context/ISuggestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
[JsonObject]
public interface ISuggestContext
{
[JsonProperty("type")]
string Type { get; }

[JsonProperty("path")]
PropertyPathMarker Path { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/Nest/Domain/Mapping/Descriptors/CompletionMappingDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Nest
{
public class CompletionMappingDescriptor<T>
where T : class
{
internal CompletionMapping _Mapping = new CompletionMapping();

Expand Down Expand Up @@ -56,5 +57,23 @@ public CompletionMappingDescriptor<T> MaxInputLength(int maxInputLength)
this._Mapping.MaxInputLength = maxInputLength;
return this;
}

public CompletionMappingDescriptor<T> Context(Func<SuggestContextMappingDescriptor<T>, SuggestContextMappingDescriptor<T>> contextDescriptor)
{
if (this._Mapping.Context == null)
this._Mapping.Context = new Dictionary<string, ISuggestContext>();

var selector = contextDescriptor(new SuggestContextMappingDescriptor<T>());

foreach (var context in selector._Contexts)
{
if (this._Mapping.Context.ContainsKey(context.Key))
this._Mapping.Context[context.Key] = context.Value;
else
this._Mapping.Context.Add(context.Key, context.Value);
}

return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
public class SuggestContextMappingDescriptor<T>
where T : class
{
internal IDictionary<string, ISuggestContext> _Contexts = new Dictionary<string, ISuggestContext>();

public SuggestContextMappingDescriptor<T> Category(string name, Func<CategorySuggestDescriptor<T>, CategorySuggestDescriptor<T>> categoryDescriptor)
{
var selector = categoryDescriptor(new CategorySuggestDescriptor<T>());
var context = selector._Context;
AddContext(name, context);
return this;
}

public SuggestContextMappingDescriptor<T> GeoLocation(string name, Func<GeoLocationSuggestDescriptor<T>, GeoLocationSuggestDescriptor<T>> geoLocationDescriptor)
{
var selector = geoLocationDescriptor(new GeoLocationSuggestDescriptor<T>());
var context = selector._Context;
AddContext(name, context);
return this;
}

private void AddContext(string key, ISuggestContext context)
{
if (_Contexts.ContainsKey(key))
_Contexts[key] = context;
else
_Contexts.Add(key, context);
}
}
}
3 changes: 3 additions & 0 deletions src/Nest/Domain/Mapping/Types/CompletionMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public class CompletionMapping : IElasticType

[JsonProperty("max_input_len")]
public int? MaxInputLength { get; set; }

[JsonProperty("context")]
public IDictionary<string, ISuggestContext> Context { get ;set;}
}
}
27 changes: 27 additions & 0 deletions src/Nest/Domain/Suggest/SuggestField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest
{
[JsonObject]
public class SuggestField
{
[JsonProperty("input")]
public string Input { get; set; }

[JsonProperty("output")]
public string Output { get; set; }

[JsonProperty("payload")]
public object Payload { get; set; }

[JsonProperty("weight")]
public uint Weight { get; set; }

[JsonProperty("context")]
public IDictionary<string, IEnumerable<string>> Context { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<Compile Include="Domain\Mapping\Descriptors\FieldDataRegexFilterDescriptor.cs" />
<Compile Include="Domain\Mapping\Descriptors\FieldDataStringMappingDescriptor.cs" />
<Compile Include="Domain\Mapping\Descriptors\MappingTransformDescriptor.cs" />
<Compile Include="Domain\Mapping\Descriptors\SuggestContextMappingDescriptor.cs" />
<Compile Include="Domain\Mapping\IFieldMapping.cs" />
<Compile Include="Domain\Mapping\SpecialFields\ISpecialField.cs" />
<Compile Include="Domain\Geometry\CircleGeoShape.cs" />
Expand Down Expand Up @@ -211,6 +212,7 @@
<Compile Include="Domain\Stats\PercolateStats.cs" />
<Compile Include="Domain\Stats\PluginStats.cs" />
<Compile Include="Domain\Stats\SegmentsStats.cs" />
<Compile Include="Domain\Suggest\SuggestField.cs" />
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
Expand Down Expand Up @@ -238,6 +240,9 @@
<Compile Include="DSL\Paths\RepositorySnapshotOptionalPathDescriptor.cs" />
<Compile Include="DSL\SnapshotStatusDescriptor.cs" />
<Compile Include="DSL\RecoveryStatusDescriptor.cs" />
<Compile Include="DSL\Suggest\Context\CategorySuggestDescriptor.cs" />
<Compile Include="DSL\Suggest\Context\GeoLocationSuggestDescriptor.cs" />
<Compile Include="DSL\Suggest\Context\ISuggestContext.cs" />
<Compile Include="DSL\TemplateExistsDescriptor.cs" />
<Compile Include="Domain\Responses\PingResponse.cs" />
<Compile Include="Domain\Responses\NodesShutdownResponse.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/Tests/Nest.Tests.MockData/Domain/ElasticsearchProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ public class ElasticsearchProject
public Product Product { get; set; }

public string[] MyStringArrayField { get; set; }

public string Suggest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ public void MapFluentFull()
.Payloads()
.PreservePositionIncrements()
.PreserveSeparators()
.Context(c => c
.Category("color", cc => cc
.Path(p => p.Country)
.Default("red", "green", "blue")
)
.GeoLocation("location", lc => lc
.Precision("5m")
.Neighbors()
.Default("u33")
)
)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"elasticsearchprojects": {
"properties": {
"suggest": {
"type": "completion",
"index_analyzer": "simple",
"search_analyzer": "simple",
"payloads": true,
"max_input_len": 25,
"context": {
"color": {
"type": "category",
"path": "color_field",
"default": ["red", "green", "blue"]
},
"location": {
"type": "geo",
"precision": ["5m"],
"neighbors": true,
"default": "u33"
}
}
}
}
}
}
Loading