Index handler with JSON

Solr also supports JSON-formatted documents to be indexed. Let's look at a simple example of indexing just one document. To add documents in JSON format on our gettingstarted collection, we need to use the following URLhttp://localhost:8983/solr/gettingstarted/update/json/docs.

Open Postman and create a new request with this URL. See the following screenshot for clarity:

As you can see, we have set Content-Type to application/json. Now click on the Body tab and put the JSON content as follows:

Once you are done, execute the request and it will run to success with the following response:

{
"responseHeader":{
"status":0,
"QTime":8}
}

The status value set to 0 means it is a success. If it is a non-zero value, then it means there is a failure. In order to validate this, go to the Solr admin console and execute the query as we did earlier:

 Once you execute the query, you should see that the index also contains the new document that we added using the JSON format.

In order to add multiple documents, you have to pass a JSON array instead of a single document:

[
{
"title": "Red sails to Capri",
"author": "Ann Weil"
},
{
"title": "Five Point Someone",
"author": "Chetan Bhagat"
}
]

The URL has to be http://localhost:8983/solr/gettingstarted/update/, as shown in the following screenshot:

You will get a success response with status set to 0 once you execute the request. In order to validate, navigate to the Solr admin panel and execute the query:

Your response will now have the two documents that you have added and you should see the following documents in the response:

{
"responseHeader":{
"zkConnected":true,
"status":0,
"QTime":35,
"params":{
"q":"*:*",
"_":"1514453950466"}},
"response":{"numFound":4,"start":0,"maxScore":1.0,"docs":[
{
"title":["Red sails to Capri"],
"author":["Ann Weil"],
"id":"3813c196-0341-4708-8b05-f47225eacb12",
"title_str":["Red sails to Capri"],
"author_str":["Ann Weil"],
"_version_":1588021679301328896},
{
"title":["Harry Potter and Philosophers Stone"],
"author":["J.K. Rowling"],
"id":"dcbfd369-2a4b-4e9d-9713-1af373849438",
"title_str":["Harry Potter and Philosophers Stone"],
"author_str":["J.K. Rowling"],
"_version_":1587892903981613056},
{
"title":["Jonathan Livingston Seagull"],
"author":["Richard Bach"],
"id":"936df16b-f6fe-4147-874d-ad4525bf7e9f",
"title_str":["Jonathan Livingston Seagull"],
"author_str":["Richard Bach"],
"_version_":1588019816078245888},
{
"title":["Five Point Someone"],
"author":["Chetan Bhagat"],
"id":"68fb69cd-ad31-441b-be02-eb3339afdd42",
"title_str":["Five Point Someone"],
"author_str":["Chetan Bhagat"],
"_version_":1588021679292940288}]
}}

You can also add, update, or delete documents in a single operation. To do this, let's try to delete some documents and add a new document:

{
"add": {
"doc": {
"title": "Liferay Beginner's Guide",
"author": "Samir Bhatt"
}
},
"delete": { "id":"3813c196-0341-4708-8b05-f47225eacb12" },
"delete": { "query":"author:rowling" }
}

Here, we are executing both add and delete operations in a single request. We have added a new book, deleted a book with id, and deleted a book with query where the author is rowling.

The request in Postman will look something like this:

In order to execute this, hit the Send button. You will get a success response. In this way, we can add and delete documents in a single operation.

..................Content has been hidden....................

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