0

I am new to Elasticsearch and trying to figure out a way to delete a document that contains a field with timestamp as its value.

I have a document with a field called lastmodifiedtime. This field is of type long and is being used to store the timestamp (i.e number of milliseconds since EPOCH. ex, 1486709502).

Now I have a usecase where I need to delete all the documents that are having lastmodifiedtime older than one day. So I find out the timstamp value corresponding to the one day older time and then try to delete the documents.

I tried REST call as below

DELETE http://localhost:9200/testindex/testtype/_query
"query": {
    "term" : {
        "lastmodifiedtime" : {
        "lte": 1486709504
        }
    }
 }

But getting the following response:

{
    "found": false,
    "_index": "testindex",
    "_type": "testtype",
    "_id": "_query",
    "_version": 4,
    "result": "not_found",
    "_shards":
    {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

Could you please help me how to deal with this scenario?

6
  • 1
    This just means that your query is not returning any matches. Are you sure documents exist (just run a query for documents lastmodifiedtime lte 1486709504 and check if it returns results. Commented Apr 6, 2017 at 10:09
  • If you are using >= 5.2 of elasticsearch try this: elastic.co/guide/en/elasticsearch/reference/current/… Commented Apr 6, 2017 at 10:21
  • 1486709504 is the number of seconds, 1486709504000 is the number of milliseconds. Try it out. Commented Apr 6, 2017 at 10:25
  • @rad11: This is elasticsearch 5.2.2 and I did try delete by query api too. But no luck. Commented Apr 6, 2017 at 11:21
  • @Val : My bad. The value stored in document is 1486709504 and I am trying to query using similar value. Commented Apr 6, 2017 at 11:23

1 Answer 1

3

you can use delete_by query api

POST http://localhost:9200/testindex/testtype/_delete_by_query
{
  "query": {
    "range": {
      "lastmodifiedtime": {
        "lte": 1486709504
      }
    }
  }
}

Thanks

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

4 Comments

I tried this but no luck :(. Getting the following error: { "error": { "root_cause": [ { "type": "parse_exception", "reason": "Failed to derive xcontent" } ], "type": "parse_exception", "reason": "Failed to derive xcontent" }, "status": 400 }
ahh,,, are you trying to do a range query?
i have edited my answer, this should work now for you
glad this worked for you, can you please accept this answer as correct

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.