Docs
Guides
Setup Elasticsearch

The easiest way to connect to Elasticsearch is to use Elastic cloud. You can sign up for a free trial account and get started right away.

Connecting to Elastic Cloud

To connect to Elastic Cloud, you need to create an account and get your credentials. You can do this by following the instructions on the Elastic Cloud website (opens in a new tab).

Once you have setup a deployment, do the following:

  1. Go to the Elastic Cloud console (opens in a new tab).
  2. Click on the deployment you want to connect to.
  3. Locate the Elasticsearch URL and copy it.
  4. Paste the URL into the connection.host field in the configuration object, within the Node app.
  5. Create an API key with read privileges for the index. Copy the API key and paste it into the connection.apiKey field in the configuration object, within the Node app.
const client = Client({
  connection: {
    host: "ELASTICSEARCH_URL",
    apiKey: "API_KEY"
  }
})

(Required for browser-only Searchkit) Add CORS headers to your Elasticsearch deployment

If you are using Searchkit in the browser and without the node proxy, you will need to add CORS headers to your Elasticsearch instance.

To do this, you need to add the following to your elasticsearch.yml file:

http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept, x-elastic-client-meta

If Searchkit cannot connect to Elasticsearch, it will throw an error. You should be able to see the error in the terminal running the node API.

You can also switch on the debug flag in the configuration object, within the Node app, to see more detailed logs.

Connecting to Elasticsearch locally

If you want to run Elasticsearch locally, you can download and install it from the Elastic website (opens in a new tab).

Once you have installed Elasticsearch, you can start it by running the following command in the terminal:

elasticsearch

You can then connect to Elasticsearch by setting the connection.host field in the configuration object, within the Node app, to http://localhost:9200.

API Key is not required but its important you use one for production.

const client = Client({
  connection: {
    host: "http://localhost:9200"
  }
})

Connecting to Elasticsearch with custom headers

If you are using Elasticsearch with a proxy, you may need to add custom headers to the request. You can do this by setting the connection.headers field in the configuration object, within the Node app.

const client = Client({
  connection: {
    host: "http://localhost:9200",
    headers: {
      "X-My-Header": "My-Value"
    }
  }
})

Connecting to Opensearch

Searchkit does not provide out of the box support for Opensearch. Depending on your use case, you may be able to use the default transport with headers. If you need to use a custom transport, you can do so by implementing the Transporter interface.

 
import type { Transporter, SearchRequest, ElasticsearchResponseBody } from '@searchkit/api'
 
class MyTransporter {
  async msearch(
    requests: SearchRequest[]
  ): Promise<ElasticsearchResponseBody[]> {
    // Implement your own search logic here
    // Fetch is an example of calling an API.
    const username = 'master-user'
    const password = 'master-user-password'
 
    const response = await fetch(`https://domain-endpoint/_msearch`, {
      headers: {
        "content-type": "application/json",
        // Basic auth
        "Authorization": 'Basic ' + Buffer.from(username + ":" + password).toString('base64')
      },
      body: requests
        .reduce<string[]>(
          (sum, request) => [
            ...sum,
            JSON.stringify({ index: request.indexName }),
            "\n",
            JSON.stringify(request.body),
            "\n",
          ],
          []
        )
        .join(""),
      method: "POST",
    });
 
    const responses = await response.json();
    return responses.responses;
  }
}
 
const client = Client({
  connection: new MyTransporter(),
  ...
});