$ show toptal/chewy
Chewy is a Ruby ODM (Object Document Mapper) for Elasticsearch, providing a comprehensive DSL for defining indexes, mappings, and search queries. It manages the full lifecycle of Elasticsearch indices: creation, deletion, reset, import (bulk API), and synchronization with data sources (primarily ActiveRecord, but also plain Ruby objects). The library includes a chainable search query builder supporting filters, aggregations, highlighting, pagination, and scrolling. It offers a pluggable strategy system for controlling when and how index updates are appliedβranging from immediate (urgent) to deferred (atomic, Sidekiq, ActiveJob)βwith support for nesting strategies. Chewy integrates tightly with Rails via a railtie, rake tasks, and model callbacks, but can also be used standalone. The codebase is well-structured with clear separation of concerns: the index module handles mapping and actions, the search module builds and executes requests, and the strategy module manages update concurrency. Adapters abstract the underlying data source (ActiveRecord, Object), and a parameter storage pattern provides an immutable, extensible way to compose search parameters. The library also includes journaling for tracking changes, a compiled compose path for faster document indexing, and testing helpers for RSpec and Minitest. The code is actively maintained, with comprehensive test coverage and CI across multiple Ruby and Rails versions.
The LLM identified 5 files worth highlighting for reuse:
Base class for all search parameter storage objects. Implements an immutable value pattern: each parameter is a wrapper around a value, with a `modify!` method that returns a new instance with the value updated via a block. Subclasses define how values are merged (e.g., BoolStorage for boolean queries, HashStorage for aggregations). This decouples parameter rendering from the search request and allows easy extension. The pattern is reusable in any Ruby project needing immutable, composable options.
Abstract base class for all update strategies. Defines the interface (leave, join, wrap) and a strategy stack that can be pushed and popped at runtime. Concrete strategies (Atomic, Urgent, Bypass, Sidekiq, etc.) implement `leave` to commit index updates and `join` to wait for completion. The stack supports nesting (e.g., atomic inside urgent). This pattern is a clean implementation of the Strategy pattern and is reusable for any system requiring configurable, nestable behavior around a core operation.
Abstract base class for data source adapters. Defines a common interface: `import`, `import_fields`, `identify`, `load`, `pluck`, etc. Subclasses (ActiveRecord, Object) implement these methods. The adapter is selected by `accepts?` class method, allowing Chewy to handle different data sources transparently. The design is clean and extensible, reusable for any ODM that needs to abstract over multiple ORMs or data sources.
Builds bulk request bodies for Elasticsearch import. Handles join field caching, parent-change detection, and descendant reindexing. It constructs the correct `index`, `delete`, or `update` actions based on document state. This component is isolated and reusable for any system that needs to generate Elasticsearch bulk requests with parent-child relationships.
Provides a DSL for composing boolean queries (must, should, must_not, filter) using method chaining. It wraps Elasticsearch's bool query and supports merging multiple conditions. The implementation is self-contained with clear value semantics (immutable, returns new proxy). This is a reusable pattern for building complex boolean queries in any Ruby search application.