Skip to content

Commit 1065f32

Browse files
Gabriel MoskoviczMpdreamz
authored andcommitted
Added MultiPrecolate
1 parent e2e4182 commit 1065f32

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/Nest/DSL/MultiPercolateDescriptor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ public MultiPercolateDescriptor Percolate<T>(Func<PercolateDescriptor<T>, Percol
5050
var descriptor = getSelector(new PercolateDescriptor<T>().Index<T>().Type<T>());
5151
Self.Percolations.Add(descriptor);
5252
return this;
53+
}
5354

55+
public MultiPercolateDescriptor PercolateMany<T>(IEnumerable<T> sources, Func<PercolateDescriptor<T>, T, PercolateDescriptor<T>> getSelector)
56+
where T : class
57+
{
58+
foreach (var source in sources)
59+
{
60+
getSelector.ThrowIfNull("getSelector");
61+
var descriptor = getSelector(new PercolateDescriptor<T>().Index<T>().Type<T>(), source);
62+
Self.Percolations.Add(descriptor);
63+
}
64+
return this;
5465
}
5566

5667
public MultiPercolateDescriptor Count<T>(Func<PercolateCountDescriptor<T>, PercolateCountDescriptor<T>> getSelector)
@@ -60,8 +71,8 @@ public MultiPercolateDescriptor Count<T>(Func<PercolateCountDescriptor<T>, Perco
6071
var descriptor = getSelector(new PercolateCountDescriptor<T>().Index<T>().Type<T>());
6172
Self.Percolations.Add(descriptor);
6273
return this;
63-
6474
}
75+
6576
protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo<MultiPercolateRequestParameters> pathInfo)
6677
{
6778
MultiPercolatePathInfo.Update(pathInfo, this);

src/Tests/Nest.Tests.Integration/Core/MultiPercolate/MultiPercolateTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,76 @@ public void MultiPercolate_ReturnsExpectedResults()
103103
errorResponse.ServerError.Error.Should().NotBeNullOrWhiteSpace();
104104

105105
}
106+
107+
108+
[Test]
109+
public void MultiPercolate_PrecolateMany_ReturnsExpectedResults()
110+
{
111+
112+
//lets start fresh using a new index
113+
var indexName = ElasticsearchConfiguration.NewUniqueIndexName();
114+
IntegrationSetup.CreateTestIndex(this.Client, indexName);
115+
116+
// lets register several percolators in our new index that do a term match
117+
// on document name == indexname
118+
// we associate some metadata with the percolator so that we can later filter
119+
// the ones we want to execute easier
120+
foreach (var i in Enumerable.Range(0, 10))
121+
{
122+
var registerPercolator = this.Client.RegisterPercolator(new RegisterPercolatorRequest(indexName, "my-percolator-" + i)
123+
{
124+
Query = new TermQuery
125+
{
126+
Field = Property.Path<ElasticsearchProject>(p => p.Name.Suffix("sort")),
127+
Value = indexName
128+
},
129+
MetaData = new Dictionary<string, object>
130+
{
131+
{ "order", i}
132+
}
133+
});
134+
registerPercolator.IsValid.Should().BeTrue();
135+
}
136+
137+
138+
// Set up 2 projects to index both with indexName as Name
139+
var projects = Enumerable.Range(0, 2)
140+
.Select(i => new ElasticsearchProject { Id = 1337 + i, Name = indexName })
141+
.ToList();
142+
this.Client.IndexMany(projects, indexName);
143+
144+
this.Client.Refresh(r => r.Index(indexName));
145+
146+
147+
//Now we kick of multiple percolations
148+
var multiPercolateResponse = this.Client.MultiPercolate(mp => mp
149+
//provding document in the percolate request
150+
.PercolateMany(projects, (perc, proj) =>
151+
perc.Index(indexName).Document(proj)
152+
)
153+
);
154+
155+
multiPercolateResponse.IsValid.Should().BeTrue();
156+
157+
var percolateResponses = multiPercolateResponse.Responses.ToList();
158+
percolateResponses.Should().NotBeEmpty().And.HaveCount(4);
159+
160+
var percolateResponse = percolateResponses[0];
161+
percolateResponse.Total.Should().Be(10);
162+
percolateResponse.Matches.Should().HaveCount(10);
163+
164+
var existingResponse = percolateResponses[1];
165+
existingResponse.Total.Should().Be(10);
166+
existingResponse.Matches.Should().HaveCount(10);
167+
168+
var countResponse = percolateResponses[2];
169+
countResponse.Total.Should().Be(2);
170+
countResponse.Matches.Should().BeNull();
171+
172+
var errorResponse = percolateResponses[3];
173+
errorResponse.Total.Should().Be(0);
174+
errorResponse.ServerError.Error.Should().NotBeNullOrWhiteSpace();
175+
176+
}
106177
}
107178
}

0 commit comments

Comments
 (0)