Skip to content

Commit 62a0ccb

Browse files
committed
Support for the new completion suggester and other _suggest API changes introduced in ES master
Down to ~70 failing integration tests.
1 parent be14c87 commit 62a0ccb

33 files changed

+505
-330
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
namespace Nest
1+
using System.Collections.Generic;
2+
3+
namespace Nest
24
{
35
public class CompletionAttribute : ElasticsearchPropertyAttributeBase, ICompletionProperty
46
{
57
ICompletionProperty Self => this;
68

79
string ICompletionProperty.SearchAnalyzer { get; set; }
810
string ICompletionProperty.Analyzer { get; set; }
9-
bool? ICompletionProperty.Payloads { get; set; }
1011
bool? ICompletionProperty.PreserveSeparators { get; set; }
1112
bool? ICompletionProperty.PreservePositionIncrements { get; set; }
1213
int? ICompletionProperty.MaxInputLength { get; set; }
13-
ISuggestContextMapping ICompletionProperty.Context { get; set; }
14+
IList<ISuggestContext> ICompletionProperty.Contexts { get; set; }
1415

1516
public string SearchAnalyzer { get { return Self.SearchAnalyzer; } set { Self.SearchAnalyzer = value; } }
1617
public string Analyzer { get { return Self.Analyzer; } set { Self.Analyzer = value; } }
17-
public bool Payloads { get { return Self.Payloads.GetValueOrDefault(); } set { Self.Payloads = value; } }
1818
public bool PreserveSeparators { get { return Self.PreserveSeparators.GetValueOrDefault(); } set { Self.PreserveSeparators = value; } }
1919
public bool PreservePositionIncrements { get { return Self.PreservePositionIncrements.GetValueOrDefault(); } set { Self.PreservePositionIncrements = value; } }
2020
public int MaxInputLength { get { return Self.MaxInputLength.GetValueOrDefault(); } set { Self.MaxInputLength = value; } }
2121

2222
public CompletionAttribute() : base("completion") { }
23-
}
23+
}
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using Newtonsoft.Json;
3+
using System.Collections.Generic;
4+
using System.Linq;
35

46
namespace Nest
57
{
@@ -12,9 +14,6 @@ public interface ICompletionProperty : IProperty
1214
[JsonProperty("analyzer")]
1315
string Analyzer { get; set; }
1416

15-
[JsonProperty("payloads")]
16-
bool? Payloads { get; set; }
17-
1817
[JsonProperty("preserve_separators")]
1918
bool? PreserveSeparators { get; set; }
2019

@@ -24,8 +23,8 @@ public interface ICompletionProperty : IProperty
2423
[JsonProperty("max_input_length")]
2524
int? MaxInputLength { get; set; }
2625

27-
[JsonProperty("context")]
28-
ISuggestContextMapping Context { get; set; }
26+
[JsonProperty("contexts")]
27+
IList<ISuggestContext> Contexts { get; set; }
2928
}
3029

3130
[JsonObject(MemberSerialization.OptIn)]
@@ -35,11 +34,10 @@ public CompletionProperty() : base("completion") { }
3534

3635
public string SearchAnalyzer { get; set; }
3736
public string Analyzer { get; set; }
38-
public bool? Payloads { get; set; }
3937
public bool? PreserveSeparators { get; set; }
4038
public bool? PreservePositionIncrements { get; set; }
4139
public int? MaxInputLength { get; set; }
42-
public ISuggestContextMapping Context { get; set; }
40+
public IList<ISuggestContext> Contexts { get; set; }
4341
}
4442

