Indexing documents using JSON

In the previous section, we saw how we can add, update, and delete documents using XML. In this section, we'll see how we can perform the same operations using JSON data types.

We can send the JSON-formatted update request using Solr's update handler, by setting Content-type: application/json or text/json. There are three basic types of JSON documents that can be sent to Solr. They are:

  • A single document
  • A list of documents—an array of documents
  • A sequence of updated documents—a map type object that contains multiple commands

Adding a single document

We can add a single JSON document to Solr by posting the data to the /update/json/docs handler path. The following is an example of indexing JSON data in Solr:

$ curl 'http://localhost:8983/solr/musicCatalog/update/json/docs' -H 'Content-Type: application/json'  --data-binary '
{
    "songId":100000006,
    "songName":"Fester Skank (feat. Diztortion)",
    "artistName":"Lethal Bizzle",
    "albumArtist":"",
    "albumName":"The Official UK Top 10 Singles",
    "songDuration":2.25,
    "composer":"",
    "rating":4,
    "year":2015,
    "genre":"Hip-Hop,Rap"
}'

Adding multiple JSON documents

Similar to adding a single JSON document, we can add multiple JSON documents using a JSON array type structure that consists of JSON objects. Here is an example of sending multiple JSON documents to Solr for indexing:

$ curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/musicCatalog/update' --data-binary '
 [
  {
    "songId":100000006,
    "songName":"Fester Skank (feat. Diztortion)",
    "artistName":"Lethal Bizzle",
    "albumArtist":"",
    "albumName":"The Official UK Top 10 Singles",
    "songDuration":2.25,
    "composer":"",
    "rating":4,
    "year":2015,
    "genre":"Hip-Hop,Rap"
  },
  {
    "songId":100000007,
    "songName":"Yo (Excuse Me Miss)",
    "artistName":"Chris Brown",
    "albumArtist":"",
    "albumName":"The Official UK Top 10 Singles",
    "songDuration":3.49,
    "composer":"",
    "rating":4.5,
    "year":2006,
    "genre":"R&B,Soul"
  }
]'

A sample JSON file is provided at $SOLR_EXAMPLES/Chapter-4/sampleMusic.json, and it contains an array of objects that we can add to the Solr musicCatalog example:

$ curl 'http://localhost:8983/solr/musicCatalog/update?commit=true' --data-binary @sampleMusic.json -H 'Content-type:application/json'

Sequential JSON update commands

We can also send multiple JSON update commands, such as add, delete, and commit, within a single JSON document to Solr for indexing. The JSON update command is able to support multiple commands at once. The following is an example of sending multiple JSON commands:

curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/musicCatalog/update' --data-binary '
{
  "add": {
    "doc": {
    "songId":100000006,
    "songName":"Fester Skank (feat. Diztortion)",
    "artistName":"Lethal Bizzle",
    "albumArtist":"",
    "albumName":"The Official UK Top 10 Singles",
    "songDuration":2.25,
    "composer":"",
    "rating":4,
    "year":2015,
    "genre":"Hip-Hop,Rap"
    }
  },
  "commit": {},  
  "delete": { "query":"albumName:Justified" },  
  "delete": { "id":"100000006" }    
}'

As seen from this example, we're passing multiple commands—add, commit, and delete—within a single JSON document. The delete command can accept both the query and the ID to remove data from Solr. Here is an example of deleting multiple IDs from Solr:

{ "delete":"id1" }

Another one is as follows:

{"delete":["id1","id2"] }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.58.143