Skip to main content

Self-Hosted typesense Service

· 2 min read
Xiaohai Huang

typesense is a open source Algolia alternative. This blog shows how to run a self-hosted typesense server on my own server.

Setup a typesense Server Using Docker

Run the server with docker-compose up

docker-compose.yml
version: "3.9"
services:
typesense:
image: typesense/typesense:0.22.2
environment:
- TYPESENSE_DATA_DIR=/data
- TYPESENSE_ENABLE_CORS=true
- TYPESENSE_API_KEY=test-key
ports:
- "8108:8108"
volumes:
- /t-data:/data # /t-data is used to store search engine data

see "how to configure the server using environment variables".

Crawl the Site

  1. Create a docsearch-config.json config file which contains the information about the document website.

    1. Download config file.
    2. Update the index_name and domain name.
  2. Create a .env file which contains the information about the typesense server created in the previous step.

.env
TYPESENSE_API_KEY=test-key
TYPESENSE_HOST=xiaohai-huang.net
TYPESENSE_PORT=8108
TYPESENSE_PROTOCOL=http
  1. Run the scraper using Docker.
CONFIG="$(cat docsearch-config.json)" # Retrieved from step 1
docker run -i --rm \
--env-file=$(pwd)/.env \ # .env created at step 2
-e CONFIG="${CONFIG}" \
typesense/docsearch-scraper

Install the package.

yarn add docusaurus-theme-search-typesense@next

Add the following to docusaurus.config.js file:

{
themes: ['docusaurus-theme-search-typesense'],
themeConfig: {
typesense: {
typesenseCollectionName: 'xiaohai-mind-palace-index', // the index_name in `docsearch-config.json`

typesenseServerConfig: {
nodes: [ // typesense server info
{
host: 'xiaohai-huang.net',
port: 8108,
protocol: 'http',
},

],
apiKey: 'test-key', // TYPESENSE_API_KEY specified in docker-compose.yml
},

// Optional: Typesense search parameters: https://typesense.org/docs/0.21.0/api/documents.md#search-parameters
typesenseSearchParameters: {},

// Optional
contextualSearch: true,
},
}
}

References