4543
public class CompletionPropertyDescriptor<T>
@@ -48,11 +46,10 @@ public class CompletionPropertyDescriptor<T>
4846
{
4947
string ICompletionProperty.SearchAnalyzer { get; set; }
5048
string ICompletionProperty.Analyzer { get; set; }
51-
bool? ICompletionProperty.Payloads { get; set; }
5249
bool? ICompletionProperty.PreserveSeparators { get; set; }
5350
bool? ICompletionProperty.PreservePositionIncrements { get; set; }
5451
int? ICompletionProperty.MaxInputLength { get; set; }
55-
ISuggestContextMapping ICompletionProperty.Context { get; set; }
52+
IList<ISuggestContext> ICompletionProperty.Contexts { get; set; }
5653

5754
public CompletionPropertyDescriptor() : base("completion") { }
5855

@@ -61,8 +58,6 @@ public CompletionPropertyDescriptor<T> SearchAnalyzer(string searchAnalyzer) =>
6158

6259
public CompletionPropertyDescriptor<T> Analyzer(string analyzer) => Assign(a => a.Analyzer = analyzer);
6360

64-
public CompletionPropertyDescriptor<T> Payloads(bool payloads = true) => Assign(a => a.Payloads = payloads);
65-
6661
public CompletionPropertyDescriptor<T> PreserveSeparators(bool preserveSeparators = true) =>
6762
Assign(a => a.PreserveSeparators = preserveSeparators);
6863

@@ -71,8 +66,7 @@ public CompletionPropertyDescriptor<T> PreservePositionIncrements(bool preserveP
7166

7267
public CompletionPropertyDescriptor<T> MaxInputLength(int maxInputLength) => Assign(a => a.MaxInputLength = maxInputLength);
7368

74-
public CompletionPropertyDescriptor<T> Context(Func<SuggestContextMappingDescriptor<T>, IPromise<ISuggestContextMapping>> selector) =>
75-
Assign(a => a.Context = selector?.Invoke(new SuggestContextMappingDescriptor<T>())?.Value);
76-
69+
public CompletionPropertyDescriptor<T> Contexts(Func<SuggestContextsDescriptor<T>, IPromise<IList<ISuggestContext>>> contexts) =>
70+
Assign(a => a.Contexts = contexts?.Invoke(new SuggestContextsDescriptor<T>()).Value);
7771
}
78-
}
72+
}

Diff for: src/Nest/Mapping/Visitor/PropertyWalker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private IProperty InferProperty(Type type)
128128
if (type == typeof(GeoLocation))
129129
return new GeoPointProperty();
130130

131-
if (type.IsGeneric() && type.GetGenericTypeDefinition() == typeof(CompletionField<>))
131+
if (type == typeof(CompletionField))
132132
return new CompletionProperty();
133133

134134
return new ObjectProperty();

Diff for: src/Nest/Nest.csproj

+8-3
Original file line numberDiff line numberDiff line change
@@ -1144,23 +1144,28 @@
11441144
<Compile Include="Search\Suggesters\CompletionSuggester\CompletionSuggester.cs" />
11451145
<Compile Include="Search\Suggesters\CompletionSuggester\FuzzySuggest.cs" />
11461146
<Compile Include="Search\Suggesters\ContextSuggester\CategorySuggestContext.cs" />
1147-
<Compile Include="Search\Suggesters\ContextSuggester\GeoLocationSuggestContext.cs" />
1148-
<Compile Include="Search\Suggesters\ContextSuggester\ISuggestContext.cs" />
1147+
<Compile Include="Search\Suggesters\ContextSuggester\CategorySuggestContextQuery.cs" />
1148+
<Compile Include="Search\Suggesters\ContextSuggester\GeoSuggestContext.cs" />
1149+
<Compile Include="Search\Suggesters\ContextSuggester\GeoSuggestContextQuery.cs" />
11491150
<Compile Include="Search\Suggesters\ContextSuggester\SuggestContextJsonConverter.cs" />
1151+
<Compile Include="Search\Suggesters\ContextSuggester\ISuggestContext.cs" />
1152+
<Compile Include="Search\Suggesters\ContextSuggester\SuggestContextQuery.cs" />
1153+
<Compile Include="Search\Suggesters\ContextSuggester\SuggestContextsDescriptor.cs" />
11501154
<Compile Include="Search\Suggesters\PhraseSuggester\DirectGenerator.cs" />
11511155
<Compile Include="Search\Suggesters\PhraseSuggester\PhraseSuggestCollate.cs" />
11521156
<Compile Include="Search\Suggesters\PhraseSuggester\PhraseSuggester.cs" />
11531157
<Compile Include="Search\Suggesters\PhraseSuggester\PhraseSuggestHighlight.cs" />
11541158
<Compile Include="Search\Suggesters\Suggest.cs" />
11551159
<Compile Include="Search\Suggesters\SuggestBucket.cs" />
11561160
<Compile Include="Search\Suggesters\SuggestContainer.cs" />
1157-
<Compile Include="Search\Suggesters\SuggestContextMapping.cs" />
11581161
<Compile Include="Search\Suggesters\SuggestOption.cs" />
11591162
<Compile Include="Search\Suggesters\Suggest\ElasticClient-Suggest.cs" />
11601163
<Compile Include="Search\Suggesters\Suggest\SuggestRequest.cs" />
11611164
<Compile Include="Search\Suggesters\Suggest\SuggestRequestJsonConverter.cs" />
11621165
<Compile Include="Search\Suggesters\Suggest\SuggestResponse.cs" />
11631166
<Compile Include="Search\Suggesters\Suggest\SuggestResponseJsonConverter.cs" />
1167+
<Compile Include="Search\Suggesters\TermSuggester\StringDistance.cs" />
1168+
<Compile Include="Search\Suggesters\TermSuggester\SuggestSort.cs" />
11641169
<Compile Include="Search\Suggesters\TermSuggester\TermSuggester.cs" />
11651170
<Compile Include="Search\Validate\ElasticClient-ValidateQuery.cs" />
11661171
<Compile Include="Search\Validate\ValidateQueryRequest.cs" />

