This app demonstrates using the schema-tools gem to migrate an OpenSearch index.
The app itself is a minimal Rails application with PostgreSQL and a Books model that replicates changes to OpenSearch. It also implements a soft-delete capability in OpenSearch.
-
Start PostgreSQL with Docker:
docker compose up -d
-
Install dependencies:
bundle install
Note: This app uses the schema-tools gem from the local
../schema-toolsdirectory for development and testing. -
Create and migrate the database.
rails db:create rails db:migrate
-
Create and migrate the OpenSearch schema.
export OPENSEARCH_URL=http://localhost:9200 rails schema:migrate
Run the demonstration script to see all CRUD operations and OpenSearch schema migration:
./demo_script.rbThe Books model includes:
title(string, required)isbn(string, required, unique)description(text)author(string)pages(integer)created_at(timestamp)updated_at(timestamp)deleted_at(timestamp)
The Books model automatically syncs with OpenSearch when books are created or updated:
Set these environment variables to enable OpenSearch integration:
export OPENSEARCH_URL=http://localhost:9200
export OPENSEARCH_USERNAME=your_username # Optional
export OPENSEARCH_PASSWORD=your_password # Optional- Books are automatically saved to OpenSearch when created (
after_createcallback) - Books are automatically updated in OpenSearch when modified (
after_updatecallback) - Books are automatically deleted from OpenSearch when destroyed (
after_destroycallback) - The OpenSearch document uses the same schema as defined in
schemas/books/mappings.json
- Bulk index all books:
Book.bulk_index_to_opensearch - Delete by query:
Book.delete_by_query({ term: { "author.keyword" => "Author Name" } }) - Check if OpenSearch is configured:
Book.opensearch_client_available?
- Create:
Book.create!(title: "Book Title", isbn: "1234567890") - Read:
Book.find_by(title: "Book Title") - Update:
book.update!(title: "New Title") - Delete:
book.destroy! - List all:
Book.order(:updated_at)