diff options
author | neodarz <neodarz@neodarz.net> | 2019-01-16 22:00:27 +0100 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-01-16 22:00:27 +0100 |
commit | f2bd717b4d64bd3d9f183b92a22665282fc8b82e (patch) | |
tree | c241169d02483a27f6df25600c1ac7a6bced8859 /sphinx | |
parent | 5a66387cd3afbea3709bc69940932e7ca0c96ce6 (diff) | |
download | khanindexer-f2bd717b4d64bd3d9f183b92a22665282fc8b82e.tar.xz khanindexer-f2bd717b4d64bd3d9f183b92a22665282fc8b82e.zip |
Add searchd error management
Diffstat (limited to 'sphinx')
-rw-r--r-- | sphinx/sphinx.py | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/sphinx/sphinx.py b/sphinx/sphinx.py index 1fffa3e..88f4733 100644 --- a/sphinx/sphinx.py +++ b/sphinx/sphinx.py @@ -11,29 +11,32 @@ def search(request): Simple sphinx seach function, return a array of all documents matching the search terms. """ - res = sphinxclient.Query(request) - - opts = {'before_match': '<b>', 'after_match':'</b>', 'chunk_separator': '...', 'limit': 400, 'around': 15} - index = 'datas' - - response = [] - - if 'matches' in res: - n = 1 - for match in res['matches']: - attrsdump = '' - for attr in res['attrs']: - attrname = attr[0] - attrtype = attr[1] - if attrname != "content": - value = match['attrs'][attrname] - if attrtype==SPH_ATTR_TIMESTAMP: - value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) ) - attrsdump = '%s, \'%s\'=\'%s\'' % ( attrsdump, attrname, value) - docs = [] - docs.append(''.join([line.strip('\n') for line in match['attrs']['content']])) - res_excerpts = sphinxclient.BuildExcerpts(index=index, docs=docs, opts=opts, words=request) - response.append({'id': match['id'], 'weight': match['weight'], 'url': match['attrs']['url'], 'title': match['attrs']['title'], 'excerpts': res_excerpts}) - n += 1 + status = sphinxclient.Status() + response = {'error': 1, 'msg': 'Sphinx server not available'} + if status != None: + res = sphinxclient.Query(request) + + opts = {'before_match': '<b>', 'after_match':'</b>', 'chunk_separator': '...', 'limit': 400, 'around': 15} + index = 'datas' + + response = [] + + if 'matches' in res: + n = 1 + for match in res['matches']: + attrsdump = '' + for attr in res['attrs']: + attrname = attr[0] + attrtype = attr[1] + if attrname != "content": + value = match['attrs'][attrname] + if attrtype==SPH_ATTR_TIMESTAMP: + value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) ) + attrsdump = '%s, \'%s\'=\'%s\'' % ( attrsdump, attrname, value) + docs = [] + docs.append(''.join([line.strip('\n') for line in match['attrs']['content']])) + res_excerpts = sphinxclient.BuildExcerpts(index=index, docs=docs, opts=opts, words=request) + response.append({'id': match['id'], 'weight': match['weight'], 'url': match['attrs']['url'], 'title': match['attrs']['title'], 'excerpts': res_excerpts}) + n += 1 return response |