Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Solr Core already exists from Command Line

Basically I am writing a Powershell script that will create a new core if one doesn't exist, update the schema.xml , restart the core and run the data import utility.

One solution is doing a

solr create -c products

That'll throw an error if it already exists, but it's not an elegant solution

like image 482
Ali Kareem Raja Avatar asked Dec 23 '22 23:12

Ali Kareem Raja


2 Answers

The easiest solution would be to check a status of a core

http://localhost:8983/solr/admin/cores?action=STATUS&core=core0

Where core0 - name of the core

If core doesn't exist you will get

 <response>
    <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">0</int></lst>
    <lst name="initFailures"/>
    <lst name="status"><lst name="core0"/></lst>
  </response>

If core exists, you will get more info (just an example)

<response>
   <lst name="responseHeader">
      <int name="status">0</int>
      <int name="QTime">8</int>
   </lst>
   <lst name="initFailures" />
   <lst name="status">
      <lst name="core0">
         <str name="name">core0</str>
         <str name="instanceDir">/var/lib/solr/core0</str>
         <str name="dataDir">/var/lib/solr/core0/data/</str>
         <str name="config">solrconfig.xml</str>
         <str name="schema">schema.xml</str>
         <date name="startTime">2016-11-11T15:31:38.250Z</date>
         <long name="uptime">324812972</long>
         <lst name="index">
            <int name="numDocs">6954</int>
            <int name="maxDoc">6954</int>
            <int name="deletedDocs">0</int>
            <long name="indexHeapUsageBytes">-1</long>
            <long name="version">12</long>
            <int name="segmentCount">1</int>
            <bool name="current">true</bool>
            <bool name="hasDeletions">false</bool>
            <str name="directory">org.apache.lucene.store.NRTCachingDirectory:NRTCachingDirectory(MMapDirectory@/var/lib/solr/feature/data/index lockFactory=org.apache.lucene.store.NativeFSLockFactory@a77f582; maxCacheMB=48.0 maxMergeSizeMB=4.0)</str>
            <str name="segmentsFile">segments_3</str>
            <long name="segmentsFileSizeInBytes">165</long>
            <lst name="userData">
               <str name="commitTimeMSec">1478791558730</str>
            </lst>
            <date name="lastModified">2016-11-10T15:25:58.730Z</date>
            <long name="sizeInBytes">2605023</long>
            <str name="size">2.48 MB</str>
         </lst>
      </lst>
   </lst>
</response>
like image 129
Oyeme Avatar answered Jan 14 '23 14:01

Oyeme


Those of you coming from the future looking for the equivalent in 8.8 as it seems that the above endpoint no longer works (not sure why... maybe it's url encoding issue with the url I pass into curl but meh). You can also use the v2 API.

curl -sI localhost:8983/api/cores/<core_name> | grep "HTTP/1.1 200 OK" will give you 0 exit code if it exists, and 1 exit code if it does not. Using HTTP response code is simpler compared to parsing JSON.

So a one liner could look like

[[ ! $(curl -sI $SOLR_API/cores/new_core | grep "200 OK") ]] && sudo su solr -c 'solr create -c new_core'
like image 39
Daryl Teo Avatar answered Jan 14 '23 12:01

Daryl Teo