Module: ActiveBlur

Defined in:
lib/active_blur/config.rb,
lib/active_blur/railtie.rb,
lib/active_blur/version.rb,
lib/active_blur/searchable.rb,
lib/active_blur/active_blur.rb,
lib/active_blur/search_results.rb

Defined Under Namespace

Modules: Searchable Classes: Config, Railtie, ScoredResult, SearchResults

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.create_modeObject

Returns the value of attribute create_mode.



17
18
19
# File 'lib/active_blur/active_blur.rb', line 17

def create_mode
  @create_mode
end

Class Method Details

.create_table(clazz) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_blur/active_blur.rb', line 55

def create_table(clazz)
  options = options_for clazz
  raise "#{clazz.name} has not been configured to be searchable" unless options
  raise "table #{options[:table_name]} already exists" if @tables.include? options[:table_name]
  db_columns = columns_for_class(clazz, options)
  table_descriptor = Blur::TableDescriptor.new
  table_descriptor.shardCount = options[:shard_count]
  table_descriptor.tableUri = calculate_table_uri(options)
  table_descriptor.cluster = options[:cluster]
  table_descriptor.name = options[:table_name]
  analyzer = Blur::AnalyzerDefinition.new
  analyzer.defaultDefinition = new_column_definition(options[:default_analyzer], options[:full_text_index])
  analyzer.fullTextAnalyzerClassName = analyzer(options[:full_text_analyzer])

  col_fam_definition = Blur::ColumnFamilyDefinition.new
  col_fam_definition.defaultDefinition = new_column_definition(options[:default_analyzer], options[:full_text_index])
  col_fam_definition.columnDefinitions = {}
  columns_for_class(clazz, options).each do |col|
    col_fam_definition.columnDefinitions[col] = new_column_definition(options[:default_analyzer], options[:full_text_index])
  end
  analyzer.columnFamilyDefinitions={options[:column_family]=>col_fam_definition}
  table_descriptor.analyzerDefinition = analyzer
  puts "about to create table #{table_descriptor.inspect}"
  client.createTable(table_descriptor)
end

.index(object) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/active_blur/active_blur.rb', line 115

def index(object)
  puts "indexing #{object.class} with id #{object.id}"
  options = options_for object.class
  mutation = Blur::RowMutation.new
  mutation.table = options[:table_name]
  mutation.rowId = "#{object.class.name}-#{object.id}"
  mutation.rowMutationType = Blur::RowMutationType::REPLACE_ROW
  record_mutation = Blur::RecordMutation.new
  record_mutation.recordMutationType = Blur::RecordMutationType::REPLACE_ENTIRE_RECORD
  record = Blur::Record.new
  record.recordId = object.id.to_s
  record.family = options[:column_family]
  record.columns = []
  columns_for_class(object.class, options).each do |col|
    column = Blur::Column.new
    column.name = col
    val = object.send(col)
    if val
      column.value = val.to_s
      record.columns << column
    end
  end
  record_mutation.record = record
  mutation.recordMutations = [record_mutation]
  client.mutate(mutation)
end

.reindex(clazz) ⇒ Object



108
109
110
111
112
113
# File 'lib/active_blur/active_blur.rb', line 108

def reindex(clazz)
  all = clazz.all
  all.each do |obj|
    index(obj)
  end
end

.remove(object) ⇒ Object



142
143
144
# File 'lib/active_blur/active_blur.rb', line 142

def remove(object)
  remove_by_id(object.class, object.id)
end

.remove_by_id(clazz, id) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/active_blur/active_blur.rb', line 146

def remove_by_id(clazz, id)
  puts "removing #{clazz} with id #{id}"
  options = options_for clazz
  mutation = Blur::RowMutation.new
  mutation.table = options[:table_name]
  mutation.rowId = "#{clazz.name}-#{id}"
  mutation.rowMutationType = Blur::RowMutationType::DELETE_ROW
  client.mutate(mutation)
end

.search(clazz, query, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_blur/active_blur.rb', line 81

def search(clazz, query, options={})
  model_options = options_for clazz
  blur_query = Blur::BlurQuery.new
  #query.facets = ???
  simple_query = Blur::SimpleQuery.new
  simple_query.queryStr = query
  simple_query.superQueryOn = false
  simple_query.type = score_type(options[:score_type]) unless options[:score_type].nil?
  simple_query.postSuperFilter = options[:post_super_filter] unless options[:post_super_filter].nil?
  simple_query.preSuperFilter = options[:pre_super_filter] unless options[:pre_super_filter].nil?
  blur_query.simpleQuery = simple_query
  blur_query.allowStaleData = options[:allow_stale_data] unless options[:allow_stale_data].nil?
  blur_query.useCacheIfPresent = options[:use_cache_if_present] unless options[:use_cache_if_present].nil?
  blur_query.start = options[:start] unless options[:start].nil?
  blur_query.fetch = options[:fetch] unless options[:fetch].nil?
  blur_query.minimumNumberOfResults = options[:minimum_number_of_results] unless options[:minimum_number_of_results].nil?
  blur_query.maxQueryTime = options[:max_query_time] unless options[:max_query_time].nil?
  blur_query.uuid = options[:uuid] unless options[:uuid].nil?
  blur_query.userContext = options[:user_context] unless options[:user_context].nil?
  blur_query.cacheResult = options[:cache_result] unless options[:cache_result].nil?
  blur_query.modifyFileCaches = options[:modify_file_caches] unless options[:modify_file_caches].nil?
  selector = Blur::Selector.new(:recordOnly=>true, :columnFamiliesToFetch=>['none_col_fam'])
  blur_query.selector = selector
  results = client.query(model_options[:table_name], blur_query)
  SearchResults.new(results, clazz)
end

.searchable_classesObject



51
52
53
# File 'lib/active_blur/active_blur.rb', line 51

def searchable_classes
  @searchable_classes.keys
end

.setup_class(clazz, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_blur/active_blur.rb', line 37

def setup_class(clazz, options={})
  defaults = {:table_name=>clazz.table_name, :column_family=>clazz.table_name.singularize, :shard_count=>3}
  options = defaults.merge(@global_options).merge(options)
  options[:exclude] = options[:exclude].collect{|col| col.to_s} if options[:exclude]
  raise "options[:cluster] is required" unless options[:cluster]
  puts "setup options for #{clazz} are #{options.inspect}"
  unless create_mode
    ensure_table_exists! options[:table_name]
    ensure_table_enabled! options[:table_name]
    ensure_schemas_match! clazz, options
  end
  @searchable_classes[clazz] = options
end

.setup_connection(connection_str, options = {}, thrift_options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_blur/active_blur.rb', line 19

def setup_connection(connection_str, options={}, thrift_options={})
  @global_options = @global_options.merge(options)
  thrift_defaults = {:retries=>5}
  thrift_options = thrift_defaults.merge(thrift_options)
  if connection_str.class == String
    urls = connection_str.split(/,/)
  else
    urls = Array(connection_str)
  end
  unless thrift_options[:server_retry_period]
    thrift_options[:server_retry_period] = urls.size < 2 ? -1 : urls.size
  end
  @blur_client = ThriftClient.new Blur::Blur::Client, urls, thrift_options
  puts "ActiveBlur connection setup"
  @tables = client.tableList
  puts "Found tables #{@tables}"
end