meilisearch / meilisearch-rails

Meilisearch integration for Ruby on Rails
https://www.meilisearch.com
MIT License
295 stars 48 forks source link

Index-first search #341

Open ellnix opened 6 months ago

ellnix commented 6 months ago

Description Currently the starting point of any search is a Rails model, which is not conducive to things like shared indexes or multiple indexes.

In the current state of this gem a model may have multiple indexes but only the primary index will be searchable in the way we want it to Model.search ... and there is no convenient way to search others (while receiving ActiveRecord instances).

In addition it's hard to keep track of what models share an index, making it inconvenient (and currently impossible) to rebuild a shared index safely.

I propose we create a new type of resource in a rails app called an index:

Basic example

# app/indexes/animal_index.rb
class AnimalIndex < MeiliSearch::Rails::Base
  searches :cats do
    attributes :name, :meow
  end

  searches :whales, class_name: 'PacificWhale' do
    attribute :label, :last_spotted, :voice
  end

  searches :tigers, if: :captured?
end
# app/indexes/location_index.rb
class LocationIndex < MeiliSearch::Rails::Base
  searches :restaurants do
    attributes :name, :cousine, '_geo'
  end

  searches :public_restrooms do
    attribute '_geo'
  end

  searches :landmarks do
    attributes :name, :type, '_geo'
  end
end

LocationIndex.search('', filter: { '_geoRadius(1, 2, 3)' })

P. S.: I am not suggesting removing the current meilisearch block syntax, just adding another way to search.

botbotbotbotboot commented 6 months ago

+1