Diff for: src/Nest/Search/Suggesters/BaseSuggest.cs

+4-19
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,36 @@ namespace Nest
66
{
77
public interface ISuggester
88
{
9-
string Text { get; set; }
10-
11-
[JsonProperty(PropertyName = "field")]
9+
[JsonProperty("field")]
1210
Field Field { get; set; }
1311

14-
[JsonProperty(PropertyName = "analyzer")]
12+
[JsonProperty("analyzer")]
1513
string Analyzer { get; set; }
1614

17-
[JsonProperty(PropertyName = "size")]
15+
[JsonProperty("size")]
1816
int? Size { get; set; }
19-
20-
[JsonProperty(PropertyName = "shard_size")]
21-
int? ShardSize { get; set; }
22-
}
17+
}
2318

2419
public abstract class SuggesterBase : ISuggester
2520
{
26-
public string Text { get; set; }
2721
public Field Field { get; set; }
2822
public string Analyzer { get; set; }
2923
public int? Size { get; set; }
30-
public int? ShardSize { get; set; }
3124
}
3225

3326
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
3427
public abstract class SuggestDescriptorBase<TDescriptor, TInterface, T> : DescriptorBase<TDescriptor, TInterface>, ISuggester
3528
where TDescriptor : SuggestDescriptorBase<TDescriptor, TInterface, T>, TInterface, ISuggester
3629
where TInterface : class, ISuggester
3730
{
38-
string ISuggester.Text { get; set; }
39-
4031
Field ISuggester.Field { get; set; }
4132

4233
string ISuggester.Analyzer { get; set; }
4334

4435
int? ISuggester.Size { get; set; }
4536

46-
int? ISuggester.ShardSize { get; set; }
47-
4837
public TDescriptor Size(int? size) => Assign(a => a.Size = size);
4938

50-
public TDescriptor ShardSize(int? size) => Assign(a => a.ShardSize = size);
51-
52-
public TDescriptor Text(string text) => Assign(a => a.Text = text);
53-
5439
public TDescriptor Analyzer(string analyzer) => Assign(a => a.Analyzer = analyzer);
5540

5641
public TDescriptor Field(Field field) => Assign(a => a.Field = field);

Diff for: src/Nest/Search/Suggesters/CompletionSuggester/CompletionField.cs

+4-11
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@ namespace Nest
66
/// <summary>
77
/// Convenience class for use when indexing completion fields.
88
/// </summary>
9-
[JsonObject]
10-
public class CompletionField<TPayload>
11-
where TPayload : class
9+
[JsonObject(MemberSerialization.OptIn)]
10+
public class CompletionField
1211
{
1312
[JsonProperty("input")]
1413
public IEnumerable<string> Input { get; set; }
1514

16-
[JsonProperty("output")]
17-
public string Output { get; set; }
18-
19-
[JsonProperty("payload")]
20-
public TPayload Payload { get; set; }
21-
2215
[JsonProperty("weight")]
2316
public int? Weight { get; set; }
2417

25-
[JsonProperty("context")]
26-
public IDictionary<string, IEnumerable<string>> Context { get; set; }
18+
[JsonProperty("contexts")]
19+
public IDictionary<string, IEnumerable<string>> Contexts { get; set; }
2720
}
2821
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,84 @@
11
using System;
22
using System.Collections.Generic;
33
using Newtonsoft.Json;
4+
using System.Linq;
45

56
namespace Nest
67
{
78
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
89
[JsonConverter(typeof(ReadAsTypeJsonConverter<CompletionSuggester>))]
910
public interface ICompletionSuggester : ISuggester
1011
{
11-
[JsonProperty(PropertyName = "fuzzy")]
12+
[JsonIgnore]
13+
string Prefix { get; set; }
14+
15+
[JsonIgnore]
16+
string Regex { get; set; }
17+
18+
[JsonProperty("payload")]
19+
Fields Payload { get; set; }
20+
21+
[JsonProperty("fuzzy")]
1222
IFuzzySuggester Fuzzy { get; set; }
1323

14-
[JsonProperty("context")]
15-
IDictionary<string, object> Context { get; set; }
24+
[JsonProperty("contexts")]
25+
IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; }
1626
}
1727

1828
public class CompletionSuggester : SuggesterBase, ICompletionSuggester
1929
{
2030
public IFuzzySuggester Fuzzy { get; set; }
21-
public IDictionary<string, object> Context { get; set; }
31+
32+
public IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; }
33+
34+
public string Prefix { get; set; }
35+
36+
public string Regex { get; set; }
37+
38+
public Fields Payload { get; set; }
2239
}
2340

