I need to store only say 10 number docs under a particular index. And the 11th item should replace the old item i.e 1st item. So that i will be having only 10 doc at any time. I am using elacticsearch in golang
I'm assuming you will have fixed names for documents, like 1, 2,...,10 or whatever. So one approach could be to use Redis to implement a circular list https://redis.io/commands/rpoplpush#pattern-circular-list (you can also implement your own algorithm to implement that circular list by code)
So basically you should follow the next steps:
1
if you get a count < 10 you call insert document query with your data and with the number extracted from the list as the document name. If count = 10 you call update document query on ElasticSearch
The circular will progress in this way:
The initial state of the list is [1, 2, ...10]
. You extract 1
and after extracting it, it goes to the end of the list: [2,3,..., 10,1]
[2, 3, ...10, 1]
. You extract 2
and after extracting it, it goes to the end of the list: [3,4,..., 1,2]
If you want to store only 10 doc then you should apply algo = (document no%10)+1. the return value is your elasticsearch _id field the algo retyrn only 1 to 10. and always index it.