0

我使用这部分代码从 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

4

1 回答 1

1

解决问题,关闭 Selinux 并重新启动服务器,我通过以下命令收听 apache 进程:

ps auxw | grep httpd | awk '{print"-p " $2}' | xargs strace

并查看访问 apache 到 sphinx 和 memcache 的权限被拒绝

于 2012-12-09T16:45:12.027 回答