Skip to content

Commit dfa1c45

Browse files
committed
refresh and consistency no longer part of the bulk action but send through the querystring #394
1 parent fa68e88 commit dfa1c45

File tree

9 files changed

+76
-69
lines changed

9 files changed

+76
-69
lines changed

Diff for: src/Nest.Tests.Integration/Core/Bulk/BulkUpdateTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ public void BulkUpdateObject()
2121
result.IsValid.Should().BeTrue();
2222

2323
//Now lets update all of them giving them a name
24-
descriptor = new BulkDescriptor();
24+
descriptor = new BulkDescriptor().Refresh();
2525
foreach (var i in Enumerable.Range(5000, 1000))
2626
{
2727
int id = i;
2828
descriptor.Update<ElasticSearchProject, object>(op => op
2929
.Object(new ElasticSearchProject { Id = id })
3030
.Document(new { name = "SufixedName-" + id})
31-
.Refresh()
3231
);
3332
}
3433

Diff for: src/Nest.Tests.Unit/Core/Bulk/BulkUrlTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ public void BulkNonFixed()
2020
var uri = new Uri(result.ConnectionStatus.RequestUrl);
2121
uri.AbsolutePath.Should().Be("/_bulk");
2222
}
23+
24+
[Test]
25+
public void BulkNonFixedWithParams()
26+
{
27+
var result = this._client.Bulk(b => b
28+
.Refresh()
29+
.Consistency(Consistency.Quorum)
30+
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject {Id = 2}))
31+
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 3 }))
32+
.Delete<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 4 }))
33+
);
34+
var status = result.ConnectionStatus;
35+
var uri = new Uri(result.ConnectionStatus.RequestUrl);
36+
uri.AbsolutePath.Should().Be("/_bulk");
37+
uri.Query.Should().Be("?refresh=true&consistency=quorem");
38+
}
39+
2340
[Test]
2441
public void BulkFixedIndex()
2542
{

Diff for: src/Nest/DSL/BulkCreateDescriptor.cs

-16
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,5 @@ public BulkCreateDescriptor<T> Ttl(string ttl)
127127
this._Ttl = ttl;
128128
return this;
129129
}
130-
131-
public BulkCreateDescriptor<T> Consistency(Consistency consistency)
132-
{
133-
this._Consistency = consistency;
134-
return this;
135-
}
136-
137-
public BulkCreateDescriptor<T> Refresh(bool refresh = true)
138-
{
139-
this._Refresh = refresh;
140-
return this;
141-
}
142-
143-
144-
145-
146130
}
147131
}

Diff for: src/Nest/DSL/BulkDeleteDescriptor.cs

-16
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,5 @@ public BulkDeleteDescriptor<T> Ttl(string ttl)
127127
this._Ttl = ttl;
128128
return this;
129129
}
130-
131-
public BulkDeleteDescriptor<T> Consistency(Consistency consistency)
132-
{
133-
this._Consistency = consistency;
134-
return this;
135-
}
136-
137-
public BulkDeleteDescriptor<T> Refresh(bool refresh = true)
138-
{
139-
this._Refresh = refresh;
140-
return this;
141-
}
142-
143-
144-
145-
146130
}
147131
}

Diff for: src/Nest/DSL/BulkDescriptor.cs

+39-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Linq.Expressions;
66
using Nest.Domain;
77
using System.Collections.Concurrent;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Converters;
810

911

1012
namespace Nest
@@ -14,7 +16,12 @@ public class BulkDescriptor
1416
internal string _FixedIndex { get; set; }
1517
internal string _FixedType { get; set; }
1618

17-
internal IList<BaseBulkOperation> _Operations = new SynchronizedCollection<BaseBulkOperation>();
19+
[JsonConverter(typeof(StringEnumConverter))]
20+
internal Consistency? _Consistency { get; set; }
21+
22+
internal bool? _Refresh { get; set; }
23+
24+
internal IList<BaseBulkOperation> _Operations = new SynchronizedCollection<BaseBulkOperation>();
1825

