-1

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

kish
  • 545
  • 2
  • 5
  • 11

2 Answers2

1

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.

Rajnish
  • 167
  • 1
  • 5
-1

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:

  • You load those 10 values ordered in the circular list, let's say 1,2, 3, ... 10

  • When you want to store a document in Redis, you extract an element from the list, for our list that element will be 1

  • make a query count on your ElasticSearch index to get the number of the document in the index

  • 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]

  • Second extraction from the list, current state of the list: [2, 3, ...10, 1]. You extract 2 and after extracting it, it goes to the end of the list: [3,4,..., 1,2]

  • and so on

Community
  • 1
  • 1
Averias
  • 931
  • 1
  • 11
  • 20
  • Adding Redis just for this seems totally unnecessary. That's a lot of development and operational overhead for what is very simple to implement with no new dependencies. – Adrian Mar 19 '18 at 19:27