Module: SearchFlip::Index

Defined in:
lib/search_flip/index.rb

Overview

The SearchFlip::Index mixin makes your class correspond to an ElasticSearch index. Your class can then create or delete the index, modify the mapping, import records, delete records and query the index. This gem uses an individual ElasticSearch index for each index class, because ElasticSearch requires to have the same mapping for the same field name, even if the field is living in different types of the same index.

Examples:

Simple index class

class CommentIndex
  include SearchFlip::Index

  def self.model
    Comment
  end

  def self.type_name
    "comments"
  end

  def self.serialize(comment)
    {
      id: comment.id,
      user_id: comment.user_id,
      message: comment.message,
      created_at: comment.created_at
    }
  end
end

Create/delete the index

CommentIndex.create_index
CommentIndex.delete_index if CommentIndex.index_exists?

Import records

CommentIndex.import(Comment.all)

Query the index

CommentIndex.search("hello world")
CommentIndex.where(user_id: 1)
CommentIndex.range(:created_at, gt: Time.now - 7.days)

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object


45
46
47
# File 'lib/search_flip/index.rb', line 45

def self.included(base)
  base.extend ClassMethods
end