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

Constant Summary collapse

ConnectionMutex =
Mutex.new

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

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