Search code examples
solrsolrj

How to flatten object from String to SolrInputDocument


I wish to know if it is possible to post JSON/XML strins from SolrJ.

I am currently receiving documents from some messaging service as JSON strings.

One such document is as below:

{
  "id":"1","name":"Item 1","price":10,
  "producer":{"id":"10","name":"Prod 1",
    "address": {"id":"100","city":"City 1","zipcode":95000}
  }
}

Do I need to convert this string to SolrInputDocument for serving it to Solr?

I am currently using SolrJ and see no API for directly serving a JSON to Solr.

( I see post.jar shipped with Solr is able to post json/xml documents directly to Solr and I would like the same behavior via a SolrJ API )

If there is no such API, how do I convert the string to SolrInputDocument?


Solution

  • You don't need to convert it to SolrInputDocument. See http://wiki.apache.org/solr/Solrj#Directly_adding_POJOs_to_Solr

    But Solr does not allow nested fields. You need to flatten your JSON i.e. keep only scalars, and arrays (or lists) as multi-valued fields only. You may convert it like this:

    {
      "id":"1",
      "name":"Item 1",
      "price":10,
      "producer_id":"10",
      "producer_name":"Prod 1",
      "producer_address_id":"100",
      "producer_address_city": "city1",
      "producer_address_zipcode":95000
    }
    

    or you can also keep the producer address in a multi-valued field like this:

    {
      "id":"1",
      "name":"Item 1",
      "price":10,
      "producer_id":"10",
      "producer_name":"Prod 1",
      "producer_address":["100", "city1", "95000"]
    }
    

    Which one you should choose depends on your query requirements.