0

I want to delete the most current item in my Elasticsearch index sorted by myDateField which is a date type. Is that possible? I want something like this query but this would delete all matching items even though I have the size at 1.

{
    "query" : {
        "match_all" : {

        }
    },
    "size" : "1",
    "sort" : [
        {
            "myDateField" : {
                "order" : "desc"
            }
        }
    ]
}

1 Answer 1

2

Delete by query is unlikely to support any sorting features.

If you try Delete by query - however you'll get the error: request does not support [sort]. I couldn't find any documentation saying that the "sort" parameter is not supported in delete by query.

I've one idea to do it but don't know it's the best way or not?

Step 1: Do a normal query based on your conditions+sorting and get those ids.

Step 2: Build a bulk query to delete all documents retrieved above by id those you got on Step 1.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your comment! I was hoping to do this in one step but that makes sense to have to do it in two steps. Would I also add ?refresh=wait_for to wait in step 2 for the document to be deleted before attempting another query?
Yeah, if refresh=wait_for, then wait for a refresh to make this operation visible to search

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.