Giter Club home page Giter Club logo

activesearch's Introduction

ActiveSearch

This gem allows any class to be indexed by the chosen fulltext search engine.

Installation

Depending on the chosen engine, you need to require a dependency and then activesearch on your Gemfile:

Mongoid 2.x users

If you are using mongoid 2.x, you should use gem 'activesearch', '~> 0.0.16'. What lies below is for mongoid 3.x.

###Mongoid

gem 'mongoid'
gem 'activesearch'

This is not a fulltext search engine, but it's a good solution for those users that don't have access to anything else. It works by storing keywords taken from the specified fields and storing them in an Array field, which would be indexed.

###elasticsearch

gem 'tire'
gem 'activesearch'

Important: the elasticsearch engine has not not been updated so it does not work.

###Algolia

gem 'httparty'
gem 'activesearch'

Then you need to configure the client. In rails, put this in an algolia.rb initializer:

Algolia.configure do |c|
  c.api_key = "1234567890"
  c.app_id = "someapp"
  c.host = "https://apieu1.algolia.com"
end

##Configuration

call "search_by" from your model:

search_by [:title, :body, store: [:slug]], if: :its_friday

IMPORTANT: the first parameter must be either an array, or a symbol. The second parameter must be a conditions hash.

the :store option allows you to store that value in the index but it won't be used for search. You can also add :if or :unless conditions in the same way you would do with ActiveModel callbacks. If you need virtual options, pass a symbol instead:

search_by :options_for_search

And define an instance method with that name which must return an array with the options, ie:

def options_for_search
  [:field, :another_field]
end

Querying

ActiveSearch.search("some words").first.to_hash["title"]

You can access the stored fields with to_hash, so you don't need to fetch the real document.

Why?

You might wonder why you would like to use ActiveSearch instead of a specific option for your fulltext search index. ActiveSearch provides an uniform API, which is in itself a drop-in replacement in case you want to move from an engine to another.

Testing

Run specs with this command:

bundle exec parallel_rspec spec/

Since different engines define their own version of ActiveSearch, running specs on a single process will break.

Contributing

  1. Fork it
  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 new Pull Request

activesearch's People

Contributors

did avatar papipo avatar youroff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.