DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • API and Security: From IT to Cyber
  • Parent Document Retrieval (PDR): Useful Technique in RAG
  • Automatic 1111: Adding Custom APIs

Trending

  • Implementing the Planning Pattern With Java Enterprise and LangChain4j
  • Grok AI API Tutorial: Chat, Image, Video, Tool Calling, and Web Search
  • Getting Started With GitHub Copilot CLI for Coding Tasks
  • Conversational Risk Accumulation: Stateful Guardrails Beyond Single-Turn LLM Checks
  1. DZone
  2. Data Engineering
  3. Data
  4. Read-Only Collections in Solr

Read-Only Collections in Solr

Learn more about read-only collections in Solr.

By 
Rafał Kuć user avatar
Rafał Kuć
·
Oct. 10, 19 · Presentation
Likes (1)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

Read-only collection

An actual image of a ''read-only'' collection.

Have you ever wonder how to avoid accidental, or on purpose, modification of collection data? Of course, we could reject access as one of the possible solutions, but it is not always possible. In today's blog post, we will look into how to easily protect your collection from accidental modifications by setting it to read-only.

Default Behavior

When we create the collection, either via the API or using the script, it is created in the all-access mode — you can both read data from it and write data to it. Let's create a collection using the following command:

$ bin/solr create_collection -c readonly


We are working with Solr 8.2, so Solr will use the configuration called  _default that is available in SolrCloud and used when no collection is specified. It allows us to index a simple document using the following command:

$ curl -XPOST -H 'Content-Type:application/json' 'http://localhost:8983/solr/readonly/update' -d '[
  {
   "id" : 1,
   "name" : "Test document"
  }
 ]'


In response, Solr will tell us that indexing went well; the document was processed and indexed properly:

Read-Only Collection

If we would like our created collection to be read-only so that we can't write data to it, we would have to set the readOnly attribute of that collection to value. To do that, we will use the Collections API with the following command:

$ curl -XGET 'localhost:8983/solr/admin/collections?action=MODIFYCOLLECTION&collection=readonly&readOnly=true'


Response from Solr after the above command should look as follows:

{
   "responseHeader":{
     "status":0,
     "QTime":846},
   "success":{
     "192.168.0.20:8983_solr":{
       "responseHeader":{
         "status":0,
         "QTime":724}}}}


Let's test if that read-only mode works and how it works.

Let's start with simple indexing by using the following command:

$ curl -XPOST -H 'Content-Type:application/json' 'http://localhost:8983/solr/readonly/update' -d '[
  {
   "id" : 2,
   "name" : "Second test document"
  }
 ]


In this case, Solr will return an error similar to the following one:

{
   "responseHeader":{
     "status":403,
     "QTime":1},
   "error":{
     "metadata":[
       "error-class","org.apache.solr.common.SolrException",
       "root-error-class","org.apache.solr.common.SolrException"],
     "msg":"Collection readonly is read-only.",
     "code":403}}


What if we would like to use the Schema API to add a new field? Let's test it by using the following command:

$ curl -XPOST -H 'Content-type:application/json' 'http://localhost:8983/solr/readonly/schema' --data-binary '{
  "add-field" : {
   "name" : "test",
   "type" : "string",
   "stored" : true,
   "indexed" : true
  }
 }'


In this case, the operation is successful:

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


Enabling Modifications

In order to turn off collection read-only mode, and once again, allow modifications, we need to use the Collections API one again and set the readOnly attribute of the collection tofalse:

$ curl -XGET 'localhost:8983/solr/admin/collections?action=MODIFYCOLLECTION&collection=readonly&readOnly=false'


Summary

As we can see in the above example with Solr 8.1, we witnessed a simple yet useful method of protection of the documents inside the collection. If our application uses a collection that can be set as read-only, it is really worth considering.

By using read-only collections, we can save ourselves potential problems of accidental data write in case of errors on our application side. With authorization and authentication available in SolrCloud, we can also limit access to the Collections API and turn off the possibility of setting the read-only mode to unauthorized persons, adding yet another layer of security to our cluster. Think about the possibilities!

Further Reading

Singleton List Showdown: Collections::singletonList Vs. List::of

Command (computing) API Data (computing) Apache Solr application Document Attribute (computing) authentication

Published at DZone with permission of Rafał Kuć. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • API and Security: From IT to Cyber
  • Parent Document Retrieval (PDR): Useful Technique in RAG
  • Automatic 1111: Adding Custom APIs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook