solr-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/lib/solr.rb +18 -0
  2. data/lib/solr/connection.rb +158 -0
  3. data/lib/solr/document.rb +71 -0
  4. data/lib/solr/exception.rb +13 -0
  5. data/lib/solr/field.rb +35 -0
  6. data/lib/solr/request.rb +24 -0
  7. data/lib/solr/request/add_document.rb +63 -0
  8. data/lib/solr/request/base.rb +29 -0
  9. data/lib/solr/request/commit.rb +21 -0
  10. data/lib/solr/request/delete.rb +50 -0
  11. data/lib/solr/request/dismax.rb +43 -0
  12. data/lib/solr/request/index_info.rb +17 -0
  13. data/lib/solr/request/optimize.rb +21 -0
  14. data/lib/solr/request/ping.rb +36 -0
  15. data/lib/solr/request/select.rb +54 -0
  16. data/lib/solr/request/standard.rb +105 -0
  17. data/lib/solr/request/update.rb +22 -0
  18. data/lib/solr/response.rb +24 -0
  19. data/lib/solr/response/add_document.rb +17 -0
  20. data/lib/solr/response/base.rb +42 -0
  21. data/lib/solr/response/commit.rb +32 -0
  22. data/lib/solr/response/delete.rb +13 -0
  23. data/lib/solr/response/dismax.rb +8 -0
  24. data/lib/solr/response/index_info.rb +26 -0
  25. data/lib/solr/response/optimize.rb +14 -0
  26. data/lib/solr/response/ping.rb +28 -0
  27. data/lib/solr/response/ruby.rb +42 -0
  28. data/lib/solr/response/standard.rb +60 -0
  29. data/lib/solr/response/xml.rb +37 -0
  30. data/lib/solr/xml.rb +47 -0
  31. data/test/unit/add_document_test.rb +40 -0
  32. data/test/unit/commit_test.rb +41 -0
  33. data/test/unit/connection_test.rb +55 -0
  34. data/test/unit/delete_test.rb +56 -0
  35. data/test/unit/dismax_request_test.rb +26 -0
  36. data/test/unit/document_test.rb +59 -0
  37. data/test/unit/field_test.rb +42 -0
  38. data/test/unit/ping_test.rb +51 -0
  39. data/test/unit/request_test.rb +61 -0
  40. data/test/unit/response_test.rb +42 -0
  41. data/test/unit/solr_mock_base.rb +40 -0
  42. data/test/unit/standard_request_test.rb +108 -0
  43. data/test/unit/standard_response_test.rb +174 -0
  44. data/test/unit/suite.rb +25 -0
  45. metadata +92 -0
