solr-ruby 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -78,12 +78,29 @@ class Solr::Connection
78
78
  # conn.query('borges') do |hit|
79
79
  # puts hit
80
80
  # end
81
+ #
82
+ # options include:
83
+ #
84
+ # :sort, :default_field, :rows, :filter_queries, :debug_query,
85
+ # :explain_other, :facets, :highlighting, :mlt,
86
+ # :operator => :or / :and
87
+ # :start => defaults to 0
88
+ # :field_list => array, defaults to ["*", "score"]
81
89
 
82
90
  def query(query, options={}, &action)
83
91
  # TODO: Shouldn't this return an exception if the Solr status is not ok? (rather than true/false).
84
92
  create_and_send_query(Solr::Request::Standard, options.update(:query => query), &action)
85
93
  end
86
94
 
95
+ # performs a dismax search and returns a Solr::Response::Standard
96
+ #
97
+ # response = conn.search('borges')
98
+ #
99
+ # options are same as query, but also include:
100
+ #
101
+ # :tie_breaker, :query_fields, :minimum_match, :phrase_fields,
102
+ # :phrase_slop, :boost_query, :boost_functions
103
+
87
104
  def search(query, options={}, &action)
88
105
  create_and_send_query(Solr::Request::Dismax, options.update(:query => query), &action)
89
106
  end
@@ -19,6 +19,7 @@ require 'solr/request/delete'
19
19
  require 'solr/request/ping'
20
20
  require 'solr/request/select'
21
21
  require 'solr/request/standard'
22
+ require 'solr/request/spellcheck'
22
23
  require 'solr/request/dismax'
23
24
  require 'solr/request/update'
24
25
  require 'solr/request/index_info'
@@ -15,13 +15,11 @@ require 'erb'
15
15
  # "Abstract" base class, only useful with subclasses that add parameters
16
16
  class Solr::Request::Select < Solr::Request::Base
17
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
18
  attr_reader :query_type
22
19
 
23
- def initialize(qt=nil)
20
+ def initialize(qt=nil, params={})
24
21
  @query_type = qt
22
+ @select_params = params
25
23
  end
26
24
 
27
25
  def response_format
@@ -37,7 +35,7 @@ class Solr::Request::Select < Solr::Request::Base
37
35
  end
38
36
 
39
37
  def to_hash
40
- return {:qt => query_type, :wt => 'ruby'}
38
+ return {:qt => query_type, :wt => 'ruby'}.merge(@select_params)
41
39
  end
42
40
 
43
41
  def to_s
@@ -0,0 +1,30 @@
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::Spellcheck < Solr::Request::Select
14
+
15
+ def initialize(params)
16
+ super('spellchecker')
17
+ @params = params
18
+ end
19
+
20
+ def to_hash
21
+ hash = super
22
+ hash[:q] = @params[:query]
23
+ hash[:suggestionCount] = @params[:suggestion_count]
24
+ hash[:accuracy] = @params[:accuracy]
25
+ hash[:onlyMorePopular] = @params[:only_more_popular]
26
+ hash[:cmd] = @params[:command]
27
+ return hash
28
+ end
29
+
30
+ end
@@ -12,7 +12,7 @@
12
12
 
13
13
  class Solr::Request::Standard < Solr::Request::Select
14
14
 
15
- VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows,
15
+ VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows, :shards,
16
16
  :filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting, :mlt]
17
17
 
18
18
  def initialize(params)
@@ -38,6 +38,8 @@ class Solr::Request::Standard < Solr::Request::Select
38
38
  @params[:rows] = params[:rows].to_i if params[:rows]
39
39
 
40
40
  @params[:field_list] ||= ["*","score"]
41
+
42
+ @params[:shards] ||= []
41
43
  end
42
44
 
43
45
  def to_hash
@@ -59,6 +61,7 @@ class Solr::Request::Standard < Solr::Request::Select
59
61
  hash[:fl] = @params[:field_list].join(',')
60
62
  hash[:debugQuery] = @params[:debug_query]
61
63
  hash[:explainOther] = @params[:explain_other]
64
+ hash[:shards] = @params[:shards].join(',') unless @params[:shards].empty?
62
65
 
63
66
  # facet parameter processing
64
67
  if @params[:facets]
@@ -71,6 +74,7 @@ class Solr::Request::Standard < Solr::Request::Select
71
74
  hash["facet.missing"] = @params[:facets][:missing]
72
75
  hash["facet.mincount"] = @params[:facets][:mincount]
73
76
  hash["facet.prefix"] = @params[:facets][:prefix]
