Stretchie

Comfortable searching pants for ActiveRecord Models. Stretchie simplifies using elastic search in your models and provides hooks to ease testing.

Defining Indices

Stretchie simply builds on elasticsearch-model, allowing you to pull the details out into a concern.

With a model like this:

class User < ActiveRecord::Base
  attr_accessor :name, :email, :misc_id
end

Your concern could look like this:

module Search
  module User
    extend ActiveSupport::Concern
    include Stretchie::Pants

    included do
      settings index: { number_of_shards: 1 } do
        mappings dynamic: 'false' do
          indexes :name, analyzer: 'whitespace', index_options: 'offsets'
          indexes :email
          indexes :misc_id
          indexes :sortable_name
        end
      end
    end

    def as_indexed_json(options={})
      json = as_json(only: [:name, :email, :misc_id])
      json['sortable_name'] = self.name.downcase
      json
    end
  end
end

And you would add this to your model:

include Search::User

If you have liked models, you can have them re-index automatically with index_dependent_models:

class User < ActiveRecord::Base
  include Search::User
  attr_accessor :name, :email
  has_may :tags
end

class Tag < ActiveRecord::Base
  include Search::Tag
  attr_accessor :name

  belongs_to :user
end

module Search
  module Tag
    extend ActiveSupport::Concern
    include Stretchie::Pants

    included do
      settings index: { number_of_shards: 1 } do
        mappings dynamic: 'false' do
          indexes :name, analyzer: 'whitespace', index_options: 'offsets'
          indexes :sortable_name
        end
      end
    end

    def as_indexed_json(options={})
      json = as_json(only: [:name])
      json['sortable_name'] = self.name.downcase
      json
    end

    def index_dependent_models
        self.users
    end
  end
end

Maintaining Indices

Create / Update your indices:

Stretchie.update_indices
Stretchie.update_indices :users

Delete your indices:

Stretchie.delete_indices
Stretchie.delete_indices :users

Refresh your indices:

Stretchie.refresh_indices
Stretchie.refresh_indices :users

Maintaining Documents in the Index

To add or update changes:

user = User.create(name: 'Adam Bregenzer', email: '[email protected]')
user.update_in_index

To remove:

user.delete_from_index

Searching

You can do a simple search:

User.search 'Adam'
User.search 'Adam', limit: 10, skip: 20, order: {'name' => 'asc'}

You can scope searches:

Tag.search 'rails', terms: {'user_id': current_user.id}

You can search specific fields:

User.field_search :email, '[email protected]'

You can search however you want:

User.query_search {'match' => {'name' => 'Foo'}}

Talk to Me!

Let me know what you think, if you use it, etc.

Installation

Add this line to your application's Gemfile:

gem 'stretchie'

And then execute:

$ bundle

Or install it yourself as:

$ gem install stretchie

Contributing

  1. Fork it ( https://github.com/adambregenzer/stretchie/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