Type-safe Elasticsearch queries in TypeScript (and JavaScript) with elasticlink
Elasticsearch queries can silently fail, returning empty results due to mismatches between query types and field mappings or simple typos. For instance, a match query on an un-analyzed keyword field or a typo in a field name like catgory instead of category will pass validation but yield no hits. This occurs because Elasticsearch's DSL is an untyped JSON blob, offering no compile-time checks for field types or query validity.Elasticlink addresses these issues by providing a type-safe, mapping-aware query builder for Elasticsearch. Users define their index mapping once, and elasticlink enforces type constraints on query methods accordingly. For example, match() is restricted to text fields, and term() to exact-value fields, with field names conveniently autocompleting.This approach ensures that potential errors, like using match() on a keyword field or typos, are flagged as red squiggles in the editor rather than becoming runtime production errors. Elasticlink is TypeScript-first but also compatible with plain JavaScript, supporting both ESM and CommonJS. It functions as a builder, generating plain Elasticsearch DSL via a .build() method without runtime overhead, making it suitable for direct use with the official @elastic/elasticsearch client.The tool validates field references against the defined mapping, including within aggregations, offering robust safety for complex queries. Additionally, elasticlink can infer TypeScript types directly from the mapping, eliminating the need for a separate source of truth. For JavaScript users, it provides IDE autocompletion and type constraints through special comments.Elasticlink remains correct across Elasticsearch versions by deferring to the official client's types for options, ensuring compatibility and feature availability. It also offers additional features like type-safe kNN search, conditional query building with .when(), and presets for index management. This comprehensive set of tools aims to prevent silent failures and improve the developer experience when working with Elasticsearch.
matchquery on an un-analyzedkeywordfield or a typo in a field name likecatgoryinstead ofcategorywill pass validation but yield no hits. This occurs because Elasticsearch's DSL is an untyped JSON blob, offering no compile-time checks for field types or query validity.Elasticlink addresses these issues by providing a type-safe, mapping-aware query builder for Elasticsearch. Users define their index mapping once, and elasticlink enforces type constraints on query methods accordingly. For example,match()is restricted to text fields, andterm()to exact-value fields, with field names conveniently autocompleting.This approach ensures that potential errors, like usingmatch()on akeywordfield or typos, are flagged as red squiggles in the editor rather than becoming runtime production errors. Elasticlink is TypeScript-first but also compatible with plain JavaScript, supporting both ESM and CommonJS. It functions as a builder, generating plain Elasticsearch DSL via a.build()method without runtime overhead, making it suitable for direct use with the official@elastic/elasticsearchclient.The tool validates field references against the defined mapping, including within aggregations, offering robust safety for complex queries. Additionally, elasticlink can infer TypeScript types directly from the mapping, eliminating the need for a separate source of truth. For JavaScript users, it provides IDE autocompletion and type constraints through special comments.Elasticlink remains correct across Elasticsearch versions by deferring to the official client's types for options, ensuring compatibility and feature availability. It also offers additional features like type-safe kNN search, conditional query building with.when(), and presets for index management. This comprehensive set of tools aims to prevent silent failures and improve the developer experience when working with Elasticsearch.