aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app.py12
-rw-r--r--sphinx/sphinx.py42
-rw-r--r--sphinx_search.conf8
3 files changed, 35 insertions, 27 deletions
diff --git a/app.py b/app.py
index 42e9055..933a140 100644
--- a/app.py
+++ b/app.py
@@ -17,12 +17,18 @@ app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def search():
query = request.args.get('search')
- if query != '' and query != None:
- response = sphinx.search(query)
+ index = request.args.get('index')
+ if query != '' and query != None and index != '' and index != None:
+ response = sphinx.search(query, index)
elif query is None:
response = {'error': 1, 'msg': 'Use `search` attribute for make a search'}
+ elif index == None:
+ response = {'error': 1, 'msg': 'Use `index` attribute to precise an index'}
+ elif index == '':
+
+ response = {'error': 1, 'msg': '`index` cannot be null'}
else:
- response = {'error': 1, 'msg': 'Query cannot be null'}
+ response = {'error': 1, 'msg': '`search` cannot be null'}
return jsonify(response)
def crawl():
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
diff --git a/sphinx_search.conf b/sphinx_search.conf
index 5ba9c23..6e8f214 100644
--- a/sphinx_search.conf
+++ b/sphinx_search.conf
@@ -1,4 +1,4 @@
-source datas {
+source neodarznet {
type = pgsql
sql_host = 127.0.0.1
@@ -14,9 +14,9 @@ source datas {
}
-index datas {
- source = datas
- path = /tmp/data/datas
+index neodarznet {
+ source = neodarznet
+ path = /tmp/data/neodarznet
}
indexer {