Skip to content

Commit cefd6f7

Browse files
committed
Merge pull request #274 from daflookie/master
Add support for percolate during Bulk Index operation
2 parents 5dc49ab + 60c3295 commit cefd6f7

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

src/Nest.Tests.Integration/Core/Bulk/BulkTests.cs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class BulkTests : IntegrationTests
1212
{
1313
[Test]
1414
public void Bulk()
15-
{
16-
//Detete so we know the create does not throw an error.
15+
{
16+
//Delete so we know the create does not throw an error.
1717
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
1818
var result = this._client.Bulk(b => b
1919
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject {Id = 2}))
@@ -24,8 +24,8 @@ public void Bulk()
2424
result.IsValid.Should().BeTrue();
2525

2626
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(3).And.OnlyContain(r => r.OK);
27-
var deleteResponses = result.Items.OfType<BulkDeleteResponseItem>();
28-
var createResponses = result.Items.OfType<BulkCreateResponseItem>();
27+
var deleteResponses = result.Items.OfType<BulkDeleteResponseItem>();
28+
var createResponses = result.Items.OfType<BulkCreateResponseItem>();
2929
var indexResponses = result.Items.OfType<BulkIndexResponseItem>();
3030

3131
deleteResponses.Should().HaveCount(1);
@@ -41,12 +41,12 @@ public void Bulk()
4141
indexResponses.Should().HaveCount(1);
4242
indexResponses.First().Id.Should().BeEquivalentTo("2");
4343
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
44-
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
44+
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
4545
}
4646

4747
[Test]
4848
public void BulkWithFixedIndex()
49-
{
49+
{
5050
var indexName = ElasticsearchConfiguration.NewUniqueIndexName();
5151
var result = this._client.Bulk(b => b
5252
.FixedPath(indexName, "mytype")
@@ -116,7 +116,7 @@ public void BulkWithFixedIndexOveriddenIndividualy()
116116
[Test]
117117
public void BulkAlternativeWayOfWriting()
118118
{
119-
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
119+
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
120120

121121
var descriptor = new BulkDescriptor();
122122
foreach (var i in Enumerable.Range(0, 1000))
@@ -128,7 +128,60 @@ public void BulkAlternativeWayOfWriting()
128128

129129
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(1000).And.OnlyContain(r => r.OK);
130130

131-
}
131+
}
132+
133+
[Test]
134+
public void BulkIndexWithPercolate()
135+
{
136+
// register up some percolator queries to test matching
137+
var query1 = "bulkindex-test-doc-1";
138+
139+
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
140+
141+
var perc = this._client.RegisterPercolator<ElasticSearchProject>(p => p
142+
.Name(query1)
143+
.Query(q => q
144+
.Term(f => f.Country, "netherlands")
145+
)
146+
);
147+
148+
var descriptor = new BulkDescriptor();
149+
150+
// match against any doc
151+
descriptor.Index<ElasticSearchProject>(i => i
152+
.Object(new ElasticSearchProject { Id = 2, Country = "netherlands" })
153+
.Percolate("*") // match on any percolated docs
154+
);
155+
156+
// no percolate requested this time
157+
descriptor.Index<ElasticSearchProject>(i => i
158+
.Object(new ElasticSearchProject { Id = 3, Country = "netherlands" })
159+
);
160+
161+
var result = this._client.Bulk(descriptor);
162+
163+
result.Should().NotBeNull();
164+
result.IsValid.Should().BeTrue();
165+
166+
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(2).And.OnlyContain(r => r.OK);
167+
var indexResponses = result.Items.OfType<BulkIndexResponseItem>();
168+
169+
// tests on percolated responses
170+
indexResponses.Should().HaveCount(2);
171+
172+
indexResponses.First().Id.Should().BeEquivalentTo("2");
173+
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
174+
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
175+
indexResponses.First().Matches.Should().NotBeNull();
176+
177+
indexResponses.ElementAt(1).Id.Should().BeEquivalentTo("3");
178+
indexResponses.ElementAt(1).Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
179+
indexResponses.ElementAt(1).Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
180+
indexResponses.First().Matches.Should().BeNull();
181+
182+
// cleanup
183+
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
184+
}
132185

133186
}
134187
}

src/Nest/Domain/Responses/BulkIndexResponseItem.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Nest.Resolvers.Converters;
1+
using System.Collections.Generic;
2+
using Nest.Resolvers.Converters;
23
using Newtonsoft.Json;
34

45
namespace Nest
@@ -20,5 +21,11 @@ public class BulkIndexResponseItem : BulkOperationResponseItem
2021
public override bool OK { get; internal set; }
2122
[JsonProperty("error")]
2223
public override string Error { get; internal set; }
24+
25+
/// <summary>
26+
/// Null if Percolation was not requested while indexing this doc, otherwise returns the percolator _ids that matched (if any)
27+
/// </summary>
28+
[JsonProperty(PropertyName = "matches")]
29+
public IEnumerable<string> Matches { get; internal set; }
2330
}
2431
}

0 commit comments

Comments
 (0)