simple_solr 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,8 +1,15 @@
1
- == 0.4.0 (Aug 16, 2011)
1
+ == 0.5.0 (Aug 17, 2011)
2
+
3
+ - Added simple_search method to retrieve results
4
+ - Added DEVELOPMENT.rdoc
2
5
 
3
- - First release as a gem, indexing works
4
6
 
5
7
  == 0.4.1
6
8
 
7
9
  - Spec that missing master_solr config falls back to solr config
8
- - add http:// to the solr uris
10
+ - add http:// to the solr uris
11
+
12
+
13
+ == 0.4.0 (Aug 16, 2011)
14
+
15
+ - First release as a gem, indexing works
data/DEVELOPMENT.rdoc ADDED
@@ -0,0 +1,11 @@
1
+ = Working on SimpleSolr
2
+
3
+ I appreciate the effort! GitHub offers excellent tools for collaborating
4
+ on projects, and the basic fork/branch/pull request style is just fine.
5
+
6
+ To get started, also no surprises:
7
+
8
+ 1. gem install bundler (if you haven't already)
9
+ 2. bundle
10
+ 3. cp spec/db/database.yml.example spec/db/database.yml
11
+ 4. rake
data/README.rdoc CHANGED
@@ -2,11 +2,17 @@ Extremely lightweight adapter between Rails and Solr.
2
2
 
3
3
  SimpleSolr uses the REST interface that Solr offers and makes a great number
4
4
  of assumptions. This has resulted in a very fast, very lightweight library
5
- almost anyone can use straight away.
5
+ almost anyone can use straight away. It does mean you will be working with
6
+ query strings and hashes directly, instead of with Ruby objects.
7
+
8
+ SimpleSolr is ideal when the Solr you use has been provided by a third party
9
+ and is more or less running fine without you.
10
+
11
+ === Design goals and benefits of SimpleSolr
6
12
 
7
13
  * only a few dozen lines of code
8
14
  * extremely simple DSL
9
- * only depends on httparty
15
+ * only depends on httparty[https://github.com/jnunemaker/httparty]
10
16
  * can handle all common use cases
11
17
  * has support for master/slave configurations
12
18
  * does not come with a bundled Solr
@@ -14,40 +20,33 @@ almost anyone can use straight away.
14
20
 
15
21
  And my personal favorite:
16
22
 
17
- * <b>does nothing for unconfigured environments</b>
23
+ * <b>no operation in unconfigured environments</b>
18
24
 
19
25
  If you have no +development+ section in the config file (see below) then
20
26
  nothing will happen at all. Your models will not be indexed, but you are
21
27
  freed from having to run a local Solr instance as well.
22
28
 
23
- Naturally there are downsides as well.
24
-
25
- * assumes an +id+ field present
26
- * makes Solr commit after every change
27
- * does not come with a bundled Solr
28
- * no rake tasks included
29
-
30
- This gem is not really suitable for use outside Rails, as it requires both
31
- ActiveRecord and ActiveSupport.
32
-
33
- I owe a great deal to @outoftime's Sunspot[https://github.com/outoftime/sunspot] library. If you need a fully-packed
34
- solution that handles every Solr case you might have, use that instead.
29
+ I owe a great deal to @outoftime's Sunspot[https://github.com/outoftime/sunspot] library. The
30
+ configuration is identical, so if you come from that you will feel right at home. If you do
31
+ need a fully-packed solution that is a good Ruby/Rails citizen and handles every Solr case you
32
+ might have, use that instead.
35
33
 
36
34
 
37
35
  == Installation
38
36
 
39
- Rails 2, in config/environment.rb:
37
+ Rails 2, in +config/environment.rb+:
40
38
 
41
39
  config.gem 'simple_solr'
42
40
 
43
- Rails 3, in Gemfile:
41
+ Rails 3, in +Gemfile+:
44
42
 
45
43
  gem 'simple_solr'
46
44
 
47
45
 
48
46
  == Configuration
49
47
 
50
- Create a file called <tt>config/simple_solr.yml</tt>.
48
+ Create a file called <tt>config/simple_solr.yml</tt>. (No, there is no generator. You can
49
+ do it by hand, I believe in you.)
51
50
 
52
51
  production:
53
52
  solr:
@@ -61,9 +60,18 @@ Create a file called <tt>config/simple_solr.yml</tt>.
61
60
 
62
61
  If you have just one Solr server, leave out the master_solr section.
63
62
 
63
+ Reasonable defaults are assumed for all these entries, so you can get away
64
+ with just your environment and +solr+ key in there. You can leave out values
65
+ in the +master_solr+ section, since the gem will fall back to using values from
66
+ the +solr+ section instead.
67
+
64
68
  === Your models
65
69
 
66
- Then in your models include the following:
70
+ <b>Note: in these examples I am using a class called Document. This is just for
71
+ illustrative purpuses, your class will probably have a different name, like
72
+ Post or Product. You get the idea.</b>
73
+
74
+ In your models include the following:
67
75
 
68
76
  class Document < ActiveRecord::Base
69
77
  simple_solr do
@@ -72,11 +80,10 @@ Then in your models include the following:
72
80
  end
73
81
 
74
82
  Only the fields listed in the +simple_solr+ block will be sent to Solr,
75
- with the addition of the +id+ field which is always included. The type of
76
- the field is not appended; the XML sent to Solr is as bare as possible.
77
-
78
- This also means no type casting is performed on dates/times. If you need
79
- anything like that, use a lambda to manipulate the field to your liking.
83
+ with the addition of the +id+ field which is always included. Every field
84
+ is passed to Builder, which has limited type casting. If you have special
85
+ needs with regards to date/time formats or anything like that, use a
86
+ lambda to manipulate the field to your liking.
80
87
 
81
88
  Full example:
82
89
 
@@ -86,7 +93,7 @@ Full example:
86
93
  field :title
87
94
  field :date_creation, :created_at
88
95
  field :shared, false
89
- field :published, "Megacorp LLC"
96
+ field :publisher, "Megacorp LLC"
90
97
  field :body
91
98
  end
92
99
  end
@@ -98,6 +105,74 @@ As you can see you have a couple options with regards to the fields:
98
105
  3. Static value such as a string or boolean - which is used verbatim
99
106
  4. A lambda - for your every customization need.
100
107
 
101
- Use the latter form if you want to add a dynamic field to Solr. The model
102
- instance is passed as a parameter, so you can use every method inside
103
- the block, or run a calculation, or whatever.
108
+ Use the latter if you want to add a dynamic field to Solr. The model
109
+ instance is passed as a parameter, so Bob's your uncle and the sky's
110
+ the limit.
111
+
112
+ === Debugging
113
+
114
+ If something isn't quite working, set httparty's +debug_output+ flag
115
+ on your model:
116
+
117
+ Document.debug_output
118
+
119
+ and then save the model to see the HTTP conversation on $stdout.
120
+
121
+ == Searching
122
+
123
+ Use the +simple_search+ class method on your model:
124
+
125
+ Document.simple_search 'apple'
126
+
127
+ This will make a trip to Solr and return the results as a hash. That's right.
128
+ The results are not correlated with your models. In fact, no processing is
129
+ done at all. Simple, right? If you need to fetch model instances based on the
130
+ results, that is up to you.
131
+
132
+ You can add parameters, for example if you want to limit results using +fq+:
133
+
134
+ Document.simple_search 'apple', :fq => "category:fruit"
135
+
136
+ See the Solr documentation[http://wiki.apache.org/solr/CommonQueryParameters] for a list
137
+ of common query parameters. Just remember that the +q+ parameter will be set already,
138
+ although it can be overriden by the parameters you provide.
139
+
140
+ The response from Solr will look something like this:
141
+
142
+ <?xml version="1.0" encoding="UTF-8"?>
143
+ <response>
144
+ <lst name="responseHeader">
145
+ <int name="status">0</int>
146
+ <int name="QTime">3</int>
147
+ </lst>
148
+ <result name="response" numFound="1" start="0" maxScore="13.722203">
149
+ <doc>
150
+ <float name="score">13.722203</float>
151
+ <date name="date_creation">2011-01-06T23:02:33Z</date>
152
+ <str name="id">969</str>
153
+ <str name="publisher">Widgets, Inc</str>
154
+ <str name="title">Golden Delicious</str>
155
+ <str name="category">Apples</str>
156
+ </doc>
157
+ </result>
158
+ <lst name="highlighting">
159
+ <lst name="969"/>
160
+ </lst>
161
+ </response>
162
+
163
+ This response will be translated into a hash by httparty. Take a look at your
164
+ webserver config/response headers if that does not happen.
165
+
166
+
167
+ == Working in batches
168
+
169
+ If you want to add a bunch of existing models to Solr, you can:
170
+
171
+ Document.all.each do |document|
172
+ document.add_to_solr
173
+ end
174
+
175
+
176
+ == Helping out
177
+
178
+ Interested in working on SimpleSolr? Please see DEVELOPMENT.rdoc.
data/lib/simple_solr.rb CHANGED
@@ -2,12 +2,14 @@ require 'active_record'
2
2
  require 'httparty'
3
3
  require 'builder'
4
4
 
5
- require "simple_solr/active_record"
5
+ require "simple_solr/update"
6
+ require "simple_solr/search"
6
7
  require "simple_solr/configuration"
7
8
  require "simple_solr/version"
8
9
 
9
10
  if defined?(ActiveRecord::Base)
10
- ActiveRecord::Base.send :include, SimpleSolr::ActiveRecord
11
+ ActiveRecord::Base.send :include, SimpleSolr::Update
12
+ ActiveRecord::Base.extend SimpleSolr::Search
11
13
  end
12
14
 
13
15
  module SimpleSolr
@@ -15,5 +17,9 @@ module SimpleSolr
15
17
  def configuration
16
18
  @configuration ||= SimpleSolr::Configuration.new
17
19
  end
20
+
21
+ def search(query, params)
22
+ SimpleSolr::Search.simple_search(query, params)
23
+ end
18
24
  end
19
25
  end
@@ -0,0 +1,12 @@
1
+ module SimpleSolr
2
+ module Search
3
+ # Search using the given query. Additional parameters can be added as a hash.
4
+ # For instance, to limit the results by using an +fq+ parameter:
5
+ #
6
+ # Product.simple_search 'delicious', :fq => "category:fruit"
7
+ def simple_search(query, params={})
8
+ query = {:q => query}
9
+ get(SimpleSolr.configuration.uri + "/select", :query => query.merge(params))
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  module SimpleSolr
2
- module ActiveRecord
2
+ module Update
3
3
  def self.included(base)
4
4
  base.extend ClassMethods
5
5
  end
@@ -18,7 +18,7 @@ module SimpleSolr
18
18
  # Store the simple_solr fields for this class
19
19
  cattr_accessor :simple_solr_fields
20
20
  self.simple_solr_fields = { id: nil }
21
- block.call
21
+ block.call if block_given?
22
22
 
23
23
  include InstanceMethods
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSolr
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/simple_solr.gemspec CHANGED
@@ -7,11 +7,9 @@ Gem::Specification.new do |s|
7
7
  s.version = SimpleSolr::VERSION
8
8
  s.authors = ["Joost Baaij"]
9
9
  s.email = ["joost@spacebabies.nl"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/tilsammans/simple_solr"
11
11
  s.summary = %q{Simple Solr client}
12
- s.description = %q{Rails plugins which connects to a Solr search engine with as less features as possible.}
13
-
14
- s.rubyforge_project = "simple_solr"
12
+ s.description = %q{Solr client for Ruby on Rails with as few features as possible.}
15
13
 
16
14
  s.files = `git ls-files`.split("\n")
17
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/spec/db/models.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  # A bunch of models with varying amounts of simple_solrism.
2
2
 
3
+ class SparseDocument < ActiveRecord::Base
4
+ simple_solr
5
+ end
6
+
3
7
  class SimpleDocument < ActiveRecord::Base
4
8
  simple_solr do
5
9
  field :title
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleSolr::Search do
4
+ describe SimpleDocument do
5
+ it "responds to search" do
6
+ SimpleDocument.should respond_to(:simple_search)
7
+ end
8
+
9
+ it "gets results" do
10
+ SimpleDocument.should_receive(:get).with("http://test.local:8983/solr/select", :query => {:q => 'bonanza'})
11
+ SimpleDocument.simple_search 'bonanza'
12
+ end
13
+
14
+ it "allows parameters" do
15
+ SimpleDocument.should_receive(:get).with("http://test.local:8983/solr/select", :query => {:q => 'bonanza', :fq => "brand_site:www.example.com"})
16
+ SimpleDocument.simple_search 'bonanza', :fq => "brand_site:www.example.com"
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SimpleSolr::ActiveRecord do
3
+ describe SimpleSolr::Update do
4
+ describe SparseDocument do
5
+ it "stores just the id field" do
6
+ SparseDocument.simple_solr_fields.should eq({:id => nil})
7
+ end
8
+ end
9
+
4
10
  describe SimpleDocument do
5
11
  it "provides simple_solr class method" do
6
12
  SimpleDocument.should respond_to(:simple_solr)
@@ -16,7 +16,12 @@ if File.exists?(database_yml)
16
16
  end
17
17
 
18
18
  else
19
- raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.example"
19
+ abort <<-FAIL
20
+
21
+ Please create #{database_yml} first to configure your database.
22
+ Take a look at: #{database_yml}.example"
23
+
24
+ FAIL
20
25
  end
21
26
 
22
27
  def clean_database!
metadata CHANGED
@@ -1,81 +1,78 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: simple_solr
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
4
5
  prerelease:
5
- version: 0.4.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Joost Baaij
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-16 00:00:00 +02:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: rspec
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2161392840 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: "2"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
25
22
  type: :development
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: activerecord
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *2161392840
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &2161392420 !ruby/object:Gem::Requirement
31
28
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
36
33
  type: :runtime
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: httparty
40
34
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *2161392420
36
+ - !ruby/object:Gem::Dependency
37
+ name: httparty
38
+ requirement: &2161391960 !ruby/object:Gem::Requirement
42
39
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
47
44
  type: :runtime
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: builder
51
45
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *2161391960
47
+ - !ruby/object:Gem::Dependency
48
+ name: builder
49
+ requirement: &2161391540 !ruby/object:Gem::Requirement
53
50
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
58
55
  type: :runtime
59
- version_requirements: *id004
60
- description: Rails plugins which connects to a Solr search engine with as less features as possible.
61
- email:
56
+ prerelease: false
57
+ version_requirements: *2161391540
58
+ description: Solr client for Ruby on Rails with as few features as possible.
59
+ email:
62
60
  - joost@spacebabies.nl
63
61
  executables: []
64
-
65
62
  extensions: []
66
-
67
63
  extra_rdoc_files: []
68
-
69
- files:
64
+ files:
70
65
  - .gitignore
71
66
  - .rspec
72
67
  - CHANGELOG.rdoc
68
+ - DEVELOPMENT.rdoc
73
69
  - Gemfile
74
70
  - README.rdoc
75
71
  - Rakefile
76
72
  - lib/simple_solr.rb
77
- - lib/simple_solr/active_record.rb
78
73
  - lib/simple_solr/configuration.rb
74
+ - lib/simple_solr/search.rb
75
+ - lib/simple_solr/update.rb
79
76
  - lib/simple_solr/version.rb
80
77
  - simple_solr.gemspec
81
78
  - spec/config/master_slave.yml
@@ -83,48 +80,46 @@ files:
83
80
  - spec/db/database.yml.example
84
81
  - spec/db/models.rb
85
82
  - spec/db/schema.rb
86
- - spec/simple_solr/active_record_spec.rb
87
83
  - spec/simple_solr/configuration_spec.rb
84
+ - spec/simple_solr/search_spec.rb
85
+ - spec/simple_solr/update_spec.rb
88
86
  - spec/simple_solr_spec.rb
89
87
  - spec/spec_helper.rb
90
88
  - spec/support/database.rb
91
89
  - spec/support/rails.rb
92
- has_rdoc: true
93
- homepage: ""
90
+ homepage: https://github.com/tilsammans/simple_solr
94
91
  licenses: []
95
-
96
92
  post_install_message:
97
93
  rdoc_options: []
98
-
99
- require_paths:
94
+ require_paths:
100
95
  - lib
101
- required_ruby_version: !ruby/object:Gem::Requirement
96
+ required_ruby_version: !ruby/object:Gem::Requirement
102
97
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- version: "0"
107
- required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
103
  none: false
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: "0"
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
113
108
  requirements: []
114
-
115
- rubyforge_project: simple_solr
116
- rubygems_version: 1.6.2
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.8
117
111
  signing_key:
118
112
  specification_version: 3
119
113
  summary: Simple Solr client
120
- test_files:
114
+ test_files:
121
115
  - spec/config/master_slave.yml
122
116
  - spec/config/simple_solr.yml
123
117
  - spec/db/database.yml.example
124
118
  - spec/db/models.rb
125
119
  - spec/db/schema.rb
126
- - spec/simple_solr/active_record_spec.rb
127
120
  - spec/simple_solr/configuration_spec.rb
121
+ - spec/simple_solr/search_spec.rb
122
+ - spec/simple_solr/update_spec.rb
128
123
  - spec/simple_solr_spec.rb
129
124
  - spec/spec_helper.rb
130
125
  - spec/support/database.rb