Search code examples
powershellsolr

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


Solution

  • 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>