1

I am using pysolr library and I want to check if solr is running or no and to switch between solr and other search engine. I found similar question and I tried many suggestions and didn't work. How to know whether solr server is running or not How do i check cassandra and solr is up? Python code to check if service is running or not.?

Ana
  • 29
  • 9
  • https://stackoverflow.com/questions/6744747/how-to-find-whether-solr-server-is-running-or-not – Abhijit Bashetti Apr 21 '20 at 04:54
  • You'll have to explain why either of those examples didn't work - but in your case - how about just try to make the query to Solr with a low timeout, and if no answer arrives within 1 - 2s, mark Solr as down and query the other search service. Each 15m reset the mark and retry the connection. – MatsLindh Apr 21 '20 at 06:31
  • I tried that : pysolr.Solr('http://localhost:8983/solr/', timeout=10) and I tried :solr=pysolr.Solr('http://localhost:8983/solr/catalogue',timeout=10) solr.ping() it shows an error : AttributeError: 'Solr' object has no attribute 'ping' – Ana Apr 21 '20 at 10:59
  • 1
    The `ping` method [was added in pysolr 3.9.0](https://github.com/django-haystack/pysolr/releases/tag/v3.9.0). Upgrade your pysolr dependency. – MatsLindh Apr 21 '20 at 13:24

2 Answers2

2

A clean way is using pysolr

import pysolr

# Create a client instance. The timeout and authentication options are not required.
solr = pysolr.Solr('http://localhost:8983/solr/')

# Do a health check.
ping = solr.ping()
resp = json.loads(ping)

if resp.get('status') == 'OK':
   print('success')
  
freedev
  • 25,946
  • 8
  • 108
  • 125
0

if the code below error(connection refused), solr is not working. gettingstarted is an example collective name in the link.

from urllib.request import *
connection = urlopen('http://localhost:8983/solr/gettingstarted/select?q=mebus&wt=python')
response = eval(connection.read())
mtbenj
  • 11
  • 2