Hello
Not a question this time, but just a share of code.
But if someone have better, I take
My problem is how delete document with query in Elasticsearch.NET
I read a lot of subjets, but I definitely fail to delete with c# API
So I use a tricky solution
Use c# API to do a search request, use http delete to delete document.
private void CleanAutomatesName()
{
List<string> result = new List<string>();
var query = @"{
""size"" : 1000,
""filter"": {
""term"": {
""automate"":""1""
}
}
}";
var tests = client.LowLevel.Search<Test>("logs-", query);
var str = Encoding.Default.GetString(tests.ResponseBodyInBytes);
dynamic data = JObject.Parse(str);
if (data["hits"].total > 0)
{
var logs = data["hits"]["hits"];
foreach (var log in logs)
{
Console.WriteLine(log._id);
Delete("http://******my_ip:my_port*****/logs-/logs-/" + log._id +"/");
}
}
}
public virtual void Delete(string url)
{
Delete(url, "application/x-www-form-urlencoded");
}
private void Delete(string url, string contentType)
{
var request = WebRequest.Create(url);
request.Method = "DELETE";
request.Timeout = 5000;
request.ContentType = contentType;
try {
var response = request.GetResponse();
response.GetResponseStream();
response.Close();
}
catch (Exception) { }
}
The performance are horrible, but it works
If someone have a good version with API, I am really interested
So far, I try some code like this:
var d1remove = client.DeleteByQuery<Test>("logs-", "logs-", q => q.Index(Indices.Parse("logs-")).Type(Types.Parse("logs-")).Query(rq => rq.Term(f => f._id, (string)log._id)));
or
QueryContainer qcremove = null;
qcremove &= new TermQuery { Field = "_id", Value = log._id };
var deleteRequest = new DeleteByQueryRequest(Indices.Parse("logs-"), Types.Parse("logs-"));
deleteRequest.Query = qcremove;
var d1remove = client.DeleteByQuery(deleteRequest);
But with no result.
Regards