Skip to content

Commit e12b900

Browse files
committed
added support for global suggest text on search fix #406
1 parent 4c7bce4 commit e12b900

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Nest.Tests.Unit/Search/suggest/PhraseSuggestTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void PhraseSuggestDescriptorDirectGeneratorTest()
5656
public void PhraseSuggestOnSearchTest()
5757
{
5858
var search = this._client.Search<ElasticSearchProject>(s => s
59+
.SuggestGlobalText("glob")
5960
.SuggestPhrase("myphrasesuggest", ts => ts
6061
.Text("n")
6162
.Analyzer("body")
@@ -68,6 +69,7 @@ public void PhraseSuggestOnSearchTest()
6869

6970
var expected = @"{
7071
suggest: {
72+
text: ""glob"",
7173
myphrasesuggest: {
7274
text: ""n"",
7375
phrase: {

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public SearchDescriptor<T> Strict(bool strict = true)
207207
internal IDictionary<string, FacetDescriptorsBucket<T>> _Facets { get; set; }
208208

209209
[JsonProperty(PropertyName = "suggest")]
210-
internal IDictionary<string, SuggestDescriptorBucket<T>> _Suggest { get; set; }
210+
internal IDictionary<string, object> _Suggest { get; set; }
211211

212212
[JsonProperty(PropertyName = "query")]
213213
internal RawOrQueryDescriptor<T> _QueryOrRaw
@@ -877,12 +877,23 @@ public SearchDescriptor<T> FacetFilter(string name, Func<FilterDescriptor<T>, Ba
877877
return this;
878878
}
879879

880+
/// <summary>
881+
/// To avoid repetition of the suggest text, it is possible to define a global text.
882+
/// </summary>
883+
public SearchDescriptor<T> SuggestGlobalText(string globalSuggestText)
884+
{
885+
if (this._Suggest == null)
886+
this._Suggest = new Dictionary<string, object>();
887+
this._Suggest.Add("text", globalSuggestText);
888+
return this;
889+
}
890+
880891
public SearchDescriptor<T> SuggestTerm(string name, Func<TermSuggestDescriptor<T>, TermSuggestDescriptor<T>> suggest)
881892
{
882893
name.ThrowIfNullOrEmpty("name");
883894
suggest.ThrowIfNull("suggest");
884895
if (this._Suggest == null)
885-
this._Suggest = new Dictionary<string, SuggestDescriptorBucket<T>>();
896+
this._Suggest = new Dictionary<string, object>();
886897
var desc = new TermSuggestDescriptor<T>();
887898
var item = suggest(desc);
888899
var bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, TermSuggest = item };
@@ -895,7 +906,7 @@ public SearchDescriptor<T> SuggestPhrase(string name, Func<PhraseSuggestDescript
895906
name.ThrowIfNullOrEmpty("name");
896907
suggest.ThrowIfNull("suggest");
897908
if (this._Suggest == null)
898-
this._Suggest = new Dictionary<string, SuggestDescriptorBucket<T>>();
909+
this._Suggest = new Dictionary<string, object>();
899910

900911
var desc = new PhraseSuggestDescriptor<T>();
901912
var item = suggest(desc);
@@ -909,7 +920,7 @@ public SearchDescriptor<T> SuggestCompletion(string name, Func<CompletionSuggest
909920
name.ThrowIfNullOrEmpty("name");
910921
suggest.ThrowIfNull("suggest");
911922
if (this._Suggest == null)
912-
this._Suggest = new Dictionary<string, SuggestDescriptorBucket<T>>();
923+
this._Suggest = new Dictionary<string, object>();
913924

914925
var desc = new CompletionSuggestDescriptor<T>();
915926
var item = suggest(desc);

0 commit comments

Comments
 (0)