Skip to content

Apache Lucene open-source search software

add utility methods to deal with Solr ordered hash maps

Erik Hatcher80314c15649f
diff --git a/client/ruby/solr-ruby/lib/solr.rb b/client/ruby/solr-ruby/lib/solr.rb
index 4f39679bfc7..248a46a8c62 100755
--- a/client/ruby/solr-ruby/lib/solr.rb
+++ b/client/ruby/solr-ruby/lib/solr.rb
@@ -15,4 +15,5 @@ require 'solr/exception'
require 'solr/request'
require 'solr/connection'
require 'solr/response'
+require 'solr/util'
require 'solr/xml'
\ No newline at end of file
diff --git a/client/ruby/solr-ruby/lib/solr/response/standard.rb b/client/ruby/solr-ruby/lib/solr/response/standard.rb
index 5f1cb7e436e..dfcba34ae0b 100644
--- a/client/ruby/solr-ruby/lib/solr/response/standard.rb
+++ b/client/ruby/solr-ruby/lib/solr/response/standard.rb
@@ -39,9 +39,8 @@ class Solr::Response::Standard < Solr::Response::Ruby
def field_facets(field)
facets = []
values = @data['facet_counts']['facet_fields'][field]
- 0.upto(values.size / 2 - 1) do |i|
- n = i * 2
- facets << FacetValue.new(values[n], values[n+1])
+ Solr::Util.paired_array_each(values) do |key, value|
+ facets << FacetValue.new(key, value)
end
facets
diff --git a/client/ruby/solr-ruby/lib/solr/util.rb b/client/ruby/solr-ruby/lib/solr/util.rb
new file mode 100755
index 00000000000..a024a136933
--- /dev/null
+++ b/client/ruby/solr-ruby/lib/solr/util.rb
@@ -0,0 +1,34 @@
+# 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::Util
+ # paired_array_each([key1,value1,key2,value2]) yields twice:
+ # |key1,value1| and |key2,value2|
+ def self.paired_array_each(a, &block)
+ 0.upto(a.size / 2 - 1) do |i|
+ n = i * 2
+ yield(a[n], a[n+1])
+ end
+ end
+
+ # paired_array_to_hash([key1,value1,key2,value2]) => {key1 => value1, key2, value2}
+ def self.paired_array_to_hash(a)
+ h = {}
+
+ paired_array_each(a) do |key,value|
+ h[key] = value
+ end
+
+ h
+ end
+
+end
diff --git a/client/ruby/solr-ruby/test/unit/util_test.rb b/client/ruby/solr-ruby/test/unit/util_test.rb
new file mode 100755
index 00000000000..d7c11d2fa41
--- /dev/null
+++ b/client/ruby/solr-ruby/test/unit/util_test.rb
@@ -0,0 +1,22 @@
+# 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'
+require 'test/unit'
+
+class UtilTest < Test::Unit::TestCase
+
+ def test_paired_array_to_hash
+ assert_equal({:key1 => :value1, :key2 => :value2}, Solr::Util.paired_array_to_hash([:key1, :value1, :key2, :value2]))
+ end
+
+end