Skip to content

Commit b07cd50

Browse files
committed
Add context suggester support
Closes #927
1 parent 8005509 commit b07cd50

15 files changed

+379
-0
lines changed

Diff for: src/Nest/DSL/Suggest/CompletionSuggestDescriptor.cs

+11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ public interface ICompletionSuggester : ISuggester
1111
{
1212
[JsonProperty(PropertyName = "fuzzy")]
1313
IFuzzySuggester Fuzzy { get; set; }
14+
15+
[JsonProperty("context")]
16+
IDictionary<string, object> Context { get; set; }
1417
}
1518

1619
public class CompletionSuggester : Suggester, ICompletionSuggester
1720
{
1821
public IFuzzySuggester Fuzzy { get; set; }
22+
public IDictionary<string, object> Context { get; set; }
1923
}
2024

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

2529
IFuzzySuggester ICompletionSuggester.Fuzzy { get; set; }
2630

31+
IDictionary<string, object> ICompletionSuggester.Context { get; set; }
32+
2733
public CompletionSuggestDescriptor<T> Size(int size)
2834
{
2935
Self.Size = size;
@@ -59,6 +65,11 @@ public CompletionSuggestDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, Fuzz
5965
return this;
6066
}
6167

68+
public CompletionSuggestDescriptor<T> Context(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> contextDictionary)
69+
{
70+
Self.Context = contextDictionary(new FluentDictionary<string, object>());
71+
return this;
72+
}
6273

