Repository: ckan/ckan

Issue:
ckan.simple_search setting being ignored
### CKAN Version if known (or site URL)

2.5.2
### Please describe the expected behaviour

When `ckan.simple_search` is set to `true` or `1`, CKAN should start up without complaining about a lost connection to SOLR.
### Please describe the actual behaviour

CKAN logs errors regarding being unable to connect to the SOLR server:

```
2016-08-17 19:01:43,150 WARNI [ckan.lib.search] Problems were found while connecting to the SOLR server
2016-08-17 19:01:43,447 ERROR [ckan.lib.search.common] [Errno 111] Connection refused
Traceback (most recent call last):
  File "/usr/lib/ckan/default/src/ckan/ckan/lib/search/common.py", line 51, in is_available
    conn.query("*:*", rows=1)
  File "/usr/local/lib/python2.7/dist-packages/solr/core.py", line 703, in query
    return self.select(*args, **params)
  File "/usr/local/lib/python2.7/dist-packages/solr/core.py", line 798, in __call__
    xml = self.raw(**params)
  File "/usr/local/lib/python2.7/dist-packages/solr/core.py", line 823, in raw
    rsp = conn._post(self.selector, request, conn.form_headers)
  File "/usr/local/lib/python2.7/dist-packages/solr/core.py", line 646, in _post
    self._reconnect()
  File "/usr/local/lib/python2.7/dist-packages/solr/core.py", line 625, in _reconnect
    self.conn.connect()
  File "/usr/lib/python2.7/httplib.py", line 778, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 111] Connection refused
2016-08-17 19:01:43,448 WARNI [ckan.lib.search] Problems were found while connecting to the SOLR server

```
### What steps can be taken to reproduce the issue?

