How to update multiple documents in Solr with JSON?

burnersk picture burnersk · Nov 27, 2013 · Viewed 19.3k times · Source

How to update multiple documents in Solr 4.5.1 with 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."
      }
    ]
  }
}

Answer

MatsLindh picture MatsLindh · Nov 27, 2013

Solr expects one "add"-key in the JSON-structure for each document (which might seem weird, if you think about the original meaning of the key in the object), since it maps directly to the XML format when doing the indexing - and this way you can have metadata for each document by itself.

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

.. works. I think allowing an array as the element referenced by "add" would make more sense, but I haven't dug further into the source or know the reasoning behind this.