Skip to content

Add support for percolate during Bulk Index operation #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions src/Nest.Tests.Integration/Core/Bulk/BulkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class BulkTests : IntegrationTests
{
[Test]
public void Bulk()
{
//Detete so we know the create does not throw an error.
{
//Delete so we know the create does not throw an error.
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
var result = this._client.Bulk(b => b
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject {Id = 2}))
Expand All @@ -24,8 +24,8 @@ public void Bulk()
result.IsValid.Should().BeTrue();

result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(3).And.OnlyContain(r => r.OK);
var deleteResponses = result.Items.OfType<BulkDeleteResponseItem>();
var createResponses = result.Items.OfType<BulkCreateResponseItem>();
var deleteResponses = result.Items.OfType<BulkDeleteResponseItem>();
var createResponses = result.Items.OfType<BulkCreateResponseItem>();
var indexResponses = result.Items.OfType<BulkIndexResponseItem>();

deleteResponses.Should().HaveCount(1);
Expand All @@ -41,12 +41,12 @@ public void Bulk()
indexResponses.Should().HaveCount(1);
indexResponses.First().Id.Should().BeEquivalentTo("2");
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
}

[Test]
public void BulkWithFixedIndex()
{
{
var indexName = ElasticsearchConfiguration.NewUniqueIndexName();
var result = this._client.Bulk(b => b
.FixedPath(indexName, "mytype")
Expand Down Expand Up @@ -116,7 +116,7 @@ public void BulkWithFixedIndexOveriddenIndividualy()
[Test]
public void BulkAlternativeWayOfWriting()
{
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);

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

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

}
}

[Test]
public void BulkIndexWithPercolate()
{
// register up some percolator queries to test matching
var query1 = "bulkindex-test-doc-1";

this._client.UnregisterPercolator<ElasticSearchProject>(query1);

var perc = this._client.RegisterPercolator<ElasticSearchProject>(p => p
.Name(query1)
.Query(q => q
.Term(f => f.Country, "netherlands")
)
);

var descriptor = new BulkDescriptor();

// match against any doc
descriptor.Index<ElasticSearchProject>(i => i
.Object(new ElasticSearchProject { Id = 2, Country = "netherlands" })
.Percolate("*") // match on any percolated docs
);

// no percolate requested this time
descriptor.Index<ElasticSearchProject>(i => i
.Object(new ElasticSearchProject { Id = 3, Country = "netherlands" })
);

var result = this._client.Bulk(descriptor);

result.Should().NotBeNull();
result.IsValid.Should().BeTrue();

result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(2).And.OnlyContain(r => r.OK);
var indexResponses = result.Items.OfType<BulkIndexResponseItem>();

// tests on percolated responses
indexResponses.Should().HaveCount(2);

indexResponses.First().Id.Should().BeEquivalentTo("2");
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
indexResponses.First().Matches.Should().NotBeNull();

indexResponses.ElementAt(1).Id.Should().BeEquivalentTo("3");
indexResponses.ElementAt(1).Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
indexResponses.ElementAt(1).Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
indexResponses.First().Matches.Should().BeNull();

// cleanup
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
}

}
}
9 changes: 8 additions & 1 deletion src/Nest/Domain/Responses/BulkIndexResponseItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nest.Resolvers.Converters;
using System.Collections.Generic;
using Nest.Resolvers.Converters;
using Newtonsoft.Json;

namespace Nest
Expand All @@ -20,5 +21,11 @@ public class BulkIndexResponseItem : BulkOperationResponseItem
public override bool OK { get; internal set; }
[JsonProperty("error")]
public override string Error { get; internal set; }

/// <summary>
/// Null if Percolation was not requested while indexing this doc, otherwise returns the percolator _ids that matched (if any)
/// </summary>
[JsonProperty(PropertyName = "matches")]
public IEnumerable<string> Matches { get; internal set; }
}
}