24-
public class CompletionSuggesterDescriptor<T> : SuggestDescriptorBase<CompletionSuggesterDescriptor<T>, ICompletionSuggester, T>, ICompletionSuggester
41+
public class CompletionSuggesterDescriptor<T> : SuggestDescriptorBase<CompletionSuggesterDescriptor<T>, ICompletionSuggester, T>, ICompletionSuggester
2542
where T : class
2643
{
2744
IFuzzySuggester ICompletionSuggester.Fuzzy { get; set; }
2845

29-
IDictionary<string, object> ICompletionSuggester.Context { get; set; }
46+
IDictionary<string, IList<ISuggestContextQuery>> ICompletionSuggester.Contexts { get; set; }
47+
48+
string ICompletionSuggester.Prefix { get; set; }
49+
50+
string ICompletionSuggester.Regex { get; set; }
51+
52+
Fields ICompletionSuggester.Payload { get; set; }
53+
54+
public CompletionSuggesterDescriptor<T> Prefix(string prefix) => Assign(a => a.Prefix = prefix);
55+
56+
public CompletionSuggesterDescriptor<T> Regex(string regex) => Assign(a => a.Regex = regex);
3057

3158
public CompletionSuggesterDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, IFuzzySuggester> selector = null) =>
3259
Assign(a => a.Fuzzy = selector.InvokeOrDefault(new FuzzySuggestDescriptor<T>()));
3360

