Class: Chef::Solr::Index

Inherits:
Chef::Solr show all
Defined in:
lib/chef/solr/index.rb

Constant Summary collapse

UNDERSCORE =
'_'
X =
'X'
X_CHEF_id_CHEF_X =
'X_CHEF_id_CHEF_X'
X_CHEF_database_CHEF_X =
'X_CHEF_database_CHEF_X'
X_CHEF_type_CHEF_X =
'X_CHEF_type_CHEF_X'

Constants inherited from Chef::Solr

CLOSE_FIELD, END_XML, FIELD_ATTR, FIELD_ATTR_END, START_XML, VERSION

Instance Attribute Summary

Attributes inherited from Chef::Solr

#http, #solr_url

Instance Method Summary collapse

Methods inherited from Chef::Solr

#initialize, #post_to_solr, #rebuild_index, #solr_add, #solr_commit, #solr_delete_by_id, #solr_delete_by_query, #solr_optimize, #solr_rollback, #solr_select

Constructor Details

This class inherits a constructor from Chef::Solr

Instance Method Details

#add(id, database, type, item) ⇒ Object



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

def add(id, database, type, item)
  unless item.respond_to?(:keys)
    raise ArgumentError, "#{self.class.name} can only index Hash-like objects. You gave #{item.inspect}"
  end

  to_index = flatten_and_expand(item)

  to_index[X_CHEF_id_CHEF_X]        = [id]
  to_index[X_CHEF_database_CHEF_X]  = [database]
  to_index[X_CHEF_type_CHEF_X]      = [type]

  solr_add(to_index)
  to_index
end

#add_field_value(keys, value) ⇒ Object



84
85
86
87
88
89
# File 'lib/chef/solr/index.rb', line 84

def add_field_value(keys, value)
  value = value.to_s
  each_expando_field(keys) { |expando_field| @flattened_item[expando_field] << value }
  @flattened_item[keys.join(UNDERSCORE)] << value
  @flattened_item[keys.last] << value
end

#delete(id) ⇒ Object



51
52
53
# File 'lib/chef/solr/index.rb', line 51

def delete(id)
  solr_delete_by_id(id)
end

#delete_by_query(query) ⇒ Object



55
56
57
# File 'lib/chef/solr/index.rb', line 55

def delete_by_query(query)
  solr_delete_by_query(query)
end

#each_expando_field(keys) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/chef/solr/index.rb', line 91

def each_expando_field(keys)
  return if keys.size == 1
  0.upto(keys.size - 1) do |index|
    original = keys[index]
    keys[index] = X
    yield keys.join(UNDERSCORE)
    keys[index] = original
  end
end

#flatten_and_expand(item) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/solr/index.rb', line 59

def flatten_and_expand(item)
  @flattened_item = Hash.new {|hash, key| hash[key] = []}

  item.each do |key, value|
    flatten_each([key.to_s], value)
  end

  @flattened_item.each_value { |values| values.uniq! }
  @flattened_item
end

#flatten_each(keys, values) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/solr/index.rb', line 70

def flatten_each(keys, values)
  case values
  when Hash
    values.each do |child_key, child_value|
      add_field_value(keys, child_key)
      flatten_each(keys + [child_key.to_s], child_value)
    end
  when Array
    values.each { |child_value| flatten_each(keys, child_value) }
  else
    add_field_value(keys, values)
  end
end