Skip to content

Commit 3312cc8

Browse files
committed
fix #651 create a better Delete extension method if you want to delete an document without specifying a CLR type for the operation
1 parent d90efa5 commit 3312cc8

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Nest/ConvenienceExtensions/DeleteExtensions.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ namespace Nest
99
/// </summary>
1010
public static class DeleteExtensions
1111
{
12+
13+
/// <summary>
14+
///The delete API allows to delete a typed JSON document from a specific index based on its id.
15+
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
16+
/// </summary>
17+
/// <typeparam name="T">The type used to infer the default index and typename</typeparam>
18+
/// <param name="client"></param>
19+
/// <param name="index">The name of the index as string</param>
20+
/// <param name="type">The type name of the document you wish to delete</param>
21+
/// <param name="id">The id as string of the document you want to delete</param>
22+
/// <param name="selector">An optional descriptor to further describe the delete operation</param>
23+
public static IDeleteResponse Delete(this IElasticClient client, string index, string type, string id,
24+
Func<DeleteDescriptor<object>, DeleteDescriptor<object>> selector = null)
25+
{
26+
selector = selector ?? (s => s);
27+
return client.Delete<object>(s => selector(s.Index(index).Type(type).Id(id)));
28+
}
29+
1230
/// <summary>
1331
///The delete API allows to delete a typed JSON document from a specific index based on its id.
1432
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
@@ -51,6 +69,23 @@ public static Task<IDeleteResponse> DeleteAsync<T>(this IElasticClient client, l
5169
return client.DeleteAsync<T>(s => selector(s.Id(id)));
5270
}
5371

72+
/// <summary>
73+
///The delete API allows to delete a typed JSON document from a specific index based on its id.
74+
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html
75+
/// </summary>
76+
/// <typeparam name="T">The type used to infer the default index and typename</typeparam>
77+
/// <param name="client"></param>
78+
/// <param name="index">The name of the index as string</param>
79+
/// <param name="type">The type name of the document you wish to delete</param>
80+
/// <param name="id">The id as string of the document you want to delete</param>
81+
/// <param name="selector">An optional descriptor to further describe the delete operation</param>
82+
public static Task<IDeleteResponse> DeleteAsync(this IElasticClient client,
83+
string index, string type, string id,
84+
Func<DeleteDescriptor<object>, DeleteDescriptor<object>> selector = null)
85+
{
86+
selector = selector ?? (s => s);
87+
return client.DeleteAsync<object>(s => selector(s.Index(index).Type(type).Id(id)));
88+
}
5489
/// <summary>
5590
///The delete API allows to delete a typed JSON document from a specific index based on its id.
5691
/// <para> </para>>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html

src/Tests/Nest.Tests.Integration/Core/DeleteTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ protected override void ResetIndexes()
1717
IntegrationSetup.TearDown();
1818
IntegrationSetup.Setup();
1919
}
20-
20+
21+
[Test]
22+
public void ShouldNotThrowOnIdOverload()
23+
{
24+
Assert.Throws<DispatchException>(() =>
25+
{
26+
this._client.Delete("id", d=>d.Index("someindex").Type("sometype"));
27+
});
28+
}
2129

2230
[Test]
2331
public void ShouldThowOnNullId()

0 commit comments

Comments
 (0)