34-
public CompletionSuggesterDescriptor<T> Context(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
35-
Assign(a => a.Context = selector?.Invoke(new FluentDictionary<string, object>()));
36-
61+
public CompletionSuggesterDescriptor<T> Contexts(Func<SuggestContextQueriesDescriptor<T>, IPromise<IDictionary<string, IList<ISuggestContextQuery>>>> contexts) =>
62+
Assign(a => a.Contexts = contexts?.Invoke(new SuggestContextQueriesDescriptor<T>()).Value);
63+
64+
public CompletionSuggesterDescriptor<T> Payload(Func<FieldsDescriptor<T>, IPromise<Fields>> payload) =>
65+
Assign(a => a.Payload = payload?.Invoke(new FieldsDescriptor<T>())?.Value);
66+
67+
public CompletionSuggesterDescriptor<T> Payload(Fields payload) => Assign(a => a.Payload = payload);
68+
}
69+
70+
public class SuggestContextQueriesDescriptor<T>
71+
: DescriptorPromiseBase<SuggestContextQueriesDescriptor<T>, IDictionary<string, IList<ISuggestContextQuery>>>
72+
{
73+
public SuggestContextQueriesDescriptor() : base(new Dictionary<string, IList<ISuggestContextQuery>>()) { }
74+
75+
public SuggestContextQueriesDescriptor<T> Category(string name, params Func<CategorySuggestContextQueryDescriptor<T>, ISuggestContextQuery>[] categoryDescriptors) =>
76+
AddContextQueries(name, categoryDescriptors?.Select(d => d?.Invoke(new CategorySuggestContextQueryDescriptor<T>())).ToList());
77+
78+
public SuggestContextQueriesDescriptor<T> GeoLocation(string name, params Func<GeoSuggestContextQueryDescriptor<T>, ISuggestContextQuery>[] geoDescriptors) =>
79+
AddContextQueries(name, geoDescriptors?.Select(d => d?.Invoke(new GeoSuggestContextQueryDescriptor<T>())).ToList());
80+
81+
private SuggestContextQueriesDescriptor<T> AddContextQueries(string name, List<ISuggestContextQuery> contextQueries) =>
82+
contextQueries == null ? this : this.Assign(a => a.Add(name, contextQueries));
3783
}
3884
}

Diff for: src/Nest/Search/Suggesters/ContextSuggester/CategorySuggestContext.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,17 @@
33

44
namespace Nest
55
{
6-
public interface ICategorySuggestContext : ISuggestContext
7-
{
8-
[JsonProperty("default")]
9-
IEnumerable<string> Default { get; set; }
10-
}
6+
public interface ICategorySuggestContext : ISuggestContext { }
117

128
[JsonObject]
139
public class CategorySuggestContext : SuggestContextBase, ICategorySuggestContext
1410
{
1511
public override string Type => "category";
16-
public IEnumerable<string> Default { get; set; }
1712
}
1813

1914
public class CategorySuggestContextDescriptor<T> : SuggestContextDescriptorBase<CategorySuggestContextDescriptor<T>, ICategorySuggestContext, T>, ICategorySuggestContext
2015
where T : class
2116
{
2217
protected override string Type => "category";
23-
IEnumerable<string> ICategorySuggestContext.Default { get; set; }
24-
25-
public CategorySuggestContextDescriptor<T> Default(params string[] defaults) => Assign(a => a.Default = defaults);
26-
27-
public CategorySuggestContextDescriptor<T> Default(IEnumerable<string> defaults) => Assign(a => a.Default = defaults);
2818
}
2919
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Nest
9+
{
10+
[JsonObject(MemberSerialization.OptIn)]
11+
public interface ICategorySuggestContextQuery : ISuggestContextQuery
12+
{
13+
[JsonProperty("prefix")]
14+
bool? Prefix { get; set; }
15+
}
16+
17+
public class CategorySuggestContextQuery : SuggestContextQueryBase, ICategorySuggestContextQuery
18+
{
19+
public bool? Prefix { get; set; }
20+
}
21+
22+
public class CategorySuggestContextQueryDescriptor<T>
23+
: SuggestContextQueryDescriptorBase<CategorySuggestContextQueryDescriptor<T>, ICategorySuggestContextQuery, T>, ICategorySuggestContextQuery
24+
{
25+
bool? ICategorySuggestContextQuery.Prefix { get; set; }
26+
27+
public CategorySuggestContextQueryDescriptor<T> Prefix(bool prefix = true) => Assign(a => a.Prefix = prefix);
28+
}
29+
}

0 commit comments

Comments
 (0)