[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GMG-Devel] [PATCH 39/83] Clean up and add docstring to search methods.
From: |
Alon Levy |
Subject: |
[GMG-Devel] [PATCH 39/83] Clean up and add docstring to search methods. |
Date: |
Tue, 25 Feb 2014 21:58:22 +0200 |
From: Praveen Kumar <address@hidden>
---
mediagoblin/plugins/search/base.py | 83 +++++++++++++++++++++++++++++---------
1 file changed, 64 insertions(+), 19 deletions(-)
diff --git a/mediagoblin/plugins/search/base.py
b/mediagoblin/plugins/search/base.py
index 5e5a123..aca5967 100644
--- a/mediagoblin/plugins/search/base.py
+++ b/mediagoblin/plugins/search/base.py
@@ -47,11 +47,18 @@ class SearchIndex(object):
self.create_index()
def _open_search_index(self):
- self.search_index = whoosh.index.open_dir(self.search_index_dir,
+ """
+ Open the associated index this class is associated with for
+ reading/writing.
+ """
+ self.search_index = whoosh.index.open_dir(self.search_index_dir,
indexname=self.search_index_name)
def _filter_field_names(self, names):
+ """
+ Removes field names which end with '_stored'.
+ """
filtered_names = [name for name in names if not
name.endswith('_stored')]
return filtered_names
@@ -78,6 +85,12 @@ class SearchIndex(object):
def _get_writer(self):
+ """
+ Returns a valid writer object for modiying the index.
+
+ Its necessary to open the index every time a modification has
+ to be made to the index.
+ """
self._open_search_index()
writer = None
if self.use_multiprocessing:
@@ -146,55 +159,88 @@ class SearchIndex(object):
writer.update_document(**document)
writer.commit()
- def _prepare_document_from_model_obj(self, model_obj):
+ def _prepare_document_from_obj(self, obj):
+ """
+ Creates a dict of field names and corresponding values
+ from the object received.
+
+ The values present in the dict prepared is actually stored
+ in the index. For the stored fields, the value is taken from
+ the corresponding non-stored fields.
+ """
document = {}
for name in self.field_names:
try:
attr = None
if name.endswith('_stored'):
parent_name = name.replace('_stored', '')
- attr = getattr(model_obj, parent_name)
+ attr = getattr(obj, parent_name)
else:
- attr = getattr(model_obj, name)
+ attr = getattr(obj, name)
if isinstance(attr, int):
attr = unicode(attr)
document[name] = attr
except AttributeError:
_log.info("Attribute %s not found in %s"%(
- name, model_obj.__class__.__name__))
+ name, obj.__class__.__name__))
return document
- def add_document_from_model_obj(self, model_obj):
- document = self._prepare_document_from_model_obj(model_obj)
+ def add_document_from_obj(self, obj):
+ """
+ Adds a document to the index created from the given obj.
+ """
+ document = self._prepare_document_from_obj(obj)
self.add_document(**document)
- _log.info("Added %s with id %s"%(model_obj.__class__.__name__,
- str(model_obj.id)))
+ _log.info("Added %s with id %s"%(obj.__class__.__name__,
+ str(obj.id)))
- def update_document_from_model_obj(self, model_obj):
- document = self._prepare_document_from_model_obj(model_obj)
+ def update_document_from_obj(self, obj):
+ """
+ Updates an existing index entry with the data from the given obj.
+ """
+ document = self._prepare_document_from_obj(obj)
self.update_document(**document)
- _log.info("Updated %s with id %s"%(model_obj.__class__.__name__,
- str(model_obj.id)))
+ _log.info("Updated %s with id %s"%(obj.__class__.__name__,
+ str(obj.id)))
- def delete_document_from_model_obj(self, model_obj):
- id_stored = unicode(model_obj.id)
+ def delete_document_from_obj(self, obj):
+ """
+ Deletes an index entry corresponding to the given object.
+ """
+ id_stored = unicode(obj.id)
self._open_search_index()
self.search_index.delete_by_term('id_stored', id_stored)
- _log.info("Deleted %s with id %s"%(model_obj.__class__.__name__,
- str(model_obj.id)))
+ _log.info("Deleted %s with id %s"%(obj.__class__.__name__,
+ str(obj.id)))
def _process_query(self, query):
+ """
+ Returns a whoosh query object for the given user query string.
+ """
query = unicode(query)
query = MultifieldParser(self.field_names,
self.schema).parse(query)
return query
def _interpret_results(self, results, request):
- raise NotImplementedError
+ """
+ Returns the results in a specific structure.
+
+ This method must be overloaded by the derived classes
+ as the way of interpreting the results is different for
+ different storage objects.
+
+ `request` is passed from the search view. It is needed
+ for generating the data for the search results.
+ """
+ raise NotImplementedError
def search(self, query, request):
+ """
+ Performs a search against the index for the given user query.
+ """
self._open_search_index()
with self.search_index.searcher() as searcher:
query = self._process_query(query)
@@ -202,4 +248,3 @@ class SearchIndex(object):
all_results = self._interpret_results(results, request)
return all_results
-
--
1.8.5.3
- [GMG-Devel] [PATCH 29/83] Add methods to interpret results by the SearchIndex object, (continued)
- [GMG-Devel] [PATCH 29/83] Add methods to interpret results by the SearchIndex object, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 30/83] fix parameters in search views, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 31/83] Refactor SearchIndex and add method to update an index., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 32/83] Add event listener for model update, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 33/83] Add listener for model delete event., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 34/83] Fix bugs in SearchIndex, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 35/83] Add indices for MediaEntry and MediaTag., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 36/83] Add schema for MediaTag., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 37/83] Register schemas for MediaEntry and MediaTag., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 38/83] Add gmg command to delete all indices., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 39/83] Clean up and add docstring to search methods.,
Alon Levy <=
- [GMG-Devel] [PATCH 40/83] Add try-except block to event listeners., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 42/83] Store attributes from a model only it its available., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 43/83] Add field for storing search results., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 41/83] Correct method names in event listeners, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 44/83] Remove the field for storing search results with the index., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 46/83] Add verbose_name and css_id to search results., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 47/83] Adapt results collection to change in result generation., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 48/83] Modify search template to show tabbed results., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 49/83] Show only unique results., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 50/83] Change element ids and some template fixes., Alon Levy, 2014/02/25