Project

stretchie

0.0
No commit activity in last 3 years
No release in over 3 years
Comfortable searching pants for ActiveRecord Models. Stretchie simplifies using elastic search in your models and provides hooks to ease testing.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 10.3.2, ~> 10.3
>= 3.0.0, ~> 3.0
>= 0.8.2, ~> 0.8
>= 1.3.9, ~> 1.3

Runtime

>= 0.1.4, ~> 0.1
>= 4.1.1, ~> 4.1
 Project Readme

Stretchie

Build Status Coverage Status

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 linked 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: 'adam@bregenzer.net')
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, 'adam@bregenzer.net'

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