Skip to content

Commit cbf6866

Browse files
committed
Merge pull request #392 from jjherbst/rest_of_preferences
Added both the Preferred and Custom preference
2 parents 6a75540 + 77a52db commit cbf6866

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public void TestExecuteOnNode()
120120
StringAssert.Contains("preference=_only_node:somenode", result.ConnectionStatus.RequestUrl);
121121
}
122122
[Test]
123+
public void TestExecuteOnPreferredNode()
124+
{
125+
var s = new SearchDescriptor<ElasticSearchProject>()
126+
.From(0)
127+
.Size(10)
128+
.ExecuteOnPreferredNode("somenode");
129+
var result = this._client.Search(s);
130+
StringAssert.Contains("preference=_prefer_node:somenode", result.ConnectionStatus.RequestUrl);
131+
}
132+
[Test]
123133
public void TestFields()
124134
{
125135
var s = new SearchDescriptor<ElasticSearchProject>()

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 29 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,21 @@ 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+
/// Prefers execution on the node with the provided node id if applicable.
421+
/// </para>
422+
/// </summary>
423+
public SearchDescriptor<T> ExecuteOnPreferredNode(string node)
424+
{
425+
node.ThrowIfNull("node");
426+
this._Preference = string.Format("_prefer_node:{0}", node);
427+
return this;
428+
}
429+
/// <summary>
415430
/// Allows to configure different boost level per index when searching across
416431
/// more than one indices. This is very handy when hits coming from one index
417432
/// matter more than hits coming from another index (think social graph where each user has an index).
@@ -888,26 +903,26 @@ public SearchDescriptor<T> PhraseSuggest(string name, Func<PhraseSuggestDescript
888903
return this;
889904
}
890905

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-
}
906+
public SearchDescriptor<T> CompletionSuggest(string name, Func<CompletionSuggestDescriptor<T>, CompletionSuggestDescriptor<T>> suggest)
907+
{
908+
name.ThrowIfNullOrEmpty("name");
909+
suggest.ThrowIfNull("suggest");
910+
if (this._Suggest == null)
911+
this._Suggest = new Dictionary<String, SuggestDescriptorBucket<T>>();
912+
CompletionSuggestDescriptor<T> desc = new CompletionSuggestDescriptor<T>();
913+
CompletionSuggestDescriptor<T> item = suggest(desc);
914+
SuggestDescriptorBucket<T> bucket = new SuggestDescriptorBucket<T> { _Text = item._Text, CompletionSuggest = item };
915+
this._Suggest.Add(name, bucket);
916+
return this;
917+
}
903918

904919
/// <summary>
905920
/// Describe the query to perform using a query descriptor lambda
906921
/// </summary>
907922
public SearchDescriptor<T> Query(Func<QueryDescriptor<T>, BaseQuery> query)
908923
{
909924
query.ThrowIfNull("query");
910-
var q = new QueryDescriptor<T>() {IsStrict = this._Strict};
925+
var q = new QueryDescriptor<T>() { IsStrict = this._Strict };
911926

912927
var bq = query(q);
913928
return this.Query(bq);

0 commit comments

Comments
 (0)