77
+ hash["facet.offset"] = @params[:facets][:offset]
74
78
  if @params[:facets][:fields] # facet fields are optional (could be facet.query only)
75
79
  @params[:facets][:fields].each do |f|
76
80
  if f.kind_of? Hash
@@ -82,6 +86,7 @@ class Solr::Request::Standard < Solr::Request::Select
82
86
  hash["f.#{key}.facet.missing"] = value[:missing]
83
87
  hash["f.#{key}.facet.mincount"] = value[:mincount]
84
88
  hash["f.#{key}.facet.prefix"] = value[:prefix]
89
+ hash["f.#{key}.facet.offset"] = value[:offset]
85
90
  else
86
91
  hash["facet.field"] << f
87
92
  end
@@ -99,6 +104,16 @@ class Solr::Request::Standard < Solr::Request::Select
99
104
  hash["hl.simple.pre"] = @params[:highlighting][:prefix]
100
105
  hash["hl.simple.post"] = @params[:highlighting][:suffix]
101
106
  hash["hl.fragsize"] = @params[:highlighting][:fragment_size]
107
+ if @params[:highlighting][:alternate_fields]
108
+ @params[:highlighting][:alternate_fields].each do |k,v|
109
+ hash["f.#{k}.hl.alternateField"] = v
110
+ end
111
+ end
112
+ if @params[:highlighting][:max_alternate_field_length]
113
+ @params[:highlighting][:max_alternate_field_length].each do |k,v|
114
+ hash["f.#{k}.hl.maxAlternateFieldLength"] = v
115
+ end
116
+ end
102
117
  end
103
118
 
104
119
  if @params[:mlt]
@@ -18,8 +18,10 @@ require 'solr/response/ping'
18
18
  require 'solr/response/add_document'
19
19
  require 'solr/response/modify_document'
20
20
  require 'solr/response/standard'
21
+ require 'solr/response/spellcheck'
21
22
  require 'solr/response/dismax'
22
23
  require 'solr/response/commit'
23
24
  require 'solr/response/delete'
24
25
  require 'solr/response/index_info'
25
- require 'solr/response/optimize'
26
+ require 'solr/response/optimize'
27
+ require 'solr/response/select'
@@ -11,7 +11,7 @@
11
11
  # limitations under the License.
12
12
 
13
13
  class Solr::Response::Ruby < Solr::Response::Base
14
- attr_reader :data
14
+ attr_reader :data, :header
15
15
 
16
16
  def initialize(ruby_code)
17
17
  super
@@ -38,5 +38,5 @@ class Solr::Response::Ruby < Solr::Response::Base
38
38
  def query_time
39
39
  @header['QTime']
40
40
  end
41
-
41
+
42
42
  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::Response::Select < Solr::Response::Ruby
14
+ def initialize(ruby_code)
15
+ super
16
+ end
17
+ end
@@ -0,0 +1,20 @@
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::Spellcheck < Solr::Response::Ruby
14
+ attr_reader :suggestions
15
+
16
+ def initialize(ruby_code)
17
+ super
18
+ @suggestions = @data['suggestions']
19
+ end
20
+ 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 'test/unit'
14
+
15
+ class ChangesYamlTest < Test::Unit::TestCase
16
+ def test_parse
17
+ change_log = YAML.load_file(File.expand_path(File.dirname(__FILE__)) + "/../../CHANGES.yml")
18
+ assert_equal Date.parse("2007-02-15"), change_log["v0.0.1"]["release_date"]
19
+ assert_equal ["initial release"], change_log["v0.0.1"]["changes"]
20
+ end
21
+ end
@@ -18,8 +18,7 @@ class ModifyDocumentTest < Test::Unit::TestCase
18
18
  def test_update_formatting
19
19
  request = Solr::Request::ModifyDocument.new(:id => 10, :overwrite => {:name => ['val1', 'val2'], :copyfield => nil})
20
20
  assert_equal :xml, request.response_format
