Search code examples
node.jssearchelasticsearchiterationpruning

How to iterate through every object in an ElasticSearch index?


This should be easy.

I've inserted a number of records into ElasticSearch and have been scouring their documentation but I can't seem to find a way to simply take an index and iterate through it in Node.js

I don't have much experience with Elastic, so it's been frustrating.

Some of the items in that list need to be pruned more or less on a 10 minute basis, otherwise the DB just grows and grows. I'd like a separate task to do just that.


Solution

  • You can achieve what you want by using a range query in your delete by query call:

    POST your_index/_delete_by_query
    {
      "query": {
        "range" : {
            "date_field" : {
               "lt" : "2017-05-08T00:00:00.000Z"
            }
        }
      }
    }