Consult [the documentation](http://docs.ckan.org/en/latest/maintaining/configuration.html) regarding the `ckan.simple_search` option:

> ckan.simple_search
> Example:
> 
> ckan.simple_search = true
> Default value: false
> 
> Switching this on tells CKAN search functionality to just query the database, (rather than using Solr). In this setup, search is crude and limited, e.g. no full-text search, no faceting, etc. However, this might be very useful for getting up and running quickly with CKAN.

Set the `ckan.simple_search` option to true in the configuration ini file, and comment out the `solr`url` setting, then restart CKAN and observe the logged output.



Discussion:

Resolving patch:
diff --git a/ckan/lib/search/__init__.py b/ckan/lib/search/__init__.py
--- a/ckan/lib/search/__init__.py
+++ b/ckan/lib/search/__init__.py
@@ -7,7 +7,6 @@
 import xml.dom.minidom
 import urllib2
 
-from ckan.common import config
 from paste.deploy.converters import asbool
 
 import ckan.model as model
@@ -23,7 +22,6 @@
 log = logging.getLogger(__name__)
 
 
-
 def text_traceback():
     with warnings.catch_warnings():
         warnings.simplefilter("ignore")
@@ -32,7 +30,6 @@ def text_traceback():
         ).strip()
     return res
 
-SIMPLE_SEARCH = asbool(config.get('ckan.simple_search', False))
 
 SUPPORTED_SCHEMA_VERSIONS = ['2.3']
 
@@ -60,11 +57,6 @@ def text_traceback():
 
 SOLR_SCHEMA_FILE_OFFSET = '/admin/file/?file=schema.xml'
 
-if SIMPLE_SEARCH:
-    import sql as sql
-    _INDICES['package'] = NoopSearchIndex
-    _QUERIES['package'] = sql.PackageSearchQuery
-
 
 def _normalize_type(_type):
     if isinstance(_type, model.domain_object.DomainObject):
@@ -219,6 +211,7 @@ def commit():
     package_index.commit()
     log.info('Commited pending changes on the search index')
 
+
 def check():
     package_query = query_for(model.Package)
 
@@ -249,10 +242,9 @@ def clear(package_reference):
 
 
 def clear_all():
-    if not SIMPLE_SEARCH:
-        package_index = index_for(model.Package)
-        log.debug("Clearing search index...")
-        package_index.clear()
+    package_index = index_for(model.Package)
+    log.debug("Clearing search index...")
+    package_index.clear()
 
 
 def check_solr_schema_version(schema_file=None):
@@ -276,11 +268,6 @@ def check_solr_schema_version(schema_file=None):
                       be only used for testing purposes (Default is None)
     '''
 
-
-    if SIMPLE_SEARCH:
-        # Not using the SOLR search backend
-        return False
-
     if not is_available():
         # Something is wrong with the SOLR server
         log.warn('Problems were found while connecting to the SOLR server')
diff --git a/ckan/lib/search/sql.py b/ckan/lib/search/sql.py
deleted file mode 100644
--- a/ckan/lib/search/sql.py
+++ /dev/null
@@ -1,42 +0,0 @@
-# encoding: utf-8
-
-from sqlalchemy import or_
-from ckan.lib.search.query import SearchQuery
-import ckan.model as model
-
-class PackageSearchQuery(SearchQuery):
-    def get_all_entity_ids(self, max_results=100):
-        """
-        Return a list of the IDs of all indexed packages.
-        """
-        # could make this a pure sql query which would be much more efficient!
-        q = model.Session.query(model.Package).filter_by(state='active').limit(max_results)
-
-        return [r.id for r in q]
-
-    def run(self, query):
-        assert isinstance(query, dict)
-        # no support for faceting atm
-        self.facets = {}
-        limit = min(1000, int(query.get('rows', 10)))
-
-        q = query.get('q')
-        ourq = model.Session.query(model.Package.id).filter_by(state='active')
-
-        def makelike(field):
-            _attr = getattr(model.Package, field)
-            return _attr.ilike('%' + term + '%')
-        if q and q not in ('""', "''", '*:*'):
-            terms = q.split()
-            # TODO: tags ...?
-            fields = ['name', 'title', 'notes']
-            for term in terms:
-                args = [makelike(field) for field in fields]
-                subq = or_(*args)
-                ourq = ourq.filter(subq)
-        self.count = ourq.count()
-        ourq = ourq.limit(limit)
-        self.results = [{'id': r[0]} for r in ourq.all()]
-
-        return {'results': self.results, 'count': self.count}
-


Test patch:
diff --git a/ckan/tests/legacy/lib/test_simple_search.py b/ckan/tests/legacy/lib/test_simple_search.py
deleted file mode 100644
--- a/ckan/tests/legacy/lib/test_simple_search.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# encoding: utf-8
-
-from nose.tools import assert_equal
-
-from ckan import model
-from ckan.lib.create_test_data import CreateTestData
-from ckan.lib.search.sql import PackageSearchQuery
-
-class TestSimpleSearch:
-    @classmethod
-    def setup_class(cls):
-        CreateTestData.create()
-    
-    @classmethod
-    def teardown_class(cls):
-        model.repo.rebuild_db()
-
-    def test_get_all_entity_ids(self):
-        ids = PackageSearchQuery().get_all_entity_ids()
-        anna = model.Package.by_name(u'annakarenina')
-        assert anna.id in ids
-        assert len(ids) >= 2, len(ids)
-        
-    def test_run_query_basic(self):
-        res = PackageSearchQuery().run({'q':'annakarenina'})
-        anna = model.Package.by_name(u'annakarenina')
-        assert_equal(res, {'results': [{'id': anna.id}], 'count': 1})
-
-    def test_run_query_home(self):
-        # This is the query from the CKAN home page
-        res = PackageSearchQuery().run({'q': '*:*'})
-        assert res['count'] >= 2, res['count']
-
-    def test_run_query_all(self):
-        # This is the default query from the search page
-        res = PackageSearchQuery().run({'q': u''})
-        assert res['count'] >= 2, res['count']