Skip to content

Commit 91fa59c

Browse files
authored
Add skip_duplicates to completion suggester (#3140)
Closes #3130
1 parent 897b2c6 commit 91fa59c

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/Nest/Search/Suggesters/BaseSuggest.cs

+6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ namespace Nest
66
{
77
public interface ISuggester
88
{
9+
/// <summary>
10+
/// The name of the field on which to run the query
11+
/// </summary>
912
[JsonProperty("field")]
1013
Field Field { get; set; }
1114

1215
[JsonProperty("analyzer")]
1316
string Analyzer { get; set; }
1417

18+
/// <summary>
19+
/// The number of suggestions to return. Defaults to 5
20+
/// </summary>
1521
[JsonProperty("size")]
1622
int? Size { get; set; }
1723
}

src/Nest/Search/Suggesters/CompletionSuggester/CompletionSuggester.cs

+42-3
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,89 @@ namespace Nest
99
[JsonConverter(typeof(ReadAsTypeJsonConverter<CompletionSuggester>))]
1010
public interface ICompletionSuggester : ISuggester
1111
{
12+
/// <summary>
13+
/// Prefix used to search for suggestions
14+
/// </summary>
1215
[JsonIgnore]
1316
string Prefix { get; set; }
1417

18+
/// <summary>
19+
/// Prefix as a regular expression used to search for suggestions
20+
/// </summary>
1521
[JsonIgnore]
1622
string Regex { get; set; }
1723

24+
/// <summary>
25+
/// Support fuzziness for the suggestions
26+
/// </summary>
1827
[JsonProperty("fuzzy")]
1928
IFuzzySuggester Fuzzy { get; set; }
2029

30+
/// <summary>
31+
/// Context mappings used to filter and/or boost suggestions
32+
/// </summary>
2133
[JsonProperty("contexts")]
2234
IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; }
35+
36+
/// <summary>
37+
/// Whether duplicate suggestions should be filtered out. Defaults to <c>false</c>
38+
/// </summary>
39+
[JsonProperty("skip_duplicates")]
40+
bool? SkipDuplicates { get; set; }
2341
}
2442

2543
public class CompletionSuggester : SuggesterBase, ICompletionSuggester
2644
{
45+
/// <inheritdoc />
2746
public IFuzzySuggester Fuzzy { get; set; }
2847

48+
/// <inheritdoc />
2949
public IDictionary<string, IList<ISuggestContextQuery>> Contexts { get; set; }
3050

51+
/// <inheritdoc />
3152
public string Prefix { get; set; }
3253

54+
/// <inheritdoc />
3355
public string Regex { get; set; }
56+
57+
/// <inheritdoc />
58+
public bool? SkipDuplicates { get; set; }
3459
}
3560

3661
public class CompletionSuggesterDescriptor<T> : SuggestDescriptorBase<CompletionSuggesterDescriptor<T>, ICompletionSuggester, T>, ICompletionSuggester
3762
where T : class
3863
{
3964
IFuzzySuggester ICompletionSuggester.Fuzzy { get; set; }
40-
4165
IDictionary<string, IList<ISuggestContextQuery>> ICompletionSuggester.Contexts { get; set; }
42-
4366
string ICompletionSuggester.Prefix { get; set; }
44-
4567
string ICompletionSuggester.Regex { get; set; }
68+
bool? ICompletionSuggester.SkipDuplicates { get; set; }
4669

70+
/// <summary>
71+
/// Prefix used to search for suggestions
72+
/// </summary>
4773
public CompletionSuggesterDescriptor<T> Prefix(string prefix) => Assign(a => a.Prefix = prefix);
4874

75+
/// <summary>
76+
/// Prefix as a regular expression used to search for suggestions
77+
/// </summary>
4978
public CompletionSuggesterDescriptor<T> Regex(string regex) => Assign(a => a.Regex = regex);
5079

80+
/// <summary>
81+
/// Support fuzziness for the suggestions
82+
/// </summary>
5183
public CompletionSuggesterDescriptor<T> Fuzzy(Func<FuzzySuggestDescriptor<T>, IFuzzySuggester> selector = null) =>
5284
Assign(a => a.Fuzzy = selector.InvokeOrDefault(new FuzzySuggestDescriptor<T>()));
5385

86+
/// <summary>
87+
/// Context mappings used to filter and/or boost suggestions
88+
/// </summary>
5489
public CompletionSuggesterDescriptor<T> Contexts(Func<SuggestContextQueriesDescriptor<T>, IPromise<IDictionary<string, IList<ISuggestContextQuery>>>> contexts) =>
5590
Assign(a => a.Contexts = contexts?.Invoke(new SuggestContextQueriesDescriptor<T>()).Value);
5691

92+
/// <summary>
93+
/// Whether duplicate suggestions should be filtered out. Defaults to <c>false</c>
94+
/// </summary>
95+
public CompletionSuggesterDescriptor<T> SkipDuplicates(bool? skipDuplicates = true) => Assign(a => a.SkipDuplicates = skipDuplicates);
5796
}
5897
}

src/Tests/Search/Request/SuggestUsageTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
4242
unicode_aware = false
4343
},
4444
size = 8,
45+
skip_duplicates = true
4546
},
4647
prefix = Project.Instance.Name
4748
} },
@@ -121,6 +122,7 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
121122
.Field(p => p.Suggest)
122123
.Size(8)
123124
.Prefix(Project.Instance.Name)
125+
.SkipDuplicates()
124126
)
125127
.Phrase("my-phrase-suggest", ph => ph
126128
.Collate(c => c
@@ -186,6 +188,7 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
186188
Analyzer = "simple",
187189
Field = Field<Project>(p=>p.Suggest),
188190
Size = 8,
191+
SkipDuplicates = true
189192
}
190193
} },
191194
{ "my-phrase-suggest", new SuggestBucket

0 commit comments

Comments
 (0)