Yonik Seeley is the creator of Apache Solr and the Chief Open Source Architect and Co-Founder at Lucid Imagination, a company dedicated to development and support of Lucene/Solr. He's also an Apache Lucene/Solr PMC member and committer. Yonik has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Indexing JSON in Solr 3.1

01.13.2011
| 13088 views |
  • submit to reddit

Solr has been able to produce JSON results for a long time, by adding wt=json to any query. A new capability has recently been added to allow indexing in JSON, as well as issuing other update commands such as deletes and commits.

All of the functionality that was available through XML update commands can now be given in JSON.
For example, you can index a document like so:

$ curl http://localhost:8983/solr/update/json -H 'Content-type:application/json' -d '
{
"add": {
"doc": {
"id" : "ISBN:978-0641723445",
"title" : "The Lightning Thief"
"author" : "Rick Riordan",
"series_t" : "Percy Jackson and the Olympians",
"cat" : ["book","hardcover"],
"genre_s" : "fantasy",
"pages_i" : 384
"price" : 12.50,
"inStock" : true,
"popularity" : 10
}
}
}'

Of course, if you want the doc to be visible, you must do a commit. This could have been done by adding a commit=true parameter to the URL in the previous command, or we could have added a commit command within the JSON itself. This time we’ll issue a separate commit command.

curl "http://localhost:8983/solr/update/json?commit=true"

 

And now, we can query the Solr index and verify the document has been correctly added (requesting the results in JSON of course!)

http://localhost:8983/solr/select?wt=json&indent=true&q=title:lightning

There’s more documentation on the Solr Wiki.
To use this functionality, you’ll need to use LucidWorks Enterprise (our commercial version of Solr), or a recent Solr 3.1-dev or 4.0-dev nightly build.

Very handy!

Published at DZone with permission of its author, Yonik Seeley.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)