Skip to main content

elasticsearch

@webda/elasticsearch

Elasticsearch indexing service for Webda — automatically mirrors model saves to one or more Elasticsearch indices, with optional date-split partitioning and a search/delete API.

When to use it​

  • You want full-text or aggregation queries on your Webda models without changing your primary store.
  • You need time-series index partitioning (daily/monthly/yearly) for log or metric models.
  • You want to expose a search endpoint directly from Webda without writing custom ES glue code.

Install​

pnpm add @webda/elasticsearch

Configuration​

{
"services": {
"search": {
"type": "ElasticSearchService",
"client": {
"node": "http://localhost:9200"
},
"indexes": {
"posts": {
"model": "MyApp/Post",
"url": "/search/posts"
},
"events": {
"model": "MyApp/Event",
"url": "/search/events",
"dateSplit": {
"frequency": "daily",
"attribute": "createdAt"
}
}
}
}
}
}
ParameterTypeDefaultRequiredDescription
clientobject—YesElasticsearch ClientOptions passed directly to @elastic/elasticsearch Client constructor (e.g. { node, auth })
indexesobject{}YesMap of index name → index configuration
indexes.<n>.modelstring—YesFully qualified model name to index (e.g. "MyApp/Post")
indexes.<n>.urlstring—NoURL prefix to expose search/delete endpoints
indexes.<n>.dateSplit.frequencystring"monthly"NoTime-based index split: yearly, monthly, weekly, daily, or hourly
indexes.<n>.dateSplit.attributestring—NoModel attribute containing the date used for index partitioning

Usage​

import { Service } from "@webda/core";
import { Bean, Inject } from "@webda/core";
import ElasticSearchService from "@webda/elasticsearch";

@Bean
export class PostSearchService extends Service {
@Inject("search")
es: ElasticSearchService;

async findPosts(query: string): Promise<any[]> {
const result = await this.es.search("posts", {
query: { match: { title: query } }
});
return result.hits.hits.map(h => h._source);
}
}

// Documents are indexed automatically when Post models are saved/updated/deleted
// via the Store event listener wired up in ElasticSearchService.resolve()

Reference​

  • API reference: see the auto-generated typedoc at docs/pages/Modules/elasticsearch/.
  • Source: packages/elasticsearch
  • Related: @webda/core for the Store base class and model events; @webda/aws for DynamoStore as a primary store to pair with.

Classes​

Interfaces​