Related: Models, Schema, Configuration
Use this page when you are defining a new SearchEngine model or expanding an existing one.
Define the collection and fields
app/search_engine/book.rb
class SearchEngine::Book < SearchEngine::Base
collection :books
attribute :sku, :string
attribute :name, :string, sort: true
attribute :description, :string
attribute :publisher_id, :string
attribute :price, :float
attribute :tags, [:string], empty_filtering: true
attribute :store, :object, nested: { name: :string, city: :string }
end
Do not declare attribute :id. Use identify_by to control the Typesense document id.
Set identity and defaults
app/search_engine/book.rb
class SearchEngine::Book < SearchEngine::Base
# ... attributes ...
identify_by :sku
query_by :name, :description
schema_retention keep_last: 2
end
identify_by controls the Typesense document id.
query_by sets a per-model default for searches.
schema_retention keeps old physical collections after swaps.
Nested fields
Use :object or [:object] with nested: to define subfields.
app/search_engine/book.rb
class SearchEngine::Book < SearchEngine::Base
attribute :retail_prices, [:object], nested: {
current_price: :float,
price_type: :string
}
end
Next steps
- Validate field types and options in Schema.
- Use Indexer to map and import data.