[GMG-Devel] [PATCH 35/83] Add indices for MediaEntry and MediaTag.

Alon Levy alon at pobox.com
Tue Feb 25 14:58:18 EST 2014


From: Praveen Kumar <praveen97uma at gmail.com>

---
 mediagoblin/plugins/search/indices.py | 58 +++++++++++++++++++++++++++--------
 1 file changed, 45 insertions(+), 13 deletions(-)

diff --git a/mediagoblin/plugins/search/indices.py b/mediagoblin/plugins/search/indices.py
index c329961..f7b8796 100644
--- a/mediagoblin/plugins/search/indices.py
+++ b/mediagoblin/plugins/search/indices.py
@@ -1,23 +1,55 @@
 import logging
 
-from mediagoblin.db.models import MediaEntry
-
-from mediagoblin.plugins.search import schemas
 from mediagoblin.plugins.search.base import SearchIndex
-from mediagoblin.plugins.search import registry
+
+from mediagoblin.db.models import MediaEntry
 
 _log = logging.getLogger(__name__)
 
-media_entry_search_index = SearchIndex(
-    model = MediaEntry,
-    schema = schemas.MediaEntryIndexSchema,
-)
 
+class MediaEntrySearchIndex(SearchIndex):
+    def __init__(self, model, schema, search_index_dir=None,
+        use_multiprocessing=None):
+        super(MediaEntrySearchIndex, self).__init__(
+            model=model, schema=schema, 
+            search_index_dir=search_index_dir,
+            use_multiprocessing=use_multiprocessing)
+
+    def _interpret_results(self, results, request_obj=None):
+        _log.info(type(results))
+        all_results = []
+        for result in results:
+            _log.info(result)
+            obj_id = result['id_stored']
+            obj = self.model.query.get(obj_id)
+            all_results.append({
+                'slug': obj.slug,
+                'url': obj.url_for_self(request_obj.urlgen),
+            })
+        return all_results
+
+
+class MediaTagSearchIndex(SearchIndex):
+    def __init__(self, model, schema, search_index_dir=None,
+        use_multiprocessing=None):
+        super(MediaTagSearchIndex, self).__init__(
+            model=model, schema=schema,
+            search_index_dir=search_index_dir,
+            use_multiprocessing=use_multiprocessing)
+
+    def _interpret_results(self, results, request_obj):
+        _log.info(results)
+        all_results = []
+        for result in results:
+            obj_id = result['id_stored']
+            obj = self.model.query.get(obj_id)
+            media_entry_obj = MediaEntry.query.get(obj.media_entry)
+            all_results.append({
+                'slug': media_entry_obj.slug,
+                'url': media_entry_obj.url_for_self(request_obj.urlgen)
+            })
+
+        return all_results
 
-def register_indices():
-    registry.IndexRegistry.register(media_entry_search_index)
-    _log.info("Registered %(index_name)s index for %(model_name)s"%({
-        'index_name': media_entry_search_index.__class__.__name__,
-        'model_name': MediaEntry.__class__.__name__}))
 
 
-- 
1.8.5.3



More information about the devel mailing list