Skip to content

Commit 685c473

Browse files
author
Juan
committed
Added both the Preferred and Custom preference
1 parent 5eee769 commit 685c473

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/Nest.Tests.Unit/Search/SearchOptions/SearchOptionTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ public void TestExecuteOnNode()
120120
StringAssert.Contains("preference=_only_node:somenode", result.ConnectionStatus.RequestUrl);
121121
}
122122
[Test]
123+
public void TestExecuteOnCustomNode()
124+
{
125+
var s = new SearchDescriptor<ElasticSearchProject>()
126+
.From(0)
127+
.Size(10)
128+
.ExecuteOnCustomNode("somenode");
129+
var result = this._client.Search(s);
130+
StringAssert.Contains("preference=somenode", result.ConnectionStatus.RequestUrl);
131+
}
132+
[Test]
133+
public void TestExecuteOnPreferredNode()
134+
{
135+
var s = new SearchDescriptor<ElasticSearchProject>()
136+
.From(0)
137+
.Size(10)
138+
.ExecuteOnPreferredNode("somenode");
139+
var result = this._client.Search(s);
140+
StringAssert.Contains("preference=_prefer_node:somenode", result.ConnectionStatus.RequestUrl);
141+
}
142+
[Test]
123143
public void TestFields()
124144
{
125145
var s = new SearchDescriptor<ElasticSearchProject>()

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public abstract class SearchDescriptorBase
2828

2929
internal Func<dynamic, Hit<dynamic>, Type> _ConcreteTypeSelector;
3030

31-
31+
3232
}
3333

3434
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
@@ -412,6 +412,39 @@ public SearchDescriptor<T> ExecuteOnNode(string node)
412412
return this;
413413
}
414414
/// <summary>
415+
/// <para>
416+
/// Controls a preference of which shard replicas to execute the search request on.
417+
/// By default, the operation is randomized between the each shard replicas.
418+
/// </para>
419+
/// <para>
420+
/// A custom value will be used to guarantee that the same shards will be used for
421+
/// the same custom value. This can help with "jumping values" when hitting different
422+
/// shards in different refresh states. A sample value can be something like the
423+
/// web session id, or the user name.
424+
/// </para>
425+
/// </summary>
426+
public SearchDescriptor<T> ExecuteOnCustomNode(string node)
427+
{
428+
node.ThrowIfNull("node");
429+
this._Preference = node;
430+
return this;
431+
}
432+
/// <summary>
433+
/// <para>
434+
/// Controls a preference of which shard replicas to execute the search request on.
435+
/// By default, the operation is randomized between the each shard replicas.
436+
/// </para>
437+
/// <para>
438+
/// Prefers execution on the node with the provided node id if applicable.
439+
/// </para>
440+
/// </summary>
441+
public SearchDescriptor<T> ExecuteOnPreferredNode(string node)
442+
{
443+
node.ThrowIfNull("node");
444+
this._Preference = string.Format("_prefer_node:{0}", node);
445+
return this;
446+
}
447+
/// <summary>
415448
/// Allows to configure different boost level per index when searching across
416449
/// more than one indices. This is very handy when hits coming from one index
417450
/// matter more than hits coming from another index (think social graph where each user has an index).
@@ -888,26 +921,26 @@ public SearchDescriptor<T> PhraseSuggest(string name, Func<PhraseSuggestDescript
888921
return this;
889922
}
890923

891-
public SearchDescriptor<T> CompletionSuggest(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest)
892-
{
893-
name.ThrowIfNullOrEmpty("name");
894-
suggest.ThrowIfNull("suggest");
895-
if (this._Suggest == null)
896-
this._Suggest = new Dictionary<String, SuggestDescriptorBucket<T>>();
897-
CompletionSuggestDescriptor<T> desc = new CompletionSuggestDescriptor<T>();
898-
CompletionSuggestDescriptor<T> item = suggest(desc);
899-
SuggestDescriptorBucket<T> bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item };
900-
this._Suggest.Add(name, bucket);
901-
return this;
902-
}
924+
public SearchDescriptor<T> CompletionSuggest(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest)
925+
{
926+
name.ThrowIfNullOrEmpty("name");
927+
suggest.ThrowIfNull("suggest");
928+
if (this._Suggest == null)
929+
this._Suggest = new Dictionary<String, SuggestDescriptorBucket<T>>();
930+
CompletionSuggestDescriptor<T> desc = new CompletionSuggestDescriptor<T>();
931+
CompletionSuggestDescriptor<T> item = suggest(desc);
932+
SuggestDescriptorBucket<T> bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item };
933+
this._Suggest.Add(name, bucket);
934+
return this;
935+
}
903936

904937
/// <summary>
905938
/// Describe the query to perform using a query descriptor lambda
906939
/// </summary>
907940
public SearchDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> query)
908941
{
909942
query.ThrowIfNull("query");
910-
var q = new QueryDescriptor<T>() {IsStrict = this._Strict};
943+
var q = new QueryDescriptor<T>() { IsStrict = this._Strict };
911944

912945
var bq = query(q);
913946
return this.Query(bq);

0 commit comments

Comments
 (0)