Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch best practices: use directly from the frontend or from the backend

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.

like image 760
Mikias Abebe Avatar asked Jan 25 '23 13:01

Mikias Abebe


1 Answers

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.

like image 57
Ashish Modi Avatar answered Jun 08 '23 03:06

Ashish Modi