Examples assume the
SearchEngine::Book model from Model configuration.Define model scopes
app/search_engine/book.rb
Compose scopes, joins, filters, and advanced query features.
SearchEngine::Book model from Model configuration.class SearchEngine::Book < SearchEngine::Base
collection :books
scope :active, -> { where(active: true) }
scope :by_store, ->(store_id) { where(store_id: store_id) }
end
SearchEngine::Book
.joins(:publisher)
.active
.where(publisher: { name: "Acme" })
.where(["price >= ?", 100])
.search("running")
.include_fields(:sku, :name, publisher: [:name])
.order(price: :asc)
.page(1)
.per(20)
.to_a
SearchEngine::Book.all.preset(:default_search, mode: :merge)
SearchEngine::Book.all.pin("book_1", "book_2").hide("book_9")
SearchEngine::Book
.group_by(:publisher_id, limit: 1)
.facet_by(:category_id, max_values: 10)
SearchEngine.multi_search(common: { query_by: "name" }) do |m|
m.add :books, SearchEngine::Book.all.per(10)
m.add :publishers, SearchEngine::Publisher.all.per(5)
end
SearchEngine::Book.upsert(record: ::Book.first)
SearchEngine::Book.where(status: "archived").delete_all
Was this page helpful?