21
- assert_equal 'update?mode=copyfield:OVERWRITE,name:OVERWRITE', request.handler
22
-
23
- assert_match(/<add>[\s]*<doc>[\s]*<field name=["']id['"]>10<\/field>[\s]*<field name=['"]name['"]>val1<\/field>[\s]*<field name=['"]name['"]>val2<\/field>[\s]*<\/doc>[\s]*<\/add>/, request.to_s)
21
+ assert_match /copyfield\:OVERWRITE/, request.handler
22
+ assert_match /name\:OVERWRITE/, request.handler
24
23
  end
25
24
  end
@@ -32,11 +32,12 @@ class ResponseTest < SolrMockBaseTestCase
32
32
  Solr::Response::Ruby.new(' {...')
33
33
  end
34
34
  end
35
-
36
- def test_bogus_request_handling
37
- assert_raise(Solr::Exception) do
38
- Solr::Response::Base.make_response(Solr::Request::Select.new, "response data")
39
- end
40
- end
35
+
36
+ # This is now an acceptable use of Select, for the default request handler with no parameters (other than &wt=ruby)
37
+ # def test_bogus_request_handling
38
+ # assert_raise(Solr::Exception) do
39
+ # Solr::Response::Base.make_response(Solr::Request::Select.new, "response data")
40
+ # end
41
+ # end
41
42
 
42
43
  end
@@ -0,0 +1,25 @@
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 'test/unit'
14
+ require 'solr'
15
+
16
+ class SelectTest < Test::Unit::TestCase
17
+
18
+ def test_basic_query
19
+ request = Solr::Request::Select.new('custom', :q => 'query')
20
+ assert_equal :ruby, request.response_format
21
+ assert_equal 'select', request.handler
22
+ assert_equal 'query', request.to_hash[:q]
23
+ end
24
+
25
+ end
@@ -0,0 +1,26 @@
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_mock_base'
14
+
15
+ class SpellcheckResponseTest < SolrMockBaseTestCase
16
+ def test_basic
17
+ ruby_code = "{'responseHeader'=>{'status'=>0,'QTime'=>5},'suggestions'=>['whately','whatcha','whatever']}"
18
+ conn = Solr::Connection.new 'http://localhost:9999'
19
+ set_post_return(ruby_code)
20
+ response = conn.send(Solr::Request::Spellcheck.new(:query => 'whateva'))
21
+ assert_equal true, response.ok?
22
+ assert_equal 3, response.suggestions.size
23
+ assert_equal ['whately','whatcha','whatever'], response.suggestions
24
+ end
25
+ end
26
+
@@ -0,0 +1,27 @@
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 'test/unit'
14
+ require 'solr'
15
+
16
+ class SpellcheckRequestTest < Test::Unit::TestCase
17
+ def test_spellcheck_request
18
+ request = Solr::Request::Spellcheck.new(:query => 'whateva', :suggestion_count => 5, :accuracy => 0.7, :only_more_popular => true)
19
+ assert_equal :ruby, request.response_format
20
+ assert_equal 'select', request.handler
21
+ hash = request.to_hash
22
+ assert_equal 'whateva', hash[:q]
23
+ assert_equal 5, hash[:suggestionCount]
24
+ assert_equal 0.7, hash[:accuracy]
25
+ assert_equal true, hash[:onlyMorePopular]
26
+ end
27
+ end
@@ -71,10 +71,10 @@ class StandardRequestTest < Test::Unit::TestCase
71
71
  :facets => {
72
72
  :fields => [:genre,
73
73
  # field that overrides the global facet parameters
74
- {:year => {:limit => 50, :mincount => 0, :missing => false, :sort => :term, :prefix=>"199"}}],
74
+ {:year => {:limit => 50, :mincount => 0, :missing => false, :sort => :term, :prefix=>"199", :offset => 7}}],
75
75
  :queries => ["q1", "q2"],
76
76
  :prefix => "cat",
77
- :limit => 5, :zeros => true, :mincount => 20, :sort => :count # global facet parameters
77
+ :offset => 3, :limit => 5, :zeros => true, :mincount => 20, :sort => :count # global facet parameters
78
78
  }
79
79
  )
80
80
 
@@ -90,6 +90,8 @@ class StandardRequestTest < Test::Unit::TestCase
90
90
  assert_equal 0, hash["f.year.facet.mincount"]
91
91
  assert_equal false, hash["f.year.facet.sort"]
92
92
  assert_equal "199", hash["f.year.facet.prefix"]
93
+ assert_equal 3, hash["facet.offset"]
94
+ assert_equal 7, hash["f.year.facet.offset"]
93
95
  end
94
96
 
95
97
  def test_basic_sort
