'Update existing documents with nested fields in Solr

I Have an index that already contains documents. wish to populate a field in these documents with nested value. For example; let's say I have a document that looks like this:

  {
    "id":"9",
    "title":"Example document",
    "description":"Lorem Ipsum ..."
  }

Now, what I wish to do is add a field so that the document would look like this:

  {
    "id":"9",
    "title":"Example document",
    "description":"Lorem Ipsum ..."
    "attributes" : [
     {
         "id": "1",
         "name": "color",
         "value": "red"
     },
     {
          "id": "2",
          "name": "size",
          "value": "20"
     }
    ]
  }

I've come across How to write nested schema.xml in solr?, and I tried to use this schema and doing atomic update with new values using "add" to the parent field, but I've come across an error.

Here's an XML example of the update process:

<add>
    <doc>
        <field name="id">9</field>
        <field name="attributes" update="add">
            <field name="id">1</field>
            <field name="name">color/field>
            <field name="value">red</field>
        </field>
    </doc>
</add>

Here's the error:

java.lang.NullPointerException
    at org.apache.solr.update.processor.FieldMutatingUpdateProcessor.processAdd(FieldMutatingUpdateProcessor.java:110)

    at org.apache.solr.update.processor.UpdateRequestProcessor.processAdd(UpdateRequestProcessor.java:55)

    at org.apache.solr.update.processor.AbstractDefaultValueUpdateProcessorFactory$DefaultValueUpdateProcessor.processAdd(AbstractDefaultValueUpdateProcessorFactory.java:92)

    at org.apache.solr.handler.loader.XMLLoader.processUpdate(XMLLoader.java:263)

    at org.apache.solr.handler.loader.XMLLoader.load(XMLLoader.java:190)     

    at org.apache.solr.handler.UpdateRequestHandler$1.load(UpdateRequestHandler.java:97)

    at org.apache.solr.handler.ContentStreamHandlerBase.handleRequestBody(ContentStreamHandlerBase.java:82)

    at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:216)

    at org.apache.solr.core.SolrCore.execute(SolrCore.java:2637)                                                                                                        
    at org.apache.solr.servlet.HttpSolrCall.execute(HttpSolrCall.java:794)   

    at org.apache.solr.servlet.HttpSolrCall.call(HttpSolrCall.java:567)      

    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:427)

    at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:357)

    at org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:201)

    at org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)

    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1435)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1350)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:191)
    at org.eclipse.jetty.server.handler.InetAccessHandler.handle(InetAccessHandler.java:177)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:322)
    at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:763)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    at org.eclipse.jetty.server.Server.handle(Server.java:516)
    at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388)
    at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
    at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:383)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:882)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1036)

So, what am I doing wrong?



Solution 1:[1]

It would be something like

  {
        "id":"9",
        "attributes" :{"add": [
         {
             "id": "1",
             "name": "color",
             "value": "red"
         },
         {
              "id": "2",
              "name": "size",
              "value": "20"
         }
        ]
       }
   }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Tawfiq