[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GMG-Devel] [PATCH 21/83] Fix bugs and add search method in SearchIndex
From: |
Alon Levy |
Subject: |
[GMG-Devel] [PATCH 21/83] Fix bugs and add search method in SearchIndex |
Date: |
Tue, 25 Feb 2014 21:58:04 +0200 |
From: Praveen Kumar <address@hidden>
---
mediagoblin/plugins/search/base.py | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/mediagoblin/plugins/search/base.py
b/mediagoblin/plugins/search/base.py
index c4f12af..c3a3cae 100644
--- a/mediagoblin/plugins/search/base.py
+++ b/mediagoblin/plugins/search/base.py
@@ -8,8 +8,8 @@ from mediagoblin.plugins.search.schemas import
MediaEntryIndexSchema
import whoosh
from whoosh.filedb.multiproc import MultiSegmentWriter
+from whoosh.qparser import MultifieldParser
-config = pluginapi.get_config('mediagoblin.plugins.search')
_log = logging.getLogger(__name__)
@@ -23,6 +23,8 @@ class SearchIndex(object):
"""
def __init__(self, model, schema, search_index_dir=None,
use_multiprocessing=None):
+
+ self.config = pluginapi.get_config('mediagoblin.plugins.search')
self.schema = schema()
self.field_names = self.schema.names()
@@ -36,11 +38,11 @@ class SearchIndex(object):
self.search_index_dir = search_index_dir
if not self.search_index_dir:
- self.search_index_dir = config['search_index_dir']
+ self.search_index_dir = self.config['search_index_dir']
self.search_index_dir += self.identifier
self.use_multiprocessing = use_multiprocessing
if not self.use_multiprocessing:
- self.use_multiprocessing = config['use_multiprocessing']
+ self.use_multiprocessing = self.config['use_multiprocessing']
self.create_index()
@@ -56,7 +58,7 @@ class SearchIndex(object):
raise IndexDoesNotExistsError(
self.search_index_dir, self.search_index_name)
- if self.search_index.exists_in(
+ if whoosh.index.exists_in(
self.search_index_dir, indexname=self.search_index_name):
return True
@@ -90,6 +92,8 @@ class SearchIndex(object):
self.search_index = whoosh.index.create_in(self.search_index_dir,
indexname=self.search_index_name, schema=self.schema)
+ self.search_index = whoosh.index.open_dir(self.search_index_dir,
+ indexname=self.search_index_name)
def add_document(self, **document):
@@ -131,11 +135,20 @@ class SearchIndex(object):
for name in self.field_names:
try:
attr = getattr(model_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.__name__))
- _log.info("Adding document ", document)
+ name, model_obj.__class__.__name__))
+ _log.info("Adding document %s"%document['title'])
self.add_document(**document)
+ def search(self, query):
+ query = unicode(query)
+ with self.search_index.searcher() as searcher:
+ query = MultifieldParser(self.field_names,
+ self.search_index.schema).parse(query)
+ results = searcher.search(query)
+ return results
--
1.8.5.3
- [GMG-Devel] [PATCH 11/83] Add indexer for MediaEntry objects., (continued)
- [GMG-Devel] [PATCH 11/83] Add indexer for MediaEntry objects., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 12/83] Add IndexRegistry class to maintain the search indices., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 13/83] Clean up plugins.search.indices, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 14/83] Rename base.py to registry.py and indices.py to base.py., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 15/83] Add index identifier to SearchIndex., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 16/83] Correct the index identifier., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 17/83] Collect all indices in indices.py, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 18/83] Add method to create a document from a model instance, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 19/83] Add event listeners for tracking ORM events for indices., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 20/83] Register indices and orm events for search., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 21/83] Fix bugs and add search method in SearchIndex,
Alon Levy <=
- [GMG-Devel] [PATCH 23/83] Add ORMEventsListener class to implement the ORM Events, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 22/83] Add ListenerRegistry class to collect all the listeners., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 24/83] Correct indentations in search.__init__, Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 25/83] Add search forms., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 26/83] Add view to handle search queries., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 27/83] Add html template for search page., Alon Levy, 2014/02/25
- [GMG-Devel] [PATCH 28/83] Fix bugs, Alon Levy, 2014/02/25
- [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