How to update multiple documents in Solr using JSON?

How to update multiple documents in Solr 4.5.1 using JSON? I tried this, but it does not work:

POST /solr/mycore/update/json :

 { "commit": {}, "add": { "overwrite": true, "doc": [{ "thumbnail": "/images/404.png", "url": "/404.html?1", "id": "demo:/404.html?1", "channel": "demo", "display_name": "One entry", "description": "One entry is not enough." }, { "thumbnail": "/images/404.png", "url": "/404.html?2", "id": "demo:/404.html?2", "channel": "demo", "display_name": "Another entry", "description": "Another entry is required." } ] } } 
+7
json solr solr4
source share
4 answers

Solr expects a single β€œadded” key to be added in the JSON structure for each document (which may seem strange if you think about the original key value in the object), because it is displayed directly in XML format when performing indexing - and thus You can have metadata for each document yourself.

 { "commit": {}, "add": { "doc": { "id": "321321", "name": "barfoo" } }, "add": { "doc": { "id": "123123", "name": "Foobar" } } } 

.. working. I think that using the array as the element referenced by the β€œadd” will make more sense, but I have not yet delved into the source or did not know the reasons underlying it.

+4
source share

I understand that (at least) from versions 4.0 and older from solr, this is fixed. Take a look at http://wiki.apache.org/solr/UpdateJSON .

B. / exampledocs / books.json is an example json file with multiple documents.

 [ { "id" : "978-0641723445", "cat" : ["book","hardcover"], "name" : "The Lightning Thief", "author" : "Rick Riordan", "series_t" : "Percy Jackson and the Olympians", "sequence_i" : 1, "genre_s" : "fantasy", "inStock" : true, "price" : 12.50, "pages_i" : 384 } , { "id" : "978-1423103349", "cat" : ["book","paperback"], "name" : "The Sea of Monsters", "author" : "Rick Riordan", "series_t" : "Percy Jackson and the Olympians", "sequence_i" : 2, "genre_s" : "fantasy", "inStock" : true, "price" : 6.49, "pages_i" : 304 }, ... ] 

While @fiskfisk's answer is still valid JSON, it is not easy to serialize from a data structure. It's one.

+5
source share

elachell is correct that the array format will work if you just add documents with default settings. Unfortunately, this will not work if, for example, you need to add a custom pulse to some documents or change the rewrite option. Then you need to use the full structure of the object with the β€œadd” key for each of them, which, as they pointed out, makes it unpleasantly annoying to try to serialize from most languages ​​that do not allow the same key more than once per object:

 { "commit": {}, "add": { "doc": { "id": "321321", "name": "barfoo" }, "boost": 2.0 }, "add": { "doc": { "id": "123123", "name": "Foobar" }, "boost": 1.5, "overwrite": false } 

}

+1
source share

Another option if you are on Solr 4.10 or later is to use a custom JSON structure and tell Solr how to index it (not sure how to add boosts using this method, but this is a good option if you already have struct data in JSON and don't want to convert it to Solr format). Here's the Solr documentation for this option:

https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-TransformingandIndexingCustomJSON

0
source share

All Articles