Skip to content

Apache Lucene open-source search software

Add Spellcheck support

Erik Hatcher56ceddd6c0b8
diff --git a/client/ruby/solr-ruby/CHANGES.yml b/client/ruby/solr-ruby/CHANGES.yml
index 0d25a8d84fc..68fdb302d0d 100644
--- a/client/ruby/solr-ruby/CHANGES.yml
+++ b/client/ruby/solr-ruby/CHANGES.yml
@@ -1,5 +1,10 @@
+v0.0.6:
+ release_date: TBD
+ changes:
+ - Added Solr::Request::Spellcheck
+
v0.0.5:
- release_date: 2007-08-??
+ release_date: 2007-08-27
changes:
- Added support for highlighter fragment size to Solr::Request::Standard
- Added support for MoreLikeThese to Solr::Request::Standard
diff --git a/client/ruby/solr-ruby/lib/solr/request.rb b/client/ruby/solr-ruby/lib/solr/request.rb
index 9f0f60f64b7..a3ce7da3396 100755
--- a/client/ruby/solr-ruby/lib/solr/request.rb
+++ b/client/ruby/solr-ruby/lib/solr/request.rb
@@ -19,6 +19,7 @@ require 'solr/request/delete'
require 'solr/request/ping'
require 'solr/request/select'
require 'solr/request/standard'
+require 'solr/request/spellcheck'
require 'solr/request/dismax'
require 'solr/request/update'
require 'solr/request/index_info'
diff --git a/client/ruby/solr-ruby/lib/solr/request/spellcheck.rb b/client/ruby/solr-ruby/lib/solr/request/spellcheck.rb
new file mode 100644
index 00000000000..eab24eb9a9a
--- /dev/null
+++ b/client/ruby/solr-ruby/lib/solr/request/spellcheck.rb
@@ -0,0 +1,30 @@
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class Solr::Request::Spellcheck < Solr::Request::Select
+
+ def initialize(params)
+ super('spellchecker')
+ @params = params
+ end
+
+ def to_hash
+ hash = super
+ hash[:q] = @params[:query]
+ hash[:suggestionCount] = @params[:suggestion_count]
+ hash[:accuracy] = @params[:accuracy]
+ hash[:onlyMorePopular] = @params[:only_more_popular]
+ hash[:cmd] = @params[:command]
+ return hash
+ end
+
+end
\ No newline at end of file
diff --git a/client/ruby/solr-ruby/lib/solr/response.rb b/client/ruby/solr-ruby/lib/solr/response.rb
index 0b9060a811d..ce3c0812b3d 100755
--- a/client/ruby/solr-ruby/lib/solr/response.rb
+++ b/client/ruby/solr-ruby/lib/solr/response.rb
@@ -18,6 +18,7 @@ require 'solr/response/ping'
require 'solr/response/add_document'
require 'solr/response/modify_document'
require 'solr/response/standard'
+require 'solr/response/spellcheck'
require 'solr/response/dismax'
require 'solr/response/commit'
require 'solr/response/delete'
diff --git a/client/ruby/solr-ruby/lib/solr/response/spellcheck.rb b/client/ruby/solr-ruby/lib/solr/response/spellcheck.rb
new file mode 100644
index 00000000000..a4842c58424
--- /dev/null
+++ b/client/ruby/solr-ruby/lib/solr/response/spellcheck.rb
@@ -0,0 +1,20 @@
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class Solr::Response::Spellcheck < Solr::Response::Ruby
+ attr_reader :suggestions
+
+ def initialize(ruby_code)
+ super
+ @suggestions = @data['suggestions']
+ end
+end
diff --git a/client/ruby/solr-ruby/test/unit/spellcheck_response_test.rb b/client/ruby/solr-ruby/test/unit/spellcheck_response_test.rb
new file mode 100644
index 00000000000..0d9217425f0
--- /dev/null
+++ b/client/ruby/solr-ruby/test/unit/spellcheck_response_test.rb
@@ -0,0 +1,26 @@
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'solr_mock_base'
+
+class SpellcheckResponseTest < SolrMockBaseTestCase
+ def test_basic
+ ruby_code = "{'responseHeader'=>{'status'=>0,'QTime'=>5},'suggestions'=>['whately','whatcha','whatever']}"
+ conn = Solr::Connection.new 'http://localhost:9999'
+ set_post_return(ruby_code)
+ response = conn.send(Solr::Request::Spellcheck.new(:query => 'whateva'))
+ assert_equal true, response.ok?
+ assert_equal 3, response.suggestions.size
+ assert_equal ['whately','whatcha','whatever'], response.suggestions
+ end
+end
+
diff --git a/client/ruby/solr-ruby/test/unit/spellchecker_request_test.rb b/client/ruby/solr-ruby/test/unit/spellchecker_request_test.rb
new file mode 100644
index 00000000000..9a603443e29
--- /dev/null
+++ b/client/ruby/solr-ruby/test/unit/spellchecker_request_test.rb
@@ -0,0 +1,27 @@
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'test/unit'
+require 'solr'
+
+class SpellcheckRequestTest < Test::Unit::TestCase
+ def test_spellcheck_request
+ request = Solr::Request::Spellcheck.new(:query => 'whateva', :suggestion_count => 5, :accuracy => 0.7, :only_more_popular => true)
+ assert_equal :ruby, request.response_format
+ assert_equal 'select', request.handler
+ hash = request.to_hash
+ assert_equal 'whateva', hash[:q]
+ assert_equal 5, hash[:suggestionCount]
+ assert_equal 0.7, hash[:accuracy]
+ assert_equal true, hash[:onlyMorePopular]
+ end
+end