@@ -0,0 +1,43 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Dismax < Solr::Request::Standard
14
+
15
+ VALID_PARAMS.replace(VALID_PARAMS + [:tie_breaker, :query_fields, :minimum_match, :phrase_fields, :phrase_slop,
16
+ :boost_query, :boost_functions])
17
+
18
+ def initialize(params)
19
+ super(params)
20
+ @sort_values = @params[:sort]
21
+ @params.delete(:sort)
22
+ @query_type = "dismax"
23
+ end
24
+
25
+ def to_hash
26
+ hash = super
27
+ hash[:tie] = @params[:tie_breaker]
28
+ hash[:mm] = @params[:minimum_match]
29
+ hash[:qf] = @params[:query_fields]
30
+ hash[:pf] = @params[:phrase_fields]
31
+ hash[:ps] = @params[:phrase_slop]
32
+ hash[:bq] = @params[:boost_query]
33
+ hash[:bf] = @params[:boost_functions]
34
+ # FIXME: 2007-02-13 <coda.hale@gmail.com> -- This code is duplicated in
35
+ # Solr::Request::Standard. It should be refactored into a single location.
36
+ hash[:sort] = @sort_values.collect do |sort|
37
+ key = sort.keys[0]
38
+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
39
+ end.join(',') if @sort_values
40
+ return hash
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::IndexInfo < Solr::Request::Select
14
+ def initialize
15
+ super('indexinfo')
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'solr/xml'
14
+
15
+ class Solr::Request::Optimize < Solr::Request::Update
16
+
17
+ def to_s
18
+ Solr::XML::Element.new('optimize').to_s
19
+ end
20
+
21
+ end
@@ -0,0 +1,36 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ # TODO: Consider something lazy like this?
14
+ # Solr::Request::Ping = Solr::Request.simple_request :format=>:xml, :handler=>'admin/ping'
15
+ # class Solr::Request
16
+ # def self.simple_request(options)
17
+ # Class.new do
18
+ # def response_format
19
+ # options[:format]
20
+ # end
21
+ # def handler
22
+ # options[:handler]
23
+ # end
24
+ # end
25
+ # end
26
+ # end
27
+
28
+ class Solr::Request::Ping < Solr::Request::Base
29
+ def response_format
30
+ :xml
31
+ end
32
+
33
+ def handler
34
+ 'admin/ping'
35
+ end
36
+ end
@@ -0,0 +1,54 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'erb'
14
+
15
+ # "Abstract" base class, only useful with subclasses that add parameters
16
+ class Solr::Request::Select < Solr::Request::Base
17
+
18
+ # TODO add a constant for the all-docs query, which currently is [* TO *]
19
+ # (caveat, that is all docs that have a value in the default field)
20
+ # When the Lucene JAR is upgraded in Solr, the all-docs query becomes simply *
21
+ attr_reader :query_type
22
+
23
+ def initialize(qt=nil)
24
+ @query_type = qt
25
+ end
26
+
27
+ def response_format
28
+ :ruby
29
+ end
30
+
31
+ def handler
32
+ 'select'
33
+ end
34
+
35
+ def to_hash
36
+ return {:qt => query_type, :wt => 'ruby'}
37
+ end
38
+
39
+ def to_s
40
+ raw_params = self.to_hash
41
+
42
+ http_params = []
43
+ raw_params.each do |key,value|
44
+ if value.respond_to? :each
45
+ value.each { |v| http_params << "#{key}=#{ERB::Util::url_encode(v)}" unless v.nil?}
46
+ else
47
+ http_params << "#{key}=#{ERB::Util::url_encode(value)}" unless value.nil?
48
+ end
49
+ end
50
+
51
+ http_params.join("&")
52
+ end
53
+
54
+ end
@@ -0,0 +1,105 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Request::Standard < Solr::Request::Select
14
+
15
+ VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows,
16
+ :filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting]
17
+
18
+ def initialize(params)
19
+ super('standard')
20
+
21
+ raise "Invalid parameters: #{(params.keys - VALID_PARAMS).join(',')}" unless
22
+ (params.keys - VALID_PARAMS).empty?
23
+
24
+ raise ":query parameter required" unless params[:query]
25
+
26
+ @params = params.dup
27
+
28
+ # Validate operator
29
+ if params[:operator]
30
+ raise "Only :and/:or operators allowed" unless
31
+ [:and, :or].include?(params[:operator])
32
+
33
+ @params[:operator] = params[:operator].to_s.upcase
34
+ end
35
+
36
+ # Validate start, rows can be transformed to ints
37
+ @params[:start] = params[:start].to_i if params[:start]
38
+ @params[:rows] = params[:rows].to_i if params[:rows]
39
+
40
+ @params[:field_list] ||= ["*","score"]
41
+ end
42
+
43
+ def to_hash
44
+ hash = {}
45
+
46
+ # standard request param processing
47
+ sort = @params[:sort].collect do |sort|
48
+ key = sort.keys[0]
49
+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
50
+ end.join(',') if @params[:sort]
51
+ hash[:q] = sort ? "#{@params[:query]};#{sort}" : @params[:query]
52
+ hash["q.op"] = @params[:operator]
53
+ hash[:df] = @params[:default_field]
54
+
55
+ # common parameter processing
56
+ hash[:start] = @params[:start]
57
+ hash[:rows] = @params[:rows]
58
+ hash[:fq] = @params[:filter_queries]
59
+ hash[:fl] = @params[:field_list].join(',')
60
+ hash[:debugQuery] = @params[:debug_query]
61
+ hash[:explainOther] = @params[:explain_other]
62
+
63
+ # facet parameter processing
64
+ if @params[:facets]
65
+ # TODO need validation of all that is under the :facets Hash too
66
+ hash[:facet] = true
67
+ hash["facet.field"] = []
68
+ hash["facet.query"] = @params[:facets][:queries]
69
+ hash["facet.sort"] = (@params[:facets][:sort] == :count) if @params[:facets][:sort]
70
+ hash["facet.limit"] = @params[:facets][:limit]
71
+ hash["facet.missing"] = @params[:facets][:missing]
72
+ hash["facet.mincount"] = @params[:facets][:mincount]
73
+ hash["facet.prefix"] = @params[:facets][:prefix]
74
+ @params[:facets][:fields].each do |f|
75
+ if f.kind_of? Hash
76
+ key = f.keys[0]
77
+ value = f[key]
78
+ hash["facet.field"] << key
79
+ hash["f.#{key}.facet.sort"] = (value[:sort] == :count) if value[:sort]
80
+ hash["f.#{key}.facet.limit"] = value[:limit]
81
+ hash["f.#{key}.facet.missing"] = value[:missing]
82
+ hash["f.#{key}.facet.mincount"] = value[:mincount]
83
+ hash["f.#{key}.facet.prefix"] = value[:prefix]
84
+ else
85
+ hash["facet.field"] << f
86
+ end
87
+ end
88
+ end
89
+
90
+ # highlighting parameter processing - http://wiki.apache.org/solr/HighlightingParameters
91
+ #TODO need to add per-field overriding to snippets, fragsize, requiredFieldMatch, formatting, and simple.pre/post
92
+ if @params[:highlighting]
93
+ hash[:hl] = true
94
+ hash["hl.fl"] = @params[:highlighting][:field_list].join(',') if @params[:highlighting][:field_list]
95
+ hash["hl.snippets"] = @params[:highlighting][:max_snippets]
96
+ hash["hl.requireFieldMatch"] = @params[:highlighting][:require_field_match]
97
+ hash["hl.simple.pre"] = @params[:highlighting][:prefix]
98
+ hash["hl.simple.post"] = @params[:highlighting][:suffix]
99
+ end
100
+
101
+
102
+ hash.merge(super.to_hash)
103
+ end
104
+
105
+ end
@@ -0,0 +1,22 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ # a parent class for all requests that go through the solr update handler
14
+ class Solr::Request::Update < Solr::Request::Base
15
+ def response_format
16
+ :xml
17
+ end
18
+
19
+ def handler
20
+ 'update'
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ module Solr; module Response; end; end
14
+ require 'solr/response/base'
15
+ require 'solr/response/xml'
16
+ require 'solr/response/ruby'
17
+ require 'solr/response/ping'
18
+ require 'solr/response/add_document'
19
+ require 'solr/response/standard'
20
+ require 'solr/response/dismax'
21
+ require 'solr/response/commit'
22
+ require 'solr/response/delete'
23
+ require 'solr/response/index_info'
24
+ require 'solr/response/optimize'
@@ -0,0 +1,17 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Response::AddDocument < Solr::Response::Xml
14
+ def initialize(xml)
15
+ super(xml)
16
+ end
17
+ end
@@ -0,0 +1,42 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Response::Base
14
+ attr_reader :raw_response
15
+
16
+ def initialize(raw_response)
17
+ @raw_response = raw_response
18
+ end
19
+
20
+ # factory method for creating a Solr::Response::* from
21
+ # a request and the raw response content
22
+ def self.make_response(request, raw)
23
+
24
+ # make sure response format seems sane
25
+ unless [:xml, :ruby].include?(request.response_format)
26
+ raise Solr::Exception.new("unknown response format: #{request.response_format}" )
27
+ end
28
+
29
+ # TODO: Factor out this case... perhaps the request object should provide the response class instead? Or dynamically align by class name?
30
+ # Maybe the request itself could have the response handling features that get mixed in with a single general purpose response object?
31
+
32
+ begin
33
+ klass = eval(request.class.name.sub(/Request/,'Response'))
34
+ rescue NameError
35
+ raise Solr::Exception.new("unknown request type: #{request.class}")
36
+ else
37
+ klass.new(raw)
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,32 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ require 'rexml/xpath'
14
+
15
+ class Solr::Response::Commit < Solr::Response::Xml
16
+ attr_reader :ok
17
+
18
+ def initialize(xml)
19
+ super(xml)
20
+ e = REXML::XPath.first(@doc, './result')
21
+ if e and e.attributes['status'] == '0'
22
+ @ok = true
23
+ else
24
+ @ok = false
25
+ end
26
+ end
27
+
28
+ def ok?
29
+ @ok
30
+ end
31
+ end
32
+
@@ -0,0 +1,13 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ class Solr::Response::Delete < Solr::Response::Xml; end
@@ -0,0 +1,8 @@
1
+ class Solr::Response::Dismax < Solr::Response::Standard
2
+ # no need for special processing
3
+
4
+ # FIXME: 2007-02-07 <coda.hale@gmail.com> -- The existence of this class indicates that
5
+ # the Request/Response pair architecture is a little hinky. Perhaps we could refactor
6
+ # out some of the most common functionality -- Common Query Parameters, Highlighting Parameters,
7
+ # Simple Facet Parameters, etc. -- into modules?
8
+ end