TkhSearch

This is a simple Rails gem full text search engine. It uses the local database for reverse indexing and to log search queries. Multi-model searches are supported.

Installation

Add this line to your application's Gemfile:

gem 'tkh_search'

And then execute:

$ bundle

Install initializer and migrations:

$ rake tkh_search:install

Or, upon updating the gem:

$ rake tkh_search:update

Run the migrations:

$ rake db:migrate

Indexing the Models

In any model you want to index and search, copy/paste and customize the following code:

tkh_searchable # for after_save re-indexing hooks
def self.tkh_search_indexable_fields
  # The key is the attribute name, the value is the desired strength.
  # By the way, your model needs a title or a name attribute. Real or virtual.
  # Necessary to display result links
  indexable_fields = {
    title: 8,
    description: 3,
    body: 2
  }
end

Optionally the model may have a 'published?' attribute, virtual or not, to later refine admin and public searches.

To index a model's records, use the 'reverse_indexing' class method. Example:

ModelName.reverse_indexing

Having configured all the models you wish to index in your search engine, you can index them all at once by first setting up the initializer.

TkhSearch::TkhSearchable.indexable_models = %w( Page Post Event WhateverYouWant )

and secondly, by pointing your browser to: /index_all_models

What about new and updated records?

Any single record will be indexed upon saving.

Searching your site

Simple Setup - Searching one model in any given page

Render the basic search form:

<%= render 'search/search_form', models_to_search: 'Page' %>

By default the search is ajaxified. Just below the form, place a div to host the results:

<div class="js-search-results"></div>

Searching multiple models

Just send the model names to the partial

<%= render 'search/search_form', models_to_search: 'Page Post Event' %>

Search form in Bootstrap navbar

There is a form partial to that effect.

<%= render 'search/search_form_for_navbar', models_to_search: 'Page' %>

Multiple search forms in a page

In this case you have to tell the gem in which div to render the results.

<%= render 'search/search_form', models_to_search: 'Page', results_css_class: 'blog-js-search-results' %>

And of course, you need to place a div with the same class name under your form.

Customize the submit button value

This can also be used for the sake of localization

<%= render 'search/search_form', models_to_search: 'Event', submit_label: 'find an awesome event' %>

Search Stats

Administrators can see the indexing and searching stats at

 /search_stats

Missing Features

This gem is brand new and will grow and mature.

Issues and pull requests welcome on Github.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/tkh_search/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request