danielberkompas / elasticsearch-elixir

No-nonsense Elasticsearch library for Elixir
MIT License
422 stars 73 forks source link

Reloading Indexes on New Record Insertion in DB #90

Closed blackode closed 3 years ago

blackode commented 3 years ago

Here, I'm building the indexes when the application starts using mix task

How can we reload the index as soon as a new record inserted into DB? Do we have to stop application and rebuild the indexes here?

Glad if we have any automation here, unfortunately I did not find any other than tasks.

Thanks :)

danielberkompas commented 3 years ago

This package doesn't include any automatic way to do this. You would need to set up your code such that whenever you insert a record into the database, you make a MyCluster.post request to also insert that record into your index. You could use Ecto.Multi to do this.

blackode commented 3 years ago

@danielberkompas Thanks for the reply. I saw a function named hot_swap/2 in module Elasticsearch.Index

So I thought to use in the following way...

Elasticsearch.Index.hot_swap(MyCluster, "posts")
# for whenever a post is added similaryly for the rest.

I still doubt using this way as for every new post it will create new index with prefix time.

Does this approach will have the impact in the code performance? If not we can use this instead of using Elasticsearch.post right?

FYI, we are using scylladb in our project.

Glad if you could confirm the approach.

Thank You :)

danielberkompas commented 3 years ago

I don't think you should use hot_swap for this. It's not necessary to create a new index every time you create a post. Use Elasticsearch.put_document instead: https://hexdocs.pm/elasticsearch/1.0.0/Elasticsearch.html#put_document/3

blackode commented 3 years ago

Thank you :) @danielberkompas