Apache Lucene open-source search software
Add preliminary support for SOLR-139, allowing updateable documents. Added utility method to backslash escape strings suitable for QueryParser. Bumped version number for another upcoming gem push.
Erik Hatcher8c0599def15b
diff --git a/client/ruby/solr-ruby/CHANGES.yml b/client/ruby/solr-ruby/CHANGES.ymlindex 5931a1cb6b0..3b6a53a752a 100644--- a/client/ruby/solr-ruby/CHANGES.yml+++ b/client/ruby/solr-ruby/CHANGES.yml@@ -1,3 +1,11 @@+- v0.0.5+ release_date: 2007-08-??+ changes:+ - Added support for highlighter fragment size to Solr::Request::Standard+ - Added support for MoreLikeThese to Solr::Request::Standard+ - Added Solr::Request::ModifyDocument (requires SOLR-139 patch)+ - Added Solr::Util.query_parser_escape()+- v0.0.4release_date: 2007-08-16changes:diff --git a/client/ruby/solr-ruby/Rakefile b/client/ruby/solr-ruby/Rakefileindex 2499bf2dd97..a1321193970 100644--- a/client/ruby/solr-ruby/Rakefile+++ b/client/ruby/solr-ruby/Rakefile@@ -29,7 +29,7 @@## rake SOLR_CONSOLE=true-SOLR_RUBY_VERSION = '0.0.4'+SOLR_RUBY_VERSION = '0.0.5'require 'rubygems'require 'rake'diff --git a/client/ruby/solr-ruby/lib/solr/connection.rb b/client/ruby/solr-ruby/lib/solr/connection.rbindex 963d1362737..b610dbc77ee 100755--- a/client/ruby/solr-ruby/lib/solr/connection.rb+++ b/client/ruby/solr-ruby/lib/solr/connection.rb@@ -28,7 +28,7 @@ class Solr::Connection# conn = Solr::Connection.new('http://example.com:8080/solr',# :autocommit => :on)- def initialize(url, opts={})+ def initialize(url="http://localhost:8983/solr", opts={})@url = URI.parse(url)unless @url.kind_of? URI::HTTPraise "invalid http url: #{url}"diff --git a/client/ruby/solr-ruby/lib/solr/request.rb b/client/ruby/solr-ruby/lib/solr/request.rbindex a25aaf434cb..9f0f60f64b7 100755--- a/client/ruby/solr-ruby/lib/solr/request.rb+++ b/client/ruby/solr-ruby/lib/solr/request.rb@@ -12,6 +12,7 @@module Solr; module Request; end; endrequire 'solr/request/add_document'+require 'solr/request/modify_document'require 'solr/request/base'require 'solr/request/commit'require 'solr/request/delete'diff --git a/client/ruby/solr-ruby/lib/solr/request/modify_document.rb b/client/ruby/solr-ruby/lib/solr/request/modify_document.rbnew file mode 100755index 00000000000..d0f7a28dccf--- /dev/null+++ b/client/ruby/solr-ruby/lib/solr/request/modify_document.rb@@ -0,0 +1,61 @@+# 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/xml'+require 'solr/request/base'+require 'solr/document'+require 'solr/request/update'++class Solr::Request::ModifyDocument < Solr::Request::Update++ # Example: ModifyDocument.new(:id => 10, :overwrite => {:field_name => "new value"})+ def initialize(update_data)+ modes = []+ @fields = {}+ [:overwrite, :append, :distinct, :increment].each do |mode|+ field_data = update_data[mode]+ if field_data+ field_data.each do |field_name, field_value|+ modes << "#{field_name}:#{mode.to_s.upcase}"+ @fields[field_name] = field_value+ end+ update_data.delete mode+ end+ end+ @mode = modes.join(",")+ @id = update_data # should only be one key remaining+ end++ # returns the request as a string suitable for posting+ def to_s+ e = Solr::XML::Element.new 'add'+ doc = Solr::XML::Element.new 'doc'+ e.add_element doc+ f = Solr::XML::Element.new 'field'+ f.attributes['name'] = @id.keys[0].to_s+ f.text = @id.values[0]+ doc.add_element f+ @fields.each do |key, value|+ f = Solr::XML::Element.new 'field'+ f.attributes['name'] = key.to_s+ # TODO - what about boost? - can it be updated too?+ f.text = value+ doc.add_element f+ end+ return e.to_s+ end++ def handler+ "update?mode=#{@mode}"+ end++enddiff --git a/client/ruby/solr-ruby/lib/solr/response.rb b/client/ruby/solr-ruby/lib/solr/response.rbindex b14b2c239d9..0b9060a811d 100755--- a/client/ruby/solr-ruby/lib/solr/response.rb+++ b/client/ruby/solr-ruby/lib/solr/response.rb@@ -16,6 +16,7 @@ require 'solr/response/xml'require 'solr/response/ruby'require 'solr/response/ping'require 'solr/response/add_document'+require 'solr/response/modify_document'require 'solr/response/standard'require 'solr/response/dismax'require 'solr/response/commit'diff --git a/client/ruby/solr-ruby/lib/solr/response/modify_document.rb b/client/ruby/solr-ruby/lib/solr/response/modify_document.rbnew file mode 100755index 00000000000..44c4f5b0cb6--- /dev/null+++ b/client/ruby/solr-ruby/lib/solr/response/modify_document.rb@@ -0,0 +1,17 @@+# 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::ModifyDocument < Solr::Response::Xml+ def initialize(xml)+ super+ end+enddiff --git a/client/ruby/solr-ruby/lib/solr/response/standard.rb b/client/ruby/solr-ruby/lib/solr/response/standard.rbindex 4468765d741..2e59fe9b068 100644--- a/client/ruby/solr-ruby/lib/solr/response/standard.rb+++ b/client/ruby/solr-ruby/lib/solr/response/standard.rb@@ -36,6 +36,7 @@ class Solr::Response::Standard < Solr::Response::Ruby@response['maxScore']end+ # TODO: consider the use of json.nl parameterdef field_facets(field)facets = []values = @data['facet_counts']['facet_fields'][field]diff --git a/client/ruby/solr-ruby/lib/solr/util.rb b/client/ruby/solr-ruby/lib/solr/util.rbindex 168ab65f6d3..bb134eeeefa 100755--- a/client/ruby/solr-ruby/lib/solr/util.rb+++ b/client/ruby/solr-ruby/lib/solr/util.rb@@ -25,4 +25,8 @@ class Solr::UtilHash[*a]end+ def self.query_parser_escape(string)+ # backslash prefix everything that isn't a word character+ string.gsub(/(\W)/,'\\\\\1')+ endenddiff --git a/client/ruby/solr-ruby/test/unit/modify_document_test.rb b/client/ruby/solr-ruby/test/unit/modify_document_test.rbnew file mode 100755index 00000000000..45aaeb33a3d--- /dev/null+++ b/client/ruby/solr-ruby/test/unit/modify_document_test.rb@@ -0,0 +1,25 @@+# 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 ModifyDocumentTest < Test::Unit::TestCase++ def test_update_formatting+ request = Solr::Request::ModifyDocument.new(:id => 10, :overwrite => {:name => :value})+ assert_equal :xml, request.response_format+ assert_equal 'update?mode=name:OVERWRITE', request.handler++ assert_match(/<add>[\s]*<doc>[\s]*<field name=["']id['"]>10<\/field>[\s]*<field name=['"]name['"]>value<\/field>[\s]*<\/doc>[\s]*<\/add>/, request.to_s)+ end+enddiff --git a/client/ruby/solr-ruby/test/unit/util_test.rb b/client/ruby/solr-ruby/test/unit/util_test.rbindex d7c11d2fa41..b45462a590f 100755--- a/client/ruby/solr-ruby/test/unit/util_test.rb+++ b/client/ruby/solr-ruby/test/unit/util_test.rb@@ -14,9 +14,11 @@ require 'solr'require 'test/unit'class UtilTest < Test::Unit::TestCase-def test_paired_array_to_hashassert_equal({:key1 => :value1, :key2 => :value2}, Solr::Util.paired_array_to_hash([:key1, :value1, :key2, :value2]))end+ def test_query_parser_escape+ assert_equal %q(http\:\/\/lucene\.apache\.org\/solr), Solr::Util.query_parser_escape("http://lucene.apache.org/solr")+ endend