6374
}
6475
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
public interface ICategorySuggestContext : ISuggestContext
11+
{
12+
[JsonProperty("default")]
13+
IEnumerable<string> Default { get; set; }
14+
}
15+
16+
[JsonObject]
17+
public class CategorySuggestContext : ICategorySuggestContext
18+
{
19+
public string Type { get { return "category"; } }
20+
public PropertyPathMarker Path { get; set; }
21+
public IEnumerable<string> Default { get; set; }
22+
}
23+
24+
public class CategorySuggestDescriptor<T>
25+
where T : class
26+
{
27+
internal CategorySuggestContext _Context = new CategorySuggestContext();
28+
29+
public CategorySuggestDescriptor<T> Path(string path)
30+
{
31+
this._Context.Path = path;
32+
return this;
33+
}
34+
35+
public CategorySuggestDescriptor<T> Path(Expression<Func<T, object>> objectPath)
36+
{
37+
this._Context.Path = objectPath;
38+
return this;
39+
}
40+
41+
public CategorySuggestDescriptor<T> Default(params string[] defaults)
42+
{
43+
this._Context.Default = defaults;
44+
return this;
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
8+
namespace Nest
9+
{
10+
[JsonObject]
11+
public interface IGeoLocationSuggestContext : ISuggestContext
12+
{
13+
[JsonProperty("precision")]
14+
IEnumerable<string> Precision { get; set; }
15+
16+
[JsonProperty("neighbors")]
17+
bool Neighbors { get; set; }
18+
19+
[JsonProperty("default")]
20+
object Default { get; set; }
21+
}
22+
23+
[JsonObject]
24+
public class GeoLocationSuggestContext : IGeoLocationSuggestContext
25+
{
26+
public string Type { get { return "geo"; } }
27+
28+
public IEnumerable<string> Precision { get; set; }
29+
30+
public bool Neighbors { get; set; }
31+
32+
public PropertyPathMarker Path { get; set; }
33+
34+
public object Default { get; set; }
35+
}
36+
37+
public class GeoLocationSuggestDescriptor<T>
38+
where T : class
39+
{
40+
internal GeoLocationSuggestContext _Context = new GeoLocationSuggestContext();
41+
42+
public GeoLocationSuggestDescriptor<T> Precision(params string[] precisions)
43+
{
44+
this._Context.Precision = precisions;
45+
return this;
46+
}
47+
48+
public GeoLocationSuggestDescriptor<T> Neighbors(bool neighbors = true)
49+
{
50+
this._Context.Neighbors = neighbors;
51+
return this;
52+
}
53+
54+
public GeoLocationSuggestDescriptor<T> Path(string path)
55+
{
56+
this._Context.Path = path;
57+
return this;
58+
}
59+
60+
public GeoLocationSuggestDescriptor<T> Path(Expression<Func<T, object>> objectPath)
61+
{
62+
this._Context.Path = objectPath;
63+
return this;
64+
}
65+
66+
public GeoLocationSuggestDescriptor<T> Default(LatLon geoPoint)
67+
{
68+
this._Context.Default = geoPoint;
69+
return this;
70+
}
71+
72+
public GeoLocationSuggestDescriptor<T> Default(string geoHash)
73+
{
74+
this._Context.Default = geoHash;
75+
return this;
76+
}
77+
}
78+
}

Diff for: src/Nest/DSL/Suggest/Context/ISuggestContext.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
[JsonObject]
10+
public interface ISuggestContext
11+
{
12+
[JsonProperty("type")]
13+
string Type { get; }
14+
15+
[JsonProperty("path")]
16+
PropertyPathMarker Path { get; set; }
17+
}
18+
}

Diff for: src/Nest/Domain/Mapping/Descriptors/CompletionMappingDescriptor.cs

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Nest
77
{
88
public class CompletionMappingDescriptor<T>
9+
where T : class
910
{
1011
internal CompletionMapping _Mapping = new CompletionMapping();
1112

@@ -56,5 +57,23 @@ public CompletionMappingDescriptor<T> MaxInputLength(int maxInputLength)
5657
this._Mapping.MaxInputLength = maxInputLength;
5758
return this;
5859
}
60+
61+
public CompletionMappingDescriptor<T> Context(Func<SuggestContextMappingDescriptor<T>, SuggestContextMappingDescriptor<T>> contextDescriptor)
62+
{
63+
if (this._Mapping.Context == null)
64+
this._Mapping.Context = new Dictionary<string, ISuggestContext>();
65+
66+
var selector = contextDescriptor(new SuggestContextMappingDescriptor<T>());
67+
68+
foreach (var context in selector._Contexts)
69+
{
70+
if (this._Mapping.Context.ContainsKey(context.Key))
71+
this._Mapping.Context[context.Key] = context.Value;
72+
else
73+
this._Mapping.Context.Add(context.Key, context.Value);
74+
}
75+
76+
return this;
77+
}
5978
}
6079
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Nest
7+
{
8+
public class SuggestContextMappingDescriptor<T>
9+
where T : class
10+
{
11+
internal IDictionary<string, ISuggestContext> _Contexts = new Dictionary<string, ISuggestContext>();
12+
13+
public SuggestContextMappingDescriptor<T> Category(string name, Func<CategorySuggestDescriptor<T>, CategorySuggestDescriptor<T>> categoryDescriptor)
14+
{
15+
var selector = categoryDescriptor(new CategorySuggestDescriptor<T>());
16+
var context = selector._Context;
17+
AddContext(name, context);
18+
return this;
19+
}
20+
21+
public SuggestContextMappingDescriptor<T> GeoLocation(string name, Func<GeoLocationSuggestDescriptor<T>, GeoLocationSuggestDescriptor<T>> geoLocationDescriptor)
22+
{
23+
var selector = geoLocationDescriptor(new GeoLocationSuggestDescriptor<T>());
24+
var context = selector._Context;
25+
AddContext(name, context);
26+
return this;
27+
}
28+
29+
private void AddContext(string key, ISuggestContext context)
30+
{
31+
if (_Contexts.ContainsKey(key))
32+
_Contexts[key] = context;
33+
else
34+
_Contexts.Add(key, context);
35+
}
36+
}
37+
}

Diff for: src/Nest/Domain/Mapping/Types/CompletionMapping.cs

+3
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ public class CompletionMapping : IElasticType
3333

3434
[JsonProperty("max_input_len")]
3535
public int? MaxInputLength { get; set; }
36+
37+
[JsonProperty("context")]
38+
public IDictionary<string, ISuggestContext> Context { get ;set;}
3639
}
3740
}

