Ali Kareem Raja
Ali Kareem Raja

Reputation: 576

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

Upvotes: 6

Views: 7200

Answers (4)

2652763
2652763

Reputation: 513

This will output 200 if the core exists, 404 if not.

curl -sIN "http://localhost:8983/solr/images/admin/ping" \
  | head -n 1 | awk '{print $2}'

From the Wodby Solr Docker image, along with other useful snippets, here: https://github.com/wodby/solr/blob/master/bin/actions.mk

Upvotes: 0

Kirill Roskolii
Kirill Roskolii

Reputation: 101

Accepted answer is outdated and not really suitable for scripted checks.

The only reliable way to check if the core exists I found to date is to try to search in it or reload it:

curl --fail "http://localhost:8983/solr/admin/cores?action=reload&core=${SOLR_CORE}"
# or
curl --fail "http://localhost:8983/solr/${SOLR_CORE}/select?q=*:*"

Upvotes: 2

Daryl Teo
Daryl Teo

Reputation: 5495

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'

Upvotes: 3

Oyeme
Oyeme

Reputation: 11225

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>

Upvotes: 8

Related Questions