2 from sunburnt import sunburnt
6 from sunburnt import search
10 class TermVectorOptions(search.Options):
13 def __init__(self, schema, original=None):
17 self.positions = False
19 self.fields = copy.copy(original.fields)
20 self.positions = copy.copy(original.positions)
22 def update(self, positions=False, fields=None):
25 if isinstance(fields, basestring):
27 self.schema.check_fields(fields, {"stored": True})
28 self.fields.update(fields)
29 self.positions = positions
35 opts['tv.positions'] = 'true'
37 opts['tv.fl'] = ','.join(sorted(self.fields))
41 class CustomSolrConnection(sunburnt.SolrConnection):
42 def __init__(self, *args, **kw):
43 super(CustomSolrConnection, self).__init__(*args, **kw)
44 self.analysis_url = self.url + "analysis/field/"
46 def analyze(self, params):
47 qs = urllib.urlencode(params)
48 url = "%s?%s" % (self.analysis_url, qs)
49 if len(url) > self.max_length_get_url:
50 warnings.warn("Long query URL encountered - POSTing instead of "
51 "GETting. This query will not be cached at the HTTP layer")
52 url = self.analysis_url
56 headers={"Content-Type": "application/x-www-form-urlencoded"},
59 kwargs = dict(method="GET")
60 r, c = self.request(url, **kwargs)
62 raise sunburnt.SolrError(r, c)
66 # monkey patching sunburnt SolrSearch
67 search.SolrSearch.option_modules += ('term_vectorer',)
70 def __term_vector(self, positions=False, fields=None):
71 newself = self.clone()
72 newself.term_vectorer.update(positions, fields)
74 setattr(search.SolrSearch, 'term_vector', __term_vector)
75 __original__init_common_modules = search.SolrSearch._init_common_modules
78 def __patched__init_common_modules(self):
79 __original__init_common_modules(self)
80 self.term_vectorer = TermVectorOptions(self.schema)
81 setattr(search.SolrSearch, '_init_common_modules', __patched__init_common_modules)
84 class CustomSolrInterface(sunburnt.SolrInterface):
85 # just copied from parent and SolrConnection -> CustomSolrConnection
86 def __init__(self, url, schemadoc=None, http_connection=None, mode='', retry_timeout=-1, max_length_get_url=sunburnt.MAX_LENGTH_GET_URL):
87 self.conn = CustomSolrConnection(url, http_connection, retry_timeout, max_length_get_url)
88 self.schemadoc = schemadoc
90 self.writeable = False
95 def _analyze(self, **kwargs):
97 raise TypeError("This Solr instance is only for writing")
99 'analysis_showmatch': True
101 if 'field' in kwargs: args['analysis_fieldname'] = kwargs['field']
102 if 'text' in kwargs: args['analysis_fieldvalue'] = kwargs['text']
103 if 'q' in kwargs: args['q'] = kwargs['q']
104 if 'query' in kwargs: args['q'] = kwargs['q']
106 params = map(lambda (k, v): (k.replace('_', '.'), v), sunburnt.params_from_dict(**args))
108 content = self.conn.analyze(params)
109 doc = etree.fromstring(content)
112 def highlight(self, **kwargs):
113 doc = self._analyze(**kwargs)
114 analyzed = doc.xpath("//lst[@name='index']/arr[last()]/lst[bool/@name='match']")
117 start = int(wrd.xpath("int[@name='start']")[0].text)
118 end = int(wrd.xpath("int[@name='end']")[0].text)
119 matches.add((start, end))
123 return self.substring(kwargs['text'], matches,
124 margins=kwargs.get('margins', 30),
125 mark=kwargs.get('mark', ("<b>", "</b>")))
129 def analyze(self, **kwargs):
130 doc = self._analyze(self, **kwargs)
131 terms = doc.xpath("/lst[@name='index']/arr[last()]/lst/str[1]")
132 terms = map(lambda n: unicode(n.text), terms)
135 def substring(self, text, matches, margins=30, mark=("<b>", "</b>")):
139 matches_margins = map(lambda (s, e): (max(0, s - margins), min(totlen, e + margins)), matches)
140 (start, end) = matches_margins[0]
142 for (s, e) in matches_margins[1:]:
143 if end < s or start > e:
145 start = min(start, s)
148 snip = text[start:end]
149 matches = list(matches)
150 matches.sort(lambda a, b: cmp(b[0], a[0]))
151 for (s, e) in matches:
153 snip = snip[:e + off] + mark[1] + snip[e + off:]
154 snip = snip[:s + off] + mark[0] + snip[s + off:]
155 # maybe break on word boundaries