我使用这部分代码从 sphinx 获取文档,但 sphinx 查询总是返回 None。但是,如果我从命令行执行代码,我会得到正确的结果,有什么问题吗?
def search_query(query, offset=0):
mode = SPH_MATCH_EXTENDED
host = 'localhost'
port = 9312
index = 'rss_item'
filtercol = 'group_id'
filtervals = []
sortby = '-@weights'
groupby = 'id'
groupsort = '@group desc'
limit = 30
# do query
cl = SphinxClient()
cl.SetServer ( host, port )
cl.SetWeights ( [100, 1] )
cl.SetMatchMode ( mode )
#cl.SetSortMode(SPH_SORT_TIME_SEGMENTS)
if limit:
cl.SetLimits ( offset, limit, max(limit,1000) )
res = cl.Query ( query, index )
docs =[]
for item in res['matches']:
docs.append(item['id'])
return docs
# this is django view
def search(request):
q = request.GET.get('q', '')
offset = int(request.GET.get('older', 0))
docs=search_query(q, offset)
result = Item.objects.filter(id__in=docs).all()
objects = dict([(obj.id, obj) for obj in result])
sorted_objects = [objects[id] for id in docs]
result = sorted_objects
return render_to_response('rss/_items.html',
{'latest_items':result, 'offset':offset+30,'q':q},
context_instance=RequestContext(request))
服务器:centos6,狮身人面像:2.0.5,django:1.4.2,apache/wsgi