1926
public BulkDescriptor Create<T>(Func<BulkCreateDescriptor<T>, BulkCreateDescriptor<T>> bulkCreateSelector) where T : class
2027
{
@@ -49,7 +56,7 @@ public BulkDescriptor Update<T>(Func<BulkUpdateDescriptor<T, T>, BulkUpdateDescr
4956
{
5057
return this.Update<T, T>(bulkUpdateSelector);
5158
}
52-
public BulkDescriptor Update<T, K>(Func<BulkUpdateDescriptor<T, K>, BulkUpdateDescriptor<T, K>> bulkUpdateSelector)
59+
public BulkDescriptor Update<T, K>(Func<BulkUpdateDescriptor<T, K>, BulkUpdateDescriptor<T, K>> bulkUpdateSelector)
5360
where T : class
5461
where K : class
5562
{
@@ -61,6 +68,36 @@ public BulkDescriptor Update<T, K>(Func<BulkUpdateDescriptor<T, K>, BulkUpdateDe
6168
return this;
6269
}
6370

71+
/// <summary>
72+
/// When making bulk calls, you can require a minimum number of active shards in the partition
73+
/// through the consistency parameter. The values allowed are one, quorum, and all. It defaults to the node level
74+
/// setting of action.write_consistency, which in turn defaults to quorum.
75+
/// <pre>
76+
/// For example, in a N shards with 2 replicas index, there will have to be at least 2 active shards within the relevant partition (quorum) for the
77+
/// operation to succeed. In a N shards with 1 replica scenario, there will need to be a single shard active (in this case, one and quorum is the same).
78+
/// </pre>
79+
/// </summary>
80+
/// <param name="consistency"></param>
81+
/// <returns></returns>
82+
public BulkDescriptor Consistency(Consistency consistency)
83+
{
84+
this._Consistency = consistency;
85+
return this;
86+
}
87+
88+
/// <summary>
89+
/// The refresh parameter can be set to true in order to refresh the relevant shards immediately after the bulk operation has occurred and
90+
/// make it searchable, instead of waiting for the normal refresh interval to expire.
91+
/// Setting it to true can trigger additional load, and may slow down indexing.
92+
/// </summary>
93+
/// <param name="refresh"></param>
94+
/// <returns></returns>
95+
public BulkDescriptor Refresh(bool refresh = true)
96+
{
97+
this._Refresh = refresh;
98+
return this;
99+
}
100+
64101
/// <summary>
65102
/// Allows you to perform the multiget on a fixed path.
66103
/// Each operation that doesn't specify an index or type will use this fixed index/type

Diff for: src/Nest/DSL/BulkIndexDescriptor.cs

-16
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,5 @@ public BulkIndexDescriptor<T> Ttl(string ttl)
133133
this._Ttl = ttl;
134134
return this;
135135
}
136-
137-
public BulkIndexDescriptor<T> Consistency(Consistency consistency)
138-
{
139-
this._Consistency = consistency;
140-
return this;
141-
}
142-
143-
public BulkIndexDescriptor<T> Refresh(bool refresh = true)
144-
{
145-
this._Refresh = refresh;
146-
return this;
147-
}
148-
149-
150-
151-
152136
}
153137
}

Diff for: src/Nest/DSL/BulkUpdateDescriptor.cs

-12
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,6 @@ public BulkUpdateDescriptor<T, K> Ttl(string ttl)
201201
return this;
202202
}
203203

204-
public BulkUpdateDescriptor<T, K> Consistency(Consistency consistency)
205-
{
206-
this._Consistency = consistency;
207-
return this;
208-
}
209-
210-
public BulkUpdateDescriptor<T, K> Refresh(bool refresh = true)
211-
{
212-
this._Refresh = refresh;
213-
return this;
214-
}
215-
216204
public BulkUpdateDescriptor<T, K> RetriesOnConflict(int retriesOnConflict)
217205
{
218206
this._RetriesOnConflict = retriesOnConflict;

Diff for: src/Nest/Domain/Bulk/BaseBulkOperation.cs

-5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ public abstract class BaseBulkOperation
4141
[JsonProperty("_ttl")]
4242
internal string _Ttl { get; set; }
4343

44-
[JsonProperty("consistency")]
45-
[JsonConverter(typeof(StringEnumConverter))]
46-
internal Consistency? _Consistency { get; set; }
4744

48-
[JsonProperty("refresh ")]
49-
internal bool? _Refresh { get; set; }
5045

5146
[JsonProperty("_retry_on_conflict")]
5247
internal int? _RetriesOnConflict { get; set; }

Diff for: src/Nest/ElasticClient-Bulk.cs

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Specialized;
23
using System.Text.RegularExpressions;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -66,6 +67,24 @@ private void GenerateBulkPathAndJson(BulkDescriptor bulkDescriptor, out string j
6667
path = bulkDescriptor._FixedType + "/" + path;
6768
path = bulkDescriptor._FixedIndex + "/" + path;
6869
}
70+
var queryString = new NameValueCollection();
71+
if (bulkDescriptor._Refresh.HasValue)
72+
queryString.Add("refresh", bulkDescriptor._Refresh.ToString().ToLowerInvariant());
73+
switch (bulkDescriptor._Consistency)
74+
{
75+
case Consistency.All:
76+
queryString.Add("consistency", "all");
77+
break;
78+
case Consistency.Quorum:
79+
queryString.Add("consistency", "quorem");
80+
break;
81+
case Consistency.One:
82+
queryString.Add("consistency", "one");
83+
break;
84+
}
85+
if (queryString.HasKeys())
86+
path += queryString.ToQueryString();
87+
6988
}
7089
public IBulkResponse Bulk(BulkDescriptor bulkDescriptor)
7190
{

0 commit comments

Comments
 (0)