module SearchFlip::Index

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.

@example 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

@example Create/delete the index

CommentIndex.create_index
CommentIndex.delete_index if CommentIndex.index_exists?

@example Import records

CommentIndex.import(Comment.all)

@example Query the index

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

Constants

ConnectionMutex

Public Class Methods

included(base) click to toggle source
# File lib/search_flip/index.rb, line 46
def self.included(base)
  base.extend ClassMethods
end