Commits

Thomas Wanschik  committed 5c8d3d5

added a search function, and renamed search_manager kwarg to search_index

  • Participants
  • Parent commits c05e43c

Comments (0)

Files changed (3)

File search/__init__.py

         # to bubble up.
         import_module("%s.search_indexes" % app)
 
-def register(model, mgr_name, fields_to_index, indexer=None,
-    splitter=default_splitter, relation_index=True, integrate='*', filters={},
-    language=site_language, **kwargs):
+def register(model, fields_to_index, search_index='search_index',
+    indexer=None, splitter=default_splitter, relation_index=True, integrate='*',
+    filters={}, language=site_language, **kwargs):
 
     """
     Add a search manager to the model.
     if not hasattr(model, '_meta'):
         raise AttributeError('The model being registered must derive from Model.')
 
-    if hasattr(model, mgr_name):
+    if hasattr(model, search_index):
         raise AttributeError('The model being registered already defines a'
-            ' property called %s.' % mgr_name)
+            ' property called %s.' % search_index)
 
-    model.add_to_class(mgr_name, SearchManager(fields_to_index, indexer, splitter,
-        relation_index, integrate, filters, language, **kwargs))
+    model.add_to_class(search_index, SearchManager(fields_to_index, indexer,
+        splitter, relation_index, integrate, filters, language, **kwargs))
 
     install_index_model(model)

File search/core.py

     # TODO: add keys_only query
 #    def values(self, fields):
 #        pass
+
+def search(model, query, language=settings.LANGUAGE_CODE,
+        search_index='search_index'):
+    return getattr(model, search_index).search(query, language)

File search/views.py

 from django.conf import settings
 from djangotoolbox.http import JSONResponse
 
-def live_search_results(request, model, search_manager, limit=30,
+def live_search_results(request, model, search_index='search_index', limit=30,
         result_item_formatting=None, query_converter=None,
         converter=None, redirect=False):
     """
             limit = limit_override
     except:
         pass
-    search_manager = getattr(model, search_manager)
+    search_index = getattr(model, search_index)
     language = getattr(request, 'LANGUAGE_CODE', settings.LANGUAGE_CODE)
-    results = search_manager.search(query, language=language)
+    results = search_index.search(query, language=language)
     if query_converter:
         results = query_converter(request, results)
     results = results[:limit]
         if result_item_formatting:
             entry = result_item_formatting(item)
         else:
-            value = getattr(item, search_manager.fields_to_index[0])
+            value = getattr(item, search_index.fields_to_index[0])
             entry = {'value': force_escape(value), 'result': value}
         if 'data' not in entry:
             entry['data'] = {}