An implementation of Sonar's global search using ElasticSearch. The API service uses the graphql endpoint of a Sonar instance to fetch and sync data with an ElasticSearch instance. The API consists of a simple tRPC router which exposes a search query.
The ultimate goal of this project is to create a global search of Sonar data that can be hosted locally. This enables a faster search experience than utilizing the Sonar provided globalSearch GraphQL query.
This repo currently consists of three packages:
-
@elastic-sonar-search/api
- A NodeJS server that provides a search endpoint for clients.
- Utilizes the
@trpc/serverpackage to create a router which provides thesearchendpoint. - Exports
ApiRoutertype which can be used to create a router client with the@trpc/clientpackage.
-
@elastic-sonar-search/web
- A demo Vue application that allows global search of Sonar entities. Uses the
@trpc/clientpackage to make calls to the API. - Note: It is currently only possible to run the demo web application if you have API access to a Sonar instance.
-
@elastic-sonar-search/tests
- Package containing types which are possibly useful when interacting with the API service. Specifically, the
SearchRoutertype which can be used to create a tRPC client.
A pre-existing ElasticSearch instance must be configured and accessible. The simplest way to create an instance is through docker.
Currently, the recommended way of running the API service is through Docker.
The API service listens for queries on port 3000.
- Create a
.envfile with the following variables:
SONAR_ENDPOINT=https://company.sonar.software/api/graphql
SONAR_TOKEN=a-sonar-api-key
ELASTIC_ENDPOINT=http://localhost:9200
ELASTIC_USERNAME=elastic
ELASTIC_PASSWORD=changeme- Create and run a container
docker run -p 3000:3000 --env-file .env ghcr.io/jackhenry/elastic-sonar-search/api
- Clone the repo
git clone git@github.com:jackhenry/elastic-sonar-search.git
cd elastic-sonar-search/api
-
Create a
.envfile (see Docker instructions) -
Build and run with NodeJS
npm install
npm run codegen
npm run build
export $(cat .env | xargs) && node dist/index.js
The API service consists of a tRPC router that provides a search endpoint. To utilize this endpoint in a project, create a tRPC client with the SearchRouter type provided by @elastic-sonar-search/types.
- Install necessary packages
npm install @trpc/client@next
npm install --save-dev @elastic-sonar-search/types
- Use the
SearchRoutertype with thecreateTRPCClient()function
import type { SearchRouter } from "@elastic-sonar-search/types";
import { createTRPCClient, httpBatchLink } from '@trpc/client';
const searchClient = createTRPCClient<SearchRouter>({
links: [
httpBatchLink({
url: `${ URL to API service }`
})
]
})- Call the
search()function query the ElasticSearch instance
const results = await searchClient.query.search('John Doe');- create an endpoint to manually sync the Elastic instance with Sonar data
- Use Sonar's webhooks to automatically sync Elastic instance when a relevant event occurs
- Allow the ability to override default query used to search the ElasticSearch instance