X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ca7bffa0f660afefb98d37ac002b9652e1c858e5..6e5871ce487810fa5dac891e03296c6d469ffeb3:/apps/search/custom.py
diff --git a/apps/search/custom.py b/apps/search/custom.py
index 33ce47d32..86d387e02 100644
--- a/apps/search/custom.py
+++ b/apps/search/custom.py
@@ -5,7 +5,7 @@ import urllib
import warnings
from sunburnt import search
import copy
-
+from httplib2 import socket
class TermVectorOptions(search.Options):
def __init__(self, schema, original=None):
@@ -85,11 +85,15 @@ class CustomSolrInterface(sunburnt.SolrInterface):
def __init__(self, url, schemadoc=None, http_connection=None, mode='', retry_timeout=-1, max_length_get_url=sunburnt.MAX_LENGTH_GET_URL):
self.conn = CustomSolrConnection(url, http_connection, retry_timeout, max_length_get_url)
self.schemadoc = schemadoc
- if mode == 'r':
+ if 'w' not in mode:
self.writeable = False
- elif mode == 'w':
+ elif 'r' not in mode:
self.readable = False
- self.init_schema()
+ try:
+ self.init_schema()
+ except socket.error, e:
+ raise socket.error, "Cannot connect to Solr server, and search indexing is enabled (%s)" % str(e)
+
def _analyze(self, **kwargs):
if not self.readable:
@@ -119,8 +123,8 @@ class CustomSolrInterface(sunburnt.SolrInterface):
if matches:
return self.substring(kwargs['text'], matches,
- margins=kwargs.get('margins', 30),
- mark=kwargs.get('mark', ("", "")))
+ margins=kwargs.get('margins', 30),
+ mark=kwargs.get('mark', ("", "")))
else:
return None
@@ -134,22 +138,27 @@ class CustomSolrInterface(sunburnt.SolrInterface):
start = None
end = None
totlen = len(text)
- matches_margins = map(lambda (s, e): (max(0, s - margins), min(totlen, e + margins)), matches)
- (start, end) = matches_margins[0]
-
- for (s, e) in matches_margins[1:]:
+ matches_margins = map(lambda (s, e):
+ ((s, e),
+ (max(0, s - margins), min(totlen, e + margins))),
+ matches)
+ (start, end) = matches_margins[0][1]
+ matches = []
+ for (m, (s, e)) in matches_margins[1:]:
if end < s or start > e:
continue
start = min(start, s)
end = max(end, e)
-
+ matches.append(m)
+
snip = text[start:end]
- matches = list(matches)
matches.sort(lambda a, b: cmp(b[0], a[0]))
+
for (s, e) in matches:
off = - start
snip = snip[:e + off] + mark[1] + snip[e + off:]
snip = snip[:s + off] + mark[0] + snip[s + off:]
# maybe break on word boundaries
+
return snip