DjangoSphinx 安装和简单应用

参考文章链接:
http://github.com/dcramer/django-sphinx
http://pkarl.com/articles/guide-django-full-text-search-sphinx-and-django-sp/
http://www.davidcramer.net/code/79/in-depth-django-sphinx-tutorial.html

首先需要已经安装好django以及sphinx
我的版本如下:
Django 1.1.1
Sphinx 0.9.9

1.安装django-sphinx

http://pypi.python.org/pypi/django-sphinx/2.1.4
下载得到django-sphinx-2.1.4.tar.gz 版本:2.1.4
解压缩到d:/python25/sphinx
在windows命令行编辑器中定位到d:/python25/sphinx 执行以下语句(具体路径视自己具体情况而定):

python setup.py install

安装完毕后,可以找到以下目录 D:\Python25\Lib\site-packages\django_sphinx-2.1.4-py2.5.egg

2.配置

在你的django的工程settings.py里对sphinx的版本进行定义,以下是django-sphinx作者的原话:

Note: You will need to install the sphinxapi.py package into your Python Path or use one of the included versions. To use the included version, you must specify the following in your settings.py file:
# Sphinx 0.9.9
SPHINX_API_VERSION = 0x116

# Sphinx 0.9.8
SPHINX_API_VERSION = 0x113

# Sphinx 0.9.7
SPHINX_API_VERSION = 0x107

3.新建一个Django的app进行测试

a) 新建app
在Windows命令行编辑器中执行以下语句:

python manage.py startapp sphinxtest

b) 编辑sphinxtest的models.py

#coding:utf-8
from django.db import models
from djangosphinx.models import SphinxSearch

class Story(models.Model):
title=models.CharField(max_length=200)
body=models.TextField()
tags=models.CharField(max_length=200)

search=SphinxSearch(index='sphinxtest_story')

在setting.py 的INSTALLED_APPS里添加’sphinxetest’

c) 在数据库中创建sphinxtest_story表

python manage.py syncdb

d) 修改sphinx配置文件(sphinx.conf)
django-sphinx 的作者内置了一个聪明的办法,可以根据models自动生成相应的sphinx配置语句段

python manage.py generate_sphinx_config [your_app_name] >> d:\sphinx\bin\sphinx.conf

执行之后,在sphinx.conf文件的最后面多了以下语句:

source sphinxtest_story
{
type = mysql
sql_host =
sql_user = [your django mysql user]
sql_pass = [your django mysql user password]
sql_db = [your django db]
sql_port =
sql_query_pre =
sql_query_post =
sql_query = \
SELECT id, title, body, tags\
FROM sphinxtest_story
sql_query_info = SELECT * FROM `sphinxtest_story` WHERE `id` = $id
}

index sphinxtest_story
{
source = sphinxtest_story
path = /var/data/sphinxtest_story
docinfo = extern
morphology = none
stopwords =
min_word_len = 2
charset_type = utf-8
min_prefix_len = 0
min_infix_len = 0
}

因为是自动生成的,所以并不能保证完全正确,比如索引数据的保存路径,所以还需要自己做相应的一些修改,但是这已经比完全手动配置要方便的多了
经过我修改后的配置语句段:

source sphinxtest_story
{
type = mysql
sql_host = localhost
sql_user = [your django mysql user]
sql_pass = [your django mysql user password]
sql_db = [your django db]
sql_port = 3306
sql_query_pre =SET NAMES utf8
sql_query_post =
sql_query = \
SELECT id, title, body, tags\
FROM sphinxtest_story
sql_query_info = SELECT * FROM `sphinxtest_story` WHERE `id` = $id
}

index sphinxtest_story
{
source = sphinxtest_story
path = d:/sphinx/data/sphinxtest_story/
docinfo = extern
morphology = none
stopwords =
min_word_len = 2
charset_type = utf-8 #建立索引的数据库的存储编码,目前只支持sbcs和utf-8,如果要搜索中文,使用utf-8
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F #索引时用于转换大小写的字符表
min_prefix_len = 0
min_infix_len = 0
ngram_len = 1 # 简单分词,只支持0和1,如果要搜索中文,请指定为1
ngram_chars = U+3000..U+2FA1F # 需要分词的字符,如果要搜索中文,需设置此项
}

e)建立索引

我已经在数据库中添加了几个story项目
现在开始用indexer工具建立索引:
在Windows命令行编辑器中定位到d:/sphinx/bin/ 执行:

indexer --config sphinx.conf sphinxtest_story

得到以下信息:

Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file 'sphinx.conf'...
indexing index 'sphinxtest_story'...
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 87 bytes
total 0.041 sec, 2093 bytes/sec, 72.19 docs/sec
total 1 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 5 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg

建立索引完成
f) 修改views.py

#coding:utf-8

from models import Story
from django.shortcuts import render_to_response

def search(request):
if request.method == 'POST':
query=request.POST.get('query',None)
r=Story.search.query(query)
story=list(r)
context={'story':story,'query':query,'search_meta':r._sphinx}
else:
story=list()
context={'story':story}
return render_to_response('search.html',context)

g) 创建search.html

<div>
<form action="/search/" method="POST">
<input type="text" name="query"/>
<input type="submit">
</form>
{% if story %}
<p>Your search for &ldquo;<strong>{{ query }}</strong>&rdquo; had <strong>{{ search_meta.total_found }}</strong> results.</p>
<p>search_meta object dump: {{ search_meta }}</p>
{% endif %}
<hr/>
{% for s in story %}
<h3>{{ s.title }}</h3>
<h4>{{s.body}}</h4>
<p>(weight: {{ s.sphinx.weight }})</p>
<p>story.sphinx object dump: {{ s.sphinx }}</p>
{% endfor %}
</div>

h) 修改urls.py
添加以下语句:

(r'^search/$','sphinxtest.views.search'),

i) 启动searchd服务
在Windows命令行编辑器中定位到d:/sphinx/bin 执行:

searchd

显示以下信息:

Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

WARNING: forcing --console mode on Windows
using config file './sphinx.conf'...
listening on all interfaces, port=3312
accepting connections

启动searchd服务成功

最后到浏览器里进行测试,看看结果吧

原文地址:https://www.cnblogs.com/lddhbu/p/2560455.html