Created
November 26, 2011 19:49
-
-
Save acorcutt/1396220 to your computer and use it in GitHub Desktop.
Indextank Indexer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Indexer | |
# Add indexed fields to model | |
# TODO - make this work in embedded e.g. self.index_embedded(*names) | |
module Indexes | |
extend ActiveSupport::Concern | |
included do | |
class_attribute :_index_fields | |
class_attribute :_index_texts | |
class_attribute :_index_categories | |
class_attribute :_index_variables | |
# Set items to index as global text search | |
# index_text :content,:title,"user.address" | |
def self.index_text(*names) | |
self._index_texts = ((self._index_texts ||= []) + names).uniq | |
end | |
# Set individual fields to index from hash | |
# index_field :title=>"user.title" | |
def self.index_field(names) | |
self._index_fields = (self._index_fields ||= {}).merge(names) | |
end | |
# Set individual fields to be indexed by category | |
# index_category :address=>"user.address" | |
def self.index_category(names) | |
self._index_categories = (self._index_categories ||= {}).merge(names) | |
end | |
# Set items to index in custom variable section - order is important and should NEVER be changed | |
def self.index_variable(names) | |
self._index_variables = (self._index_variables ||= {}).merge(names) | |
end | |
end | |
end | |
# Functions for indexed fields | |
module Indexed | |
extend ActiveSupport::Concern | |
included do | |
def destroy_index | |
# setup index tank | |
indextank = IndexTank::Client.new ENV['INDEXTANK_API_URL'] | |
tindex = indextank.indexes "#{Rails.env}_things" | |
#Delete index! | |
tindex.document(id.to_s).delete | |
true | |
end | |
def save_index | |
# setup index tank | |
indextank = IndexTank::Client.new ENV['INDEXTANK_API_URL'] | |
tindex = indextank.indexes "#{Rails.env}_things" | |
#puts tindex.running? | |
#puts "#{Rails.env}_things" | |
sindex = {} | |
# Text Fields | |
index_texts = "" | |
self._index_texts.each do |k| | |
r = self.instance_eval(k.to_s).to_s #result uses evalute as it could be x.y or x[0] format | |
index_texts << r << " " unless r.blank? | |
end | |
sindex[:text] = index_texts | |
# Additional Fields | |
self._index_fields.each do |k,v| | |
r = self.instance_eval(v.to_s).to_s #evalute as it could be x.y or x[0] format | |
sindex[k] = r unless r.blank? | |
end | |
categories = {} | |
self._index_categories.each do |k,v| | |
r = self.instance_eval(v.to_s).to_s #evalute as it could be x.y or x[0] format | |
categories[k] = r unless r.blank? | |
end | |
variables = {} | |
self._index_variables.each do |k,v| | |
r = self.instance_eval(v.to_s).to_f #evalute as it could be x.y or x[0] format | |
variables[k] = r | |
end | |
#add additional index field for returning all items | |
sindex['indexed']=true | |
# Pass it to index tank | |
tindex.document(id.to_s).add(sindex,:categories=>categories,:variables=>variables) | |
Rails.logger.debug "Indexed: #{id.to_s}" | |
true | |
end | |
def update_index | |
# update variables only | |
true | |
end | |
public | |
def self.search(query="indexed:true",options={}) | |
options.to_options!.reverse_merge!({:categories=>{},:function=>0,:offset=>0,:limit=>10,:fetch=>"*",:fetch_categories=>"*",:fetch_variables=>"*"}) | |
#puts options | |
indextank = IndexTank::Client.new ENV['INDEXTANK_API_URL'] | |
tindex = indextank.indexes "#{Rails.env}_things" | |
tindex.search(query,:category_filters=>options[:categories],:fetch=>options[:fetch],:fetch_categories=>options[:fetch_categories],:fetch_variables=>options[:fetch_variables],:function=>options[:function],:start=>options[:offset],:len=>options[:limit]) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment