solr_mapper 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +2 -0
- data/Rakefile +1 -1
- data/lib/solr_mapper/relations.rb +34 -12
- data/lib/solr_mapper/solr_document.rb +10 -4
- data/solr_mapper.gemspec +3 -3
- data/spec/facets_spec.rb +56 -0
- metadata +12 -4
data/Manifest
CHANGED
@@ -6,8 +6,10 @@ lib/solr_mapper/calculations.rb
|
|
6
6
|
lib/solr_mapper/locators.rb
|
7
7
|
lib/solr_mapper/relations.rb
|
8
8
|
lib/solr_mapper/solr_document.rb
|
9
|
+
solr_mapper.gemspec
|
9
10
|
spec/base.rb
|
10
11
|
spec/calculations_spec.rb
|
12
|
+
spec/facets_spec.rb
|
11
13
|
spec/locators_spec.rb
|
12
14
|
spec/misc_spec.rb
|
13
15
|
spec/paginated_spec.rb
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ require 'rubygems'
|
|
16
16
|
require 'rake'
|
17
17
|
require 'echoe'
|
18
18
|
|
19
|
-
Echoe.new('solr_mapper', '0.1.
|
19
|
+
Echoe.new('solr_mapper', '0.1.5') do |p|
|
20
20
|
p.description = "Object Document Mapper for the Apache Foundation's Solr search platform"
|
21
21
|
p.url = "http://github.com/skunkworx/solr_mapper"
|
22
22
|
p.author = "Chris Umbel"
|
@@ -19,14 +19,13 @@ module SolrMapper
|
|
19
19
|
|
20
20
|
def has_one(target, opts = {})
|
21
21
|
class_name, field_name, variable_name, id_field = determine_names(target, opts)
|
22
|
-
id_field = foreign_key(id_field)
|
23
22
|
|
24
23
|
class_eval do
|
25
24
|
define_method field_name do
|
26
25
|
val = instance_variable_get(variable_name)
|
27
26
|
|
28
27
|
unless val
|
29
|
-
id = instance_variable_get(id_field)
|
28
|
+
id = instance_variable_get("@#{id_field}")
|
30
29
|
val = Object::const_get(class_name).find(id) if id
|
31
30
|
end
|
32
31
|
|
@@ -41,10 +40,30 @@ module SolrMapper
|
|
41
40
|
|
42
41
|
def has_many(target, opts = {})
|
43
42
|
class_name, field_name, variable_name, id_field = determine_names(target, opts)
|
44
|
-
|
45
|
-
|
43
|
+
@@facet_field_name = field_name
|
46
44
|
has_many_relationships[field_name.to_s] = id_field
|
47
45
|
|
46
|
+
# add class method for loading relations via facets
|
47
|
+
class << self
|
48
|
+
define_method "#{@@facet_field_name}_facet" do |search|
|
49
|
+
fk = has_many_relationships[__method__.to_s.gsub(/_facet$/, '')]
|
50
|
+
|
51
|
+
solr_results = raw_query(search, {'facet' => 'true', 'facet.field' => fk})
|
52
|
+
klass = Object::const_get(classify(singularize(__method__.to_s.gsub(/_facet$/, ''))))
|
53
|
+
results = {}
|
54
|
+
|
55
|
+
solr_results['facet_counts']['facet_fields'][fk].each_slice(2).each do |pair|
|
56
|
+
child = klass.find(pair[0])
|
57
|
+
results[child] = pair[1] if child
|
58
|
+
end
|
59
|
+
|
60
|
+
results
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
@@facet_field_name = nil
|
65
|
+
|
66
|
+
# add a helper method that we'll use to hook arrays with manipulation logic we need
|
48
67
|
class_eval do
|
49
68
|
def relationize_array(array)
|
50
69
|
class << array
|
@@ -75,20 +94,21 @@ module SolrMapper
|
|
75
94
|
|
76
95
|
array
|
77
96
|
end
|
78
|
-
|
97
|
+
|
98
|
+
# define a getter for this relation
|
79
99
|
define_method field_name do
|
80
100
|
val = instance_variable_get(variable_name)
|
81
101
|
|
82
102
|
unless val
|
83
|
-
ids = instance_variable_get(id_field)
|
103
|
+
ids = instance_variable_get("@#{id_field}")
|
84
104
|
val = []
|
85
105
|
|
86
106
|
val.instance_variable_set("@owner", self)
|
87
107
|
val.instance_variable_set("@field_name", field_name.to_s)
|
88
108
|
|
89
109
|
unless ids
|
90
|
-
instance_variable_set(id_field, [])
|
91
|
-
ids = instance_variable_get(id_field)
|
110
|
+
instance_variable_set("@#{id_field}", [])
|
111
|
+
ids = instance_variable_get("@#{id_field}")
|
92
112
|
end
|
93
113
|
|
94
114
|
ids.each do |id|
|
@@ -104,6 +124,7 @@ module SolrMapper
|
|
104
124
|
val
|
105
125
|
end
|
106
126
|
|
127
|
+
# define a setter for this relation
|
107
128
|
define_method "#{field_name}=" do |vals|
|
108
129
|
ids = []
|
109
130
|
|
@@ -120,7 +141,7 @@ module SolrMapper
|
|
120
141
|
relationize_array(vals) unless vals.respond_to?(:destroy)
|
121
142
|
end
|
122
143
|
|
123
|
-
instance_variable_set(id_field, ids)
|
144
|
+
instance_variable_set("@#{id_field}", ids)
|
124
145
|
instance_variable_set("@#{field_name}", val)
|
125
146
|
end
|
126
147
|
end
|
@@ -156,14 +177,15 @@ module SolrMapper
|
|
156
177
|
class_name ||= classify(target)
|
157
178
|
field_name = target
|
158
179
|
variable_name = "@#{field_name}"
|
159
|
-
|
180
|
+
id_field = opts[:foreign_key]
|
181
|
+
id_field ||= "#{foreign_key(singularize(target))}"
|
160
182
|
|
161
|
-
return class_name, field_name, variable_name,
|
183
|
+
return class_name, field_name, variable_name, id_field
|
162
184
|
end
|
163
185
|
end
|
164
186
|
|
165
187
|
def refresh_relation(field_name)
|
166
|
-
ids = instance_variable_get(self.class.has_many_relationships[field_name])
|
188
|
+
ids = instance_variable_get("@#{self.class.has_many_relationships[field_name]}")
|
167
189
|
ids.clear
|
168
190
|
|
169
191
|
instance_variable_get("@#{field_name}").each do |child|
|
@@ -27,7 +27,8 @@ module SolrMapper
|
|
27
27
|
|
28
28
|
# send a read REST command to Solr
|
29
29
|
def execute_read(opts)
|
30
|
-
|
30
|
+
url = build_url('select', opts.merge(:wt => 'ruby'))
|
31
|
+
eval(RestClient.get(url))
|
31
32
|
end
|
32
33
|
|
33
34
|
# send a write REST command to SOlr
|
@@ -75,8 +76,8 @@ module SolrMapper
|
|
75
76
|
results
|
76
77
|
end
|
77
78
|
|
78
|
-
# execute solr query and return the count as well as the results
|
79
|
-
def
|
79
|
+
# execute solr query and return the count as well as the RAW results
|
80
|
+
def raw_query(values, opts = {})
|
80
81
|
if values.kind_of?(Hash)
|
81
82
|
search_string = ''
|
82
83
|
|
@@ -89,7 +90,12 @@ module SolrMapper
|
|
89
90
|
search_string = values.to_s
|
90
91
|
end
|
91
92
|
|
92
|
-
|
93
|
+
execute_read(opts.merge(:q => search_string))
|
94
|
+
end
|
95
|
+
|
96
|
+
# execute solr query and return the count as well as OBJECTS
|
97
|
+
def query_counted(values, opts = {})
|
98
|
+
response = raw_query(values, opts)
|
93
99
|
return map(response), response['response']['numFound']
|
94
100
|
end
|
95
101
|
|
data/solr_mapper.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{solr_mapper}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Chris Umbel"]
|
9
|
-
s.date = %q{2010-10-
|
9
|
+
s.date = %q{2010-10-29}
|
10
10
|
s.description = %q{Object Document Mapper for the Apache Foundation's Solr search platform}
|
11
11
|
s.email = %q{chrisu@dvdempire.com}
|
12
12
|
s.extra_rdoc_files = ["LICENSE.txt", "README.rdoc", "lib/solr_mapper.rb", "lib/solr_mapper/calculations.rb", "lib/solr_mapper/locators.rb", "lib/solr_mapper/relations.rb", "lib/solr_mapper/solr_document.rb"]
|
13
|
-
s.files = ["LICENSE.txt", "README.rdoc", "Rakefile", "lib/solr_mapper.rb", "lib/solr_mapper/calculations.rb", "lib/solr_mapper/locators.rb", "lib/solr_mapper/relations.rb", "lib/solr_mapper/solr_document.rb", "spec/base.rb", "spec/calculations_spec.rb", "spec/locators_spec.rb", "spec/misc_spec.rb", "spec/paginated_spec.rb", "spec/query_spec.rb", "spec/relation_spec.rb", "spec/solr/stuff/config/schema.xml", "spec/solr/thing/config/schema.xml", "spec/solr/widget/conf/schema.xml", "spec/url_spec.rb", "Manifest"
|
13
|
+
s.files = ["LICENSE.txt", "README.rdoc", "Rakefile", "lib/solr_mapper.rb", "lib/solr_mapper/calculations.rb", "lib/solr_mapper/locators.rb", "lib/solr_mapper/relations.rb", "lib/solr_mapper/solr_document.rb", "solr_mapper.gemspec", "spec/base.rb", "spec/calculations_spec.rb", "spec/facets_spec.rb", "spec/locators_spec.rb", "spec/misc_spec.rb", "spec/paginated_spec.rb", "spec/query_spec.rb", "spec/relation_spec.rb", "spec/solr/stuff/config/schema.xml", "spec/solr/thing/config/schema.xml", "spec/solr/widget/conf/schema.xml", "spec/url_spec.rb", "Manifest"]
|
14
14
|
s.homepage = %q{http://github.com/skunkworx/solr_mapper}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Solr_mapper", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
data/spec/facets_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Copyright 2010 The Skunkworx.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require File.dirname(__FILE__) + '/base'
|
16
|
+
require 'rest_client'
|
17
|
+
|
18
|
+
describe SolrMapper::SolrDocument do
|
19
|
+
before(:all) do
|
20
|
+
url = Thing::base_url
|
21
|
+
resource = RestClient::Resource.new("#{url}/update")
|
22
|
+
resource.post("<delete><query>id:[* TO *]</query></delete>", {:content_type => 'text/xml'})
|
23
|
+
resource.post("<commit/>", {:content_type => 'text/xml'})
|
24
|
+
|
25
|
+
url = Stuff::base_url
|
26
|
+
resource = RestClient::Resource.new("#{url}/update")
|
27
|
+
resource.post("<delete><query>id:[* TO *]</query></delete>", {:content_type => 'text/xml'})
|
28
|
+
resource.post("<commit/>", {:content_type => 'text/xml'})
|
29
|
+
|
30
|
+
stuff = Stuff.new({:_id => '04787560-bc23-012d-817c-60334b2add63', :name => 'stuff 1'})
|
31
|
+
stuff.save
|
32
|
+
|
33
|
+
stuff = Stuff.new({:_id => '04787560-bc23-012d-817c-60334b2add64', :name => 'stuff 2'})
|
34
|
+
stuff.save
|
35
|
+
|
36
|
+
stuff = Stuff.new({:_id => '04787560-bc23-012d-817c-60334b2add65', :name => 'stuff 3'})
|
37
|
+
stuff.save
|
38
|
+
|
39
|
+
thing = Thing.new({:_id => '04787560-bc23-012d-817c-60334b2add60', :content => 'A',
|
40
|
+
:stuff_id => ['04787560-bc23-012d-817c-60334b2add63', '04787560-bc23-012d-817c-60334b2add64']})
|
41
|
+
thing.save
|
42
|
+
|
43
|
+
thing = Thing.new({:_id => '04787560-bc23-012d-817c-60334b2add61', :content => 'A',
|
44
|
+
:stuff_id => ['04787560-bc23-012d-817c-60334b2add63']})
|
45
|
+
thing.save
|
46
|
+
|
47
|
+
thing = Thing.new({:_id => '04787560-bc23-012d-817c-60334b2add62', :content => 'B',
|
48
|
+
:stuff_id => ['04787560-bc23-012d-817c-60334b2add65']})
|
49
|
+
thing.save
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should lookup related object by facet" do
|
53
|
+
Thing.stuffs_facet('*:*').count.should_not == 0
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: solr_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Chris Umbel
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-29 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
28
30
|
segments:
|
29
31
|
- 0
|
30
32
|
version: "0"
|
@@ -38,6 +40,7 @@ dependencies:
|
|
38
40
|
requirements:
|
39
41
|
- - ">="
|
40
42
|
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
41
44
|
segments:
|
42
45
|
- 0
|
43
46
|
version: "0"
|
@@ -51,6 +54,7 @@ dependencies:
|
|
51
54
|
requirements:
|
52
55
|
- - ">="
|
53
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
54
58
|
segments:
|
55
59
|
- 0
|
56
60
|
version: "0"
|
@@ -64,6 +68,7 @@ dependencies:
|
|
64
68
|
requirements:
|
65
69
|
- - ">="
|
66
70
|
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
67
72
|
segments:
|
68
73
|
- 0
|
69
74
|
version: "0"
|
@@ -92,8 +97,10 @@ files:
|
|
92
97
|
- lib/solr_mapper/locators.rb
|
93
98
|
- lib/solr_mapper/relations.rb
|
94
99
|
- lib/solr_mapper/solr_document.rb
|
100
|
+
- solr_mapper.gemspec
|
95
101
|
- spec/base.rb
|
96
102
|
- spec/calculations_spec.rb
|
103
|
+
- spec/facets_spec.rb
|
97
104
|
- spec/locators_spec.rb
|
98
105
|
- spec/misc_spec.rb
|
99
106
|
- spec/paginated_spec.rb
|
@@ -104,7 +111,6 @@ files:
|
|
104
111
|
- spec/solr/widget/conf/schema.xml
|
105
112
|
- spec/url_spec.rb
|
106
113
|
- Manifest
|
107
|
-
- solr_mapper.gemspec
|
108
114
|
has_rdoc: true
|
109
115
|
homepage: http://github.com/skunkworx/solr_mapper
|
110
116
|
licenses: []
|
@@ -124,6 +130,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
130
|
requirements:
|
125
131
|
- - ">="
|
126
132
|
- !ruby/object:Gem::Version
|
133
|
+
hash: 3
|
127
134
|
segments:
|
128
135
|
- 0
|
129
136
|
version: "0"
|
@@ -132,6 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
139
|
requirements:
|
133
140
|
- - ">="
|
134
141
|
- !ruby/object:Gem::Version
|
142
|
+
hash: 11
|
135
143
|
segments:
|
136
144
|
- 1
|
137
145
|
- 2
|