No commit activity in last 3 years
No release in over 3 years
Manages different search engines in a Rails app
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 10.0
~> 3.0

Runtime

>= 1.20.1, ~> 1.20.1
>= 5.0.1, ~> 5.0.1
>= 5.0.1, ~> 5.0.1
>= 2.1.0, ~> 2.1.0
 Project Readme

SwitchSearchable

Manages multiple search engines in your Rails app. Supported are - PGSearch, AlgoliaSearch and Elasticsearch.

Installation

Add this line to your application's Gemfile:

gem 'switch_searchable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install switch_searchable

Usage

Make sure you have setup services with Algolia, Elasticsearch. If you are using PGSearch nothing needs to be setup.

In your ENV - say for example you have Algolia, Elasticsearch and PGSearch setup at the same time:

ALGOLIA_APP_ID="lasdfu0as98fusaoij"
ALGOLIA_API_KEY="doasf0a8uf3098ufwo9fjoiajfaldsj"
ALGOLIA_ENVIRONMENT="staging"
ELASTICSEARCH_HOST="196.223.442.112"
SEARCH_ENGINE = "Postgres"

SEARCH_ENGINE is set to "PGSearch". This means that you have PGSearch activated while the rest are not.

To activate Elasticsearch:

SEARCH_ENGINE = "Elasticsearch"

To activate Algolia:

SEARCH_ENGINE = "Algolia"

In your config/initializers/switch_searchable.rb file:

if ENV["SEARCH_ENGINE"] == "Algolia"

  AlgoliaSearch.configuration = {
    application_id: ENV["ALGOLIA_APP_ID"],
    api_key: ENV["ALGOLIA_API_KEY"],
  }

elsif ENV["SEARCH_ENGINE"] == "Elasticsearch"

  config = {
    host: ENV["ELASTICSEARCH_HOST"],
    transport_options: {
      request: {
        timeout: 5,
      },
    },
  }

  if File.exists?("config/elasticsearch.yml")
    yml = ERB.new(File.new("config/elasticsearch.yml").read)
    config.merge!(YAML.load(yml.result)).deep_symbolize_keys
  end

  Elasticsearch::Model.client = Elasticsearch::Client.new(config)
end

In your AR model:

class Lead < ActiveRecord::Base
  include SwitchSearchable::Searchable

  searchable_attributes(
    :company_name,
    :first_name,
    :last_name,
    :email,
    {phones: [:number]}
  )

  has_may :phones
end

To create an index, run:

Lead.reindex!

After that, you can now search:

Lead.search("Neil the man")

License

The gem is available as open source under the terms of the MIT License.