Diff for: src/Nest/Domain/Suggest/SuggestField.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
[JsonObject]
10+
public class SuggestField
11+
{
12+
[JsonProperty("input")]
13+
public string Input { get; set; }
14+
15+
[JsonProperty("output")]
16+
public string Output { get; set; }
17+
18+
[JsonProperty("payload")]
19+
public object Payload { get; set; }
20+
21+
[JsonProperty("weight")]
22+
public uint Weight { get; set; }
23+
24+
[JsonProperty("context")]
25+
public IDictionary<string, IEnumerable<string>> Context { get; set; }
26+
}
27+
}

Diff for: src/Nest/Nest.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
<Compile Include="Domain\Mapping\Descriptors\FieldDataRegexFilterDescriptor.cs" />
154154
<Compile Include="Domain\Mapping\Descriptors\FieldDataStringMappingDescriptor.cs" />
155155
<Compile Include="Domain\Mapping\Descriptors\MappingTransformDescriptor.cs" />
156+
<Compile Include="Domain\Mapping\Descriptors\SuggestContextMappingDescriptor.cs" />
156157
<Compile Include="Domain\Mapping\IFieldMapping.cs" />
157158
<Compile Include="Domain\Mapping\SpecialFields\ISpecialField.cs" />
158159
<Compile Include="Domain\Geometry\CircleGeoShape.cs" />
@@ -211,6 +212,7 @@
211212
<Compile Include="Domain\Stats\PercolateStats.cs" />
212213
<Compile Include="Domain\Stats\PluginStats.cs" />
213214
<Compile Include="Domain\Stats\SegmentsStats.cs" />
215+
<Compile Include="Domain\Suggest\SuggestField.cs" />
214216
<Compile Include="DSL\Aggregations\PercentileRanksAggregationDescriptor.cs" />
215217
<Compile Include="DSL\Aggregations\TopHitsAggregationDescriptor.cs" />
216218
<Compile Include="DSL\Aggregations\GeoBoundsAggregationDescriptor.cs" />
@@ -238,6 +240,9 @@
238240
<Compile Include="DSL\Paths\RepositorySnapshotOptionalPathDescriptor.cs" />
239241
<Compile Include="DSL\SnapshotStatusDescriptor.cs" />
240242
<Compile Include="DSL\RecoveryStatusDescriptor.cs" />
243+
<Compile Include="DSL\Suggest\Context\CategorySuggestDescriptor.cs" />
244+
<Compile Include="DSL\Suggest\Context\GeoLocationSuggestDescriptor.cs" />
245+
<Compile Include="DSL\Suggest\Context\ISuggestContext.cs" />
241246
<Compile Include="DSL\TemplateExistsDescriptor.cs" />
242247
<Compile Include="Domain\Responses\PingResponse.cs" />
243248
<Compile Include="Domain\Responses\NodesShutdownResponse.cs" />

Diff for: src/Tests/Nest.Tests.MockData/Domain/ElasticsearchProject.cs

+2
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ public class ElasticsearchProject
5555
public Product Product { get; set; }
5656

5757
public string[] MyStringArrayField { get; set; }
58+
59+
public string Suggest { get; set; }
5860
}
5961
}

Diff for: src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ public void MapFluentFull()
239239
.Payloads()
240240
.PreservePositionIncrements()
241241
.PreserveSeparators()
242+
.Context(c => c
243+
.Category("color", cc => cc
244+
.Path(p => p.Country)
245+
.Default("red", "green", "blue")
246+
)
247+
.GeoLocation("location", lc => lc
248+
.Precision("5m")
249+
.Neighbors()
250+
.Default("u33")
251+
)
252+
)
242253
)
243254
)
244255
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"elasticsearchprojects": {
3+
"properties": {
4+
"suggest": {
5+
"type": "completion",
6+
"index_analyzer": "simple",
7+
"search_analyzer": "simple",
8+
"payloads": true,
9+
"max_input_len": 25,
10+
"context": {
11+
"color": {
12+
"type": "category",
13+
"path": "color_field",
14+
"default": ["red", "green", "blue"]
15+
},
16+
"location": {
17+
"type": "geo",
18+
"precision": ["5m"],
19+
"neighbors": true,
20+
"default": "u33"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)