Django:你推荐哪种搜索库用于Windows?

时间:2012-09-19 08:16:50

标签: django search sphinx django-haystack

我在Windows上开发网站,但是当我尝试添加搜索时,我遇到了Sphinx和Haystack + Xapian的太多问题。可能的解决方案是转到Linux,但我不想改变我的工作环境。你推荐什么样的搜索库/服务器/ Windows?您使用过哪个版本,存储库,教程?也许你可以写自己的迷你教程?我真的对这个问题感到沮丧,并且几天都无法前进。

1 个答案:

答案 0 :(得分:0)

最后我让Sphinx上班了。在多个表中搜索仍然存在问题,但我相信这是可以解决的。

有用的链接:
How to install and implement Sphinx Search on XAMPP for Windows 7 with MySQL and PHP

Tutorial: Installing on Windows

Installing Sphinx on Windows

在以下代码段中使用了我系统上的路径。

views.py中的搜索功能:

def search(request):
    from sphinxapi import SphinxClient, SPH_MATCH_ANY, SPH_SORT_RELEVANCE
    S = request.GET['search']
    client = SphinxClient()
    client.SetServer('127.0.0.1', 9312)
    #client.SetSelect("*, AVG(price) AS avgprice")
    client.SetMatchMode(SPH_MATCH_ANY)
    client.SetSortMode(SPH_SORT_RELEVANCE)
    client.SetFieldWeights({'header': 20, 'text': 10})
    result = client.Query(S, '*')
    matches = result["matches"]
    ids = [match["id"] for match in matches]
    article = {"header": "Search results", "text": ""}
    if ids != []:
        objects = Main.objects.filter(pk__in = ids)
        for object in objects:
            url = request.build_absolute_uri(object.get_absolute_url())
            article["text"] += "<a href=" + url + ">" + object.header + "</a>" + "\n"
        ResponseDict = {"articles": [article]}
    else:
        ResponseDict = {"articles": []}
    return render_to_response("index.html", ResponseDict, 
        context_instance = RequestContext(request))

sphinx.conf(位于... \ Sphinx \ bin文件夹中):

source src1
{
    type = pgsql
    sql_host = localhost
    sql_user = <db user>
    sql_pass = <pwd>
    sql_db = <db name>
    sql_port = 5432
    sql_query = \
        SELECT id, header, text \
        FROM app_main
    sql_query_info = SELECT * FROM app_main WHERE id=$id
    sql_attr_uint = source_id
}


source src2
{
    type = pgsql
    sql_host = localhost
    sql_user = <db user>
    sql_pass = <pwd>
    sql_db = <db name>
    sql_port = 5432
    sql_query = \
        SELECT id, header, text \
        FROM app_comment
    sql_query_info = SELECT * FROM app_comment WHERE id=$id
    sql_attr_uint = source_id
}


index test1
{
    source = src1
    source = src2
    path = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/data/test1
    docinfo = extern
    charset_type = utf-8
}


index testrt
{
    type = rt
    rt_mem_limit = 32M
    path = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/data/testrt
    charset_type = utf-8
    rt_field = title
    rt_field = content
    rt_attr_uint = gid
}


indexer
{
    mem_limit = 32M
}


searchd
{
    listen = 127.0.0.1:9312
    log = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/log/searchd.log
    query_log = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/log/query.log
    read_timeout = 5
    max_children = 30
    pid_file = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/log/searchd.pid
    max_matches = 1000
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old = 1
    workers = threads # for RT to work
    binlog_path = D:/blizzard/Projects/Python/Web/moz455/app/sphinx/data
}

在app文件夹中创建\ sphinx,\ sphinx \ data和\ sphinx \ log文件夹后,可以使用命令进行索引

D:/Old/Sphinx/bin/indexer --config D:/Old/Sphinx/bin/sphinx.conf --all

并使用命令

开始搜索
D:/Old/Sphinx/bin/searchd --config D:/Old/Sphinx/bin/sphinx.conf