|
| 1 | +# Elasticsearch proxy example |
| 2 | + |
| 3 | +This folder contains an example of how to build a lightweight proxy |
| 4 | +between your frontend code and Elasticsearch if you don't |
| 5 | +have a more sophisticated backend in place yet. |
| 6 | + |
| 7 | +> **IMPORTANT:** This is not a production ready code and it is only for demonstration purposes, |
| 8 | +> we make no guarantees on it's security and stability. |
| 9 | +
|
| 10 | +This project is designed to be deployed on [Vercel](https://vercel.com/), a cloud platform |
| 11 | +for static sites and Serverless Functions. You can use other functions providers, |
| 12 | +such as [Google Cloud functions](https://cloud.google.com/functions). |
| 13 | + |
| 14 | +## Project structure |
| 15 | + |
| 16 | +The project comes with four endpoints: |
| 17 | + |
| 18 | +- `/api/search`: runs a search, requires `'read'` permission |
| 19 | +- `/api/autocomplete`: runs an autocomplete suggestion, requires `'read'` permission |
| 20 | +- `/api/index`: indexes or updates a document, requires `'write'` permission |
| 21 | +- `/api/delete`: deletes a document, requires `'write'` permission |
| 22 | + |
| 23 | +Inside `utils/authorize.js` you can find the authorization logic for the endpoints. |
| 24 | +In each endpoint you should configure the `INDEX` variable. |
| 25 | + |
| 26 | +## How to use |
| 27 | + |
| 28 | +Create an account on Vercel, then create a deployment on Elastic Cloud. If you |
| 29 | +don't have an account on Elastic Cloud, you can create one with a free 14-day trial |
| 30 | +of the [Elasticsearch Service](https://www.elastic.co/elasticsearch/service). |
| 31 | + |
| 32 | +### Configure Elasticsearch |
| 33 | + |
| 34 | +Once you have created a deployment on Elastic Cloud copy the generated Cloud Id and the credentials. |
| 35 | +Then open `utils/prepare-elasticsearch.js` and fill your credentials. The script generates |
| 36 | +an [Api Key](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html) |
| 37 | +that you can use for authenticating your request. Based on the configuration of the Api Key, you will be able |
| 38 | +to perform different operation on the specified indices or index pattern. |
| 39 | + |
| 40 | +### Configure Vercel |
| 41 | + |
| 42 | +Install the [Vercel CLI](https://vercel.com/docs/cli) to bootstrap the project, |
| 43 | +or read the [quickstart](https://vercel.com/docs) documentation. |
| 44 | + |
| 45 | +If you are using the CLI, bootstrap the project by running `vercel`. Test the project locally |
| 46 | +with `vercel dev`, and deploy it with `vercel deploy`. |
| 47 | +Configure the `ELASTIC_CLOUD_ID` [environment varible](https://vercel.com/docs/environment-variables) as well. |
| 48 | +The Api Key is passed from the frontend app via a `Authorization` header as `Bearer` token and is |
| 49 | +used to authorize the API calls to the endpoints as well. |
| 50 | +Additional configuration, such as CORS, can be added to [`vercel.json`](https://vercel.com/docs/configuration). |
| 51 | + |
| 52 | +## Authentication |
| 53 | + |
| 54 | +If you are using Elasticsearch only for search purposes, such as a search box, you can create |
| 55 | +an Api Key with `read` permissions and store it in your frontend app. Then you can send it |
| 56 | +via `Authorization` header to the proxy and run your searches. |
| 57 | + |
| 58 | +If you need to ingest data as well, it's more secure to have a strong authentication in your application. |
| 59 | +For such cases, use an external authentication service, such as [Auth0](https://auth0.com/) |
| 60 | +or [Magic Link](https://magic.link/). Then create a different Api Key with `read` and `write` |
| 61 | +permissions for authenticated users, that will not be stored in the frontend app. |
| 62 | + |
| 63 | +## License |
| 64 | + |
| 65 | +This software is licensed under the [Apache 2 license](../../LICENSE). |
0 commit comments