C# How simply delete by id or query with c#


#1

Hello

Not a question this time, but just a share of code.
But if someone have better, I take :slight_smile:

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 :slight_smile:
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 :raised_hands:
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. :disappointed_relieved:

Regards


(Russ Cam) #2

What does your mapping look like for logs- document type in logs- index?


#3

Because I just have logs :slight_smile:
So I have juste one level of document and index . It is the raw data
I create new index after with script


(Russ Cam) #4

what does the mapping looks like i.e. what is returned for

GET /logs-/_mapping/logs-

#5
{
"logs-": {
    "mappings": {
        "logs-": {
            "properties": {
                "@timestamp": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
             },
            "@version": {
                "type": "string"
            },
            "AckServerLoopId": {
                "type": "string"
            },
            "AckServerMic": {
                "type": "string"
            },
            [...] more than dozen of other fields

Why this question? :slight_smile:


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.