I was building a web app that has a frontend with React and a graphql backend service built with NodeJS. one feature is a search and I have been exploring Elasticsearch. I've noticed there are libraries like reactive-search for React that let you connect to an Elasticsearch endpoint and also provide you UI components that generate the query themselves which makes it extremely easy to set up for basic search but complex for custom queries. So, I've decided to do the search from the backend and return the data via graphql. The concern I have is which method of connecting is better and why.
NB: Also include any best practices about Elasticsearch. I am using a managed Elasticsearch cluster from AWS.
The most important thing to keep in mind while using elasticsearch for powering search functionality is to have capability to switch indexes without having a down time. Elasticsearch Aliases comes to rescue for this. You can read more here - https://www.elastic.co/guide/en/elasticsearch/reference/6.2/indices-aliases.html
In nutshell it sits in front of your indexes so you never interacte directly with indexes from frontend. It should always go via aliases. In case you need to reindex the data in a new index, you could just switch the aliases. More like a blue green deployment.
Regarding using directly or via backend depends on the use cases. For example. If you are using AWS elasticsearch and you need some kind of authentication, you should let the backend talking to elasticsearch (backend using IAM roles to generate the AWS Signature). If you do it via frontend, you might have to expose your secrets which is obviously not a good strategy.
Another reason why you should consider backend is that it gives you flexibility to handle breaking changes more efficiently. For example in case you are changing the structure, you could just modify the backend to return the structure which your frontend is expecting.
I have used AWS Elasticsearch extensively. Let me know if you are looking for anything specifically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With