[GMG-Devel] [PATCH 31/83] Refactor SearchIndex and add method to update an index.

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


From: Praveen Kumar <praveen97uma at gmail.com>

---
 mediagoblin/plugins/search/base.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mediagoblin/plugins/search/base.py b/mediagoblin/plugins/search/base.py
index 9172d26..2b6e2b6 100644
--- a/mediagoblin/plugins/search/base.py
+++ b/mediagoblin/plugins/search/base.py
@@ -131,7 +131,7 @@ class SearchIndex(object):
 
         writer.commit()
 
-    def update_document(self, document={}):
+    def update_document(self, **document):
         """
         Updates an existing document in the index.
 
@@ -142,8 +142,8 @@ class SearchIndex(object):
         writer = self._get_writer()
         writer.update_document(**document)
         writer.commit()
-
-    def add_document_from_model_obj(self, model_obj):
+    
+    def _prepare_document_from_model_obj(self, model_obj):
         document = {}
         for name in self.field_names:
             try:
@@ -160,10 +160,19 @@ class SearchIndex(object):
             except AttributeError:
                 _log.info("Attribute %s not found in %s"%(
                     name, model_obj.__class__.__name__))
-        _log.info("Adding document %s"%document['title'])
-        
+        return document
+
+
+    def add_document_from_model_obj(self, model_obj):
+        document = self._prepare_document_from_model_obj(model_obj)
         self.add_document(**document)
+        _log.info("Added document %s"%document['title'])
     
+    def update_document_from_model_obj(self, model_obj):
+        document = self._prepare_document_from_model_obj(model_obj)
+        self.update_document(**document)
+        _log.info("Updated document %s"%(document['title']))
+
     def _process_query(self, query):
         query = unicode(query)
         query = MultifieldParser(self.field_names,
-- 
1.8.5.3



More information about the devel mailing list