Permalink
Browse files

Fix JSON format so it is accepted by Solr

It turns out that Solr's JSON format is either too complex (duplicate
keys) or too simple (removing the ability for session based boosts or
commitwithin). I've opted to implement the simple. I plan to raise an
issue to get the format sorted out.
  • Loading branch information...
1 parent b422407 commit 31377a1faf570a25fe3686b3f22ad285a6a1c647 @mootpointer committed Feb 28, 2012
Showing with 16 additions and 22 deletions.
  1. +9 −12 lib/rsolr/json.rb
  2. +7 −10 spec/api/json_spec.rb
View
@@ -10,20 +10,17 @@ def build &block
def add data, add_attrs = nil, &block
data = [data] unless data.is_a?(Array)
build do |json|
- json.add do
- json.doc data do |doc|
- doc = RSolr::JSON::Document.new(doc) if doc.respond_to?(:each_pair)
- yield doc if block_given?
- # json << doc.attrs if doc.attrs
- doc.fields.each do |f|
- if f.attrs.keys.length > 1
- json[f.name] = f.attrs.merge(:value => f.value)
- else
- json[f.name] = f.value
- end
+ json.add data do |doc|
+ doc = RSolr::JSON::Document.new(doc) if doc.respond_to?(:each_pair)
+ yield doc if block_given?
+ doc.attrs.map{|k,v| json[k] = v} if doc.attrs
+ doc.fields.each do |f|
+ if f.attrs.keys.length > 1
+ json[f.name] = f.attrs.merge(:value => f.value)
+ else
+ json[f.name] = f.value
end
end
- add_attrs.map{|k,v| json[k] = v} if add_attrs # Done down here to game Jsonify's append logic.
end
json.compile!
end
View
@@ -12,16 +12,13 @@
it 'should yield a Document object when #add is called with a block' do
documents = [{:id=>1, :name=>'sam', :cat=>['cat 1', 'cat 2']}]
- add_attrs = {:boost=>200.00}
- result = generator.add(documents, add_attrs) do |doc|
+ result = generator.add(documents) do |doc|
doc.field_by_name(:name).attrs[:boost] = 10
doc.fields.size.should == 3
doc.fields_by_name(:cat).size.should == 1
end
- puts result
- result.should be_json_eql('["cat 1", "cat 2"]').at_path('add/doc/0/cat')
- result.should be_json_eql('200.0').at_path('add/boost')
- result.should be_json_eql('10').at_path('add/doc/0/name/boost')
+ result.should be_json_eql('["cat 1", "cat 2"]').at_path('add/0/cat')
+ result.should be_json_eql('10').at_path('add/0/name/boost')
end
# add a single hash ("doc")
@@ -32,8 +29,8 @@
}
result = generator.add(data)
result.should have_json_path("add")
- result.should have_json_size(1).at_path("add/doc")
- result.should be_json_eql(data.to_json).at_path('add/doc/0')
+ result.should have_json_size(1).at_path("add")
+ result.should be_json_eql(data.to_json).at_path('add/0')
end
# add an array of hashes
@@ -49,7 +46,7 @@
}
]
message = generator.add(data)
- message.should have_json_size(2).at_path("add/doc")
+ message.should have_json_size(2).at_path("add")
end
end
@@ -59,7 +56,7 @@
:name => ['matt1', 'matt2']
}
result = generator.add(data)
- result.should be_json_eql('["matt1","matt2"]').at_path('add/doc/0/name')
+ result.should be_json_eql('["matt1","matt2"]').at_path('add/0/name')
end
end

0 comments on commit 31377a1

Please sign in to comment.