aboutsummaryrefslogtreecommitdiff
path: root/sphinx/sphinx.py
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-01-17 22:20:46 +0100
committerneodarz <neodarz@neodarz.net>2019-01-17 22:20:46 +0100
commit82aefaaaf1c0fc64e584f7025259f84d2bcc347b (patch)
tree00a7094105d23cca35525bee0f1f80a8e5faff1d /sphinx/sphinx.py
parent5fe782772472914ce454198e5a6f3b40e2ecdb69 (diff)
downloadkhanindexer-82aefaaaf1c0fc64e584f7025259f84d2bcc347b.tar.xz
khanindexer-82aefaaaf1c0fc64e584f7025259f84d2bcc347b.zip
Add index selection
Diffstat (limited to '')
-rw-r--r--sphinx/sphinx.py42
1 files changed, 22 insertions, 20 deletions
diff --git a/sphinx/sphinx.py b/sphinx/sphinx.py
index b0886d2..11d3fee 100644
--- a/sphinx/sphinx.py
+++ b/sphinx/sphinx.py
@@ -6,7 +6,7 @@ sphinxclient = SphinxClient()
sphinxclient.SetServer(host=config.SPHINX_HOST, port=config.SPHINX_PORT)
-def search(request):
+def search(request, index):
"""
Simple sphinx seach function, return a array of all documents matching the
search terms.
@@ -14,29 +14,31 @@ def search(request):
status = sphinxclient.Status()
response = {'error': 1, 'msg': 'Sphinx server not available'}
if status != None:
- res = sphinxclient.Query(request)
+ res = sphinxclient.Query(request, index)
opts = {'before_match': '', 'after_match':'', '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
+ if res != None:
+ 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
+ else:
+ response = {'error': 1, 'msg': 'Maybe `'+str(index)+'` is not an index in the database?'}
return response