diff options
author | neodarz <neodarz@neodarz.net> | 2019-01-16 19:35:47 +0100 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-01-16 19:35:47 +0100 |
commit | 04bb3fc0cd974cbae1db16e1c4413e05f2f3cd3c (patch) | |
tree | 666aefe74739b63b85527fd6af2fa3d044e66768 /sphinx/sphinx.py | |
parent | fb562ce9623c84f2eac5d3358d76caa634f709f2 (diff) | |
download | khanindexer-04bb3fc0cd974cbae1db16e1c4413e05f2f3cd3c.tar.xz khanindexer-04bb3fc0cd974cbae1db16e1c4413e05f2f3cd3c.zip |
Add manticoresearch search function
Diffstat (limited to '')
-rw-r--r-- | sphinx/sphinx.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/sphinx/sphinx.py b/sphinx/sphinx.py new file mode 100644 index 0000000..1fffa3e --- /dev/null +++ b/sphinx/sphinx.py @@ -0,0 +1,39 @@ +from .sphinxapi import * + +import config + +sphinxclient = SphinxClient() + +sphinxclient.SetServer(host=config.SPHINX_HOST, port=config.SPHINX_PORT) + +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 + + return response |