-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.py
281 lines (199 loc) · 7.8 KB
/
connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# encoding: utf-8
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""
.. module:: connection
:platform: Unix, Windows, Linux
:synopsis: Code for handling of python to solr communication
.. :moduleauthor: Maciej Dziardziel <[email protected]>
.. :moduleauthor: Michal Domanski <[email protected]>
"""
import httplib
import os.path
import socket
import urlparse
from urllib2 import urlopen, URLError
import exceptions
import config
from results import SelectResponse
from converters import msg_from_iterable
try:
import simplejson
except ImportError:
raise ImportError, 'insol depends on simplejson module but it was not found'
def reload_config():
import config as new_config
global config
config = new_config
def _get_solr_url():
current_config = config.get_current_config()
return urlparse.urlunparse(['http', '%s:%s' % (current_config['host'], current_config['port']),
os.path.join('solr', current_config['core'], ''), '', '', ''])
def _get_solr_select_url():
"""
return solr url based on config in app_settings
"""
return '%s%s/' % (_get_solr_url(), config.SOLR_SELECT_PATH)
def _get_solr_ping_url():
return '%s%s/' % (_get_solr_url(), config.SOLR_PING_PATH)
def _get_solr_update_url():
return '%s%s/' % (_get_solr_url(), config.SOLR_UPDATE_PATH)
def _send_query(query, **kwargs):
"""
default function for sending query to solr
"""
solr_url = kwargs.get('solr_url', _get_solr_select_url())
url = '%s?%s' % (solr_url, query)
try:
return urlopen(url)
except URLError, exc:
raise exceptions.SolrConnectionError(exc)
def _handle_connection(connection, **kwargs):
"""
default connection handler, expects and object
with .read method and performs decoding on it,
extra parameter decoder for overload of default one
"""
decoder = kwargs.get('decoder', simplejson.loads)
return decoder(connection.read())
def _build_response(data, **kwargs):
"""
takes a response object and loads it with data from Solr
then returns it to user , have fun :)
"""
carrier = kwargs.get('carrier', SelectResponse)()
carrier._header = data['responseHeader']
carrier._response = data.get('response')
carrier._facets = data.get('facet_counts')
carrier._stats = data.get('stats')
return carrier
def _update(data):
connection = httplib.HTTPConnection(config.SOLR_ADDRESS, config.SOLR_PORT)
connection.request('POST', _get_solr_update_url(), data, headers={'Content-type': 'text/xml'})
try:
response = connection.getresponse()
except httplib.HTTPException, e:
raise exceptions.SolrConnectionError(e.message)
except socket.timeout, e:
raise exceptions.SolrConnectionTimeout(e.message)
return response
def add(elems):
"""
Add data to solr index.
:param elems: data for solr to add to index
:type elems: iterable
:return: response to sent data
:rtype: HTTPResponse
"""
if not isinstance(elems, (tuple, list)):
elems = [elems]
return _update(msg_from_iterable(elems))
def commit(wait_flush = False, wait_searcher = False):
"""
Send 'commit' message to solr. If not used, your changes will not be visible.
:param wait_flush: not clear, i suppose this makes connection wait for searcher flush
:param wait_searcher: wait for creation of new searcher, guarantees you get updated data when searching
:type wait_flush: bool
:type wait_searcher: bool
"""
wait_flush_cmd = 'waitFlush="false"'
wait_searcher_cmd = 'waitSearcher="false"'
if wait_flush:
wait_flush_cmd = 'waitFlush="true"'
if wait_searcher:
wait_searcher_cmd = 'waitSearcher="true"'
cmd = '<commit %s %s />' % (wait_flush_cmd, wait_searcher_cmd)
return _update(cmd)
def optimize(wait_flush = False):
"""
Send 'optimize' command to solr. Rebuilds and optimizes solr index.
:param wait_flush: not clear, need to be updated
:type wait_flush: bool
.. warning::
This operation is generates extra load on solr server and may render server unresponsive in extreme situations.
"""
wait_flush_cmd = 'waitFlush="false"'
if wait_flush:
wait_flush_cmd = 'waitFlush="true"'
cmd = '<optimize %s />' % (wait_flush_cmd,)
return _update(cmd)
def is_alive():
"""
Check if solr responds.
"""
connection = httplib.HTTPConnection(config.SOLR_ADDRESS, config.SOLR_PORT)
connection.request('GET', _get_solr_ping_url())
try:
response = connection.getresponse()
except httplib.HTTPException, e:
return False
except socket.timeout, e:
return False
return (response.status==200)
def delete(**kwargs):
"""
Remove data from solr. Need to be reworked to accept query param.
:param id: id of document to remove
:type id: int
"""
if 'id' in kwargs:
xml = '<delete><id>%s</id></delete>' % kwargs['id']
return _update(xml)
def delete_multi(doc_ids, field_name='id'):
"""
Deletes multiple documents at once.
:param doc_ids: list of document ids to removed from solr index
:type doc_ids: iterable
:param field_name: field to be used as the parameter for removal
:type field_name: str
"""
xml = '\n'.join('<delete><%(field_name)s>%(doc_id)s</%(field_name)s></delete>' % {'field_name':field_name, 'doc_id':doc_id } for doc_id in doc_ids)
return _update(xml)
def search(query, **kwargs):
"""
Main interest for most of users of this module, expects a query object,
can take extra arguments to override default behaviour
:param query: main and only required object, carries data for searching in solr
:type query: :mod:`query`
:param send_query: optional handler for sending data
:type send_query: callable
:param handle_connection: optional handler for handling connection with solr
:type handle_connection: callable
:param build_response: handler for building response object from received data
:type build_response: callable
.. rubric:: Usage:
1.default::
>>> connection.search(query)
2.debugging::
>>> def debug_send_query(query):
>>> print locals()
>>> return connection._send_query(query)
>>> connection.search(query, send_query = debug_send_query)
This of course is only a simple demo.
"""
handlers = {
'send_query': _send_query,
'handle_connection': _handle_connection,
'build_response': _build_response,
}
handlers.update(kwargs)
if not 'wt' in query: query.wt = kwargs.get('wt','json')
if not hasattr(query, '_url'):
raise exceptions.SolrQueryError, 'no _url attribute on your query object'
connection = handlers['send_query'](query._url)
data = handlers['handle_connection'](connection)
response = handlers['build_response'](data)
return response
def search_multicore(core_config_name, query):
config.load_config(core_config_name)
return search(query)