Check if Solr Core already exists from Command Line
Asked Answered
S

4

6

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

Significance answered 15/11, 2016 at 7:59 Comment(1)
if you know the port and core name you can issue a simple query to the core (i.e. q=*:*&rows=0) and see if it responds.Solenoid
I
8

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>
Isley answered 15/11, 2016 at 9:49 Comment(0)
M
3

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'
Massive answered 24/4, 2021 at 7:49 Comment(0)
F
2

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=*:*"
Frayne answered 28/7, 2021 at 3:21 Comment(0)
C
0

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

Collegian answered 18/5, 2022 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.