@@ -101,6 +103,8 @@ class StandardRequestTest < Test::Unit::TestCase
101
103
  request = Solr::Request::Standard.new(:query => 'query',
102
104
  :highlighting => {
103
105
  :field_list => ['title', 'author'],
106
+ :alternate_fields => {'title'=>'title', 'author'=>'author'},
107
+ :max_alternate_field_length => {'title'=>30, 'author'=>20},
104
108
  :max_snippets => 3,
105
109
  :require_field_match => true,
106
110
  :prefix => "<blink>",
@@ -112,6 +116,10 @@ class StandardRequestTest < Test::Unit::TestCase
112
116
  hash = request.to_hash
113
117
  assert_equal true, hash[:hl]
114
118
  assert_equal "title,author", hash["hl.fl"]
119
+ assert_equal "title", hash["f.title.hl.alternateField"]
120
+ assert_equal "author", hash["f.author.hl.alternateField"]
121
+ assert_equal 30, hash["f.title.hl.maxAlternateFieldLength"]
122
+ assert_equal 20, hash["f.author.hl.maxAlternateFieldLength"]
115
123
  assert_equal true, hash["hl.requireFieldMatch"]
116
124
  assert_equal "<blink>", hash["hl.simple.pre"]
117
125
  assert_equal "</blink>", hash["hl.simple.post"]
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: solr-ruby
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.5
7
- date: 2007-08-27 00:00:00 -04:00
6
+ version: 0.0.6
7
+ date: 2008-07-14 00:00:00 -04:00
8
8
  summary: Ruby library for working with Apache Solr
9
9
  require_paths:
10
10
  - lib
@@ -30,27 +30,20 @@ authors:
30
30
  - Apache Solr
31
31
  files:
32
32
  - lib/solr
33
- - lib/solr.rb
34
33
  - lib/solr/connection.rb
35
34
  - lib/solr/document.rb
36
35
  - lib/solr/exception.rb
37
36
  - lib/solr/field.rb
38
37
  - lib/solr/importer
39
- - lib/solr/importer.rb
40
- - lib/solr/indexer.rb
41
- - lib/solr/request
42
- - lib/solr/request.rb
43
- - lib/solr/response
44
- - lib/solr/response.rb
45
- - lib/solr/solrtasks.rb
46
- - lib/solr/util.rb
47
- - lib/solr/xml.rb
48
38
  - lib/solr/importer/array_mapper.rb
49
39
  - lib/solr/importer/delimited_file_source.rb
50
40
  - lib/solr/importer/hpricot_mapper.rb
51
41
  - lib/solr/importer/mapper.rb
52
42
  - lib/solr/importer/solr_source.rb
53
43
  - lib/solr/importer/xpath_mapper.rb
44
+ - lib/solr/importer.rb
45
+ - lib/solr/indexer.rb
46
+ - lib/solr/request
54
47
  - lib/solr/request/add_document.rb
55
48
  - lib/solr/request/base.rb
56
49
  - lib/solr/request/commit.rb
@@ -61,8 +54,11 @@ files:
61
54
  - lib/solr/request/optimize.rb
62
55
  - lib/solr/request/ping.rb
63
56
  - lib/solr/request/select.rb
57
+ - lib/solr/request/spellcheck.rb
64
58
  - lib/solr/request/standard.rb
65
59
  - lib/solr/request/update.rb
60
+ - lib/solr/request.rb
61
+ - lib/solr/response
66
62
  - lib/solr/response/add_document.rb
67
63
  - lib/solr/response/base.rb
68
64
  - lib/solr/response/commit.rb
@@ -73,10 +69,18 @@ files:
73
69
  - lib/solr/response/optimize.rb
74
70
  - lib/solr/response/ping.rb
75
71
  - lib/solr/response/ruby.rb
72
+ - lib/solr/response/select.rb
73
+ - lib/solr/response/spellcheck.rb
76
74
  - lib/solr/response/standard.rb
77
75
  - lib/solr/response/xml.rb
76
+ - lib/solr/response.rb
77
+ - lib/solr/solrtasks.rb
78
+ - lib/solr/util.rb
79
+ - lib/solr/xml.rb
80
+ - lib/solr.rb
78
81
  - test/unit/add_document_test.rb
79
82
  - test/unit/array_mapper_test.rb
83
+ - test/unit/changes_yaml_test.rb
80
84
  - test/unit/commit_test.rb
81
85
  - test/unit/connection_test.rb
82
86
  - test/unit/data_mapper_test.rb
@@ -92,7 +96,10 @@ files:
92
96
  - test/unit/ping_test.rb
93
97
  - test/unit/request_test.rb
94
98
  - test/unit/response_test.rb
99
+ - test/unit/select_test.rb
95
100
  - test/unit/solr_mock_base.rb
101
+ - test/unit/spellcheck_response_test.rb
102
+ - test/unit/spellchecker_request_test.rb
96
103
  - test/unit/standard_request_test.rb
97
104
  - test/unit/standard_response_test.rb
98
105
  - test/unit/suite.rb