1
|
|
|
from django.core.exceptions import ImproperlyConfigured |
2
|
|
|
|
3
|
|
|
from simple_forums.utils import get_setting |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class BaseSearch(object): |
7
|
|
|
|
8
|
|
|
# Sub-classes should override this with the settings required for |
9
|
|
|
# their search adapter connection. (host, port, password, etc.) |
10
|
|
|
REQUIRED_SETTINGS = [] |
11
|
|
|
|
12
|
|
|
def __init__(self): |
13
|
|
|
""" Pull connection information from settings file """ |
14
|
|
|
self.get_config() |
15
|
|
|
|
16
|
|
|
def add(self, object): |
17
|
|
|
""" Add the specified object to the search index. |
18
|
|
|
|
19
|
|
|
This must be implemented by the search backends that are sub- |
20
|
|
|
classes of this class. |
21
|
|
|
""" |
22
|
|
|
raise NotImplementedError |
23
|
|
|
|
24
|
|
|
def get_config(self): |
25
|
|
|
""" Get configuration options from the settings file """ |
26
|
|
|
self.connection_info = {} |
27
|
|
|
self.connection_settings = get_setting('search_backend', default={}) |
28
|
|
|
|
29
|
|
|
for field in self.REQUIRED_SETTINGS: |
30
|
|
|
try: |
31
|
|
|
self.connection_info[field] = self.connection_settings[field] |
32
|
|
|
except KeyError: |
33
|
|
|
raise ImproperlyConfigured( |
34
|
|
|
"Could not find '%s' in 'search_backend' setting" % field) |
35
|
|
|
|
36
|
|
|
def remove(self, object): |
37
|
|
|
""" Remove the specified object from the search index. |
38
|
|
|
|
39
|
|
|
This must be implemented by the search backends that are sub- |
40
|
|
|
classes of this class. |
41
|
|
|
""" |
42
|
|
|
raise NotImplementedError |
43
|
|
|
|
44
|
|
|
def search(self, search_query): |
45
|
|
|
""" Search for the specified search query. |
46
|
|
|
|
47
|
|
|
This must be implemented by the search backends that are sub- |
48
|
|
|
classes of this class. |
49
|
|
|
""" |
50
|
|
|
raise NotImplementedError |
51
|
|
|
|
52
|
|
|
def wipe(self): |
53
|
|
|
""" Wipes the search index. |
54
|
|
|
|
55
|
|
|
This must be implemented by the search backends that are sub- |
56
|
|
|
classes of this class. |
57
|
|
|
""" |
58
|
|
|
raise NotImplementedError |
59
|
|
|
|