转自:http://www.cnblogs.com/chang/archive/2013/01/10/2855355.html
很遗憾,haystack+ sphinx 没有文档可以查看,菜鸟们都安息了。还好有django-sphinx 。这玩意挺好!!!!
安装:
django-sphinx , 这个现在已经是非常稳定的了,所以github上边也不会再有任何新的发行版了,稳定才是最好的保障。所以直接pip install django-sphinx 安装吧
sphinx ,下载,我暂时使用的是稳定版的,0.9.9的,http://sphinxsearch.com/downloads/archive/ ,你根据自己需要下载啊,整完直接解压到d盘吧
d:\sphinx
然后直接创建一个data文件夹 即d:\sphinx\data\ (在配置中使用的存储索引的地方)
使用,这个超简单的,下面是官方的用法说明
from djangosphinx.models import SphinxSearch class MyModel(models.Model): search = SphinxSearch() # 默认索引名是你的表名即model名 search = SphinxSearch('index_name') # 自己设定索引名 searchdelta = SphinxSearch( index='index_name delta_name', weights={ #设定相关权重 'name': 100, 'description': 10, 'tags': 80, }, mode='SPH_MATCH_ALL', rankmode='SPH_RANK_NONE', ) #以上是在model中使用 #下面是在views.py中使用查询方法 queryset = MyModel.search.query('query') results1 = queryset.order_by('@weight', '@id', 'my_attribute') results2 = queryset.filter(my_attribute=5) results3 = queryset.filter(my_other_attribute=[5, 3,4]) results4 = queryset.exclude(my_attribute=5)[0:10] results5 = queryset.count()
下面介绍我在练习中使用的例子
from django.db import models from djangosphinx.models import SphinxSearch class Chang(models.Model): title=models.CharField(max_length=200) body=models.TextField() tags=models.CharField(max_length=200) search=SphinxSearch(index='sphinxtest_chang')
然后再view中使用
from model import Chang
from django.shortcuts import render_to_response
def search(request): if request.method == 'POST': query=request.POST.get('query',None) r=Chang.search.query(query) chang=list(r) context={'chang':chang,'query':query,'search_meta':r._sphinx} else: chang=list() context={'chang':chang} return render_to_response('search/search.html',context)
再看我的templates\sphinxtest\search.html
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div> <form action="/search/" method="POST"> <input type="text" name="query"/> <input type="submit"> </form> {% if chang%} <p>Your search for “<strong>{{ query }}</strong>” had <strong>{{ search_meta.total_found }}</strong> results.</p> <p>search_meta object dump: {{ search_meta }}</p> {% endif %} <hr/> {% for s in chang%} <h3>{{ s.title }}</h3> <h4>{{s.body}}</h4> <p>(weight: {{ s.sphinx.weight }})</p> <p>story.sphinx object dump: {{ s.sphinx }}</p> {% endfor %} </div> </body> </html>
最后看看我的settings.py
INSTALLED_APPS = (
... 'djangosphinx', 'sphinxtest',
... ) #Sphinx 0.9.9 SPHINX_API_VERSION = 0x116
和urls.py配置
urlpatterns = patterns('',
(r'^search/$','sphinxtest.views.search'), )
准备工作做完之后,我们开始创建索引吧
python manage.py generate_sphinx_config sphinxtest >> d:\sphinx\bin\sphinx.conf
#这个本身sphinx.conf应该在d:\sphinx\下,我这样地址会创建一个新的,使用时也用他,不过这个是不完全的,建议直接参考d:\sphinx\下的sphinx-min.conf
直接上我的配置吧
source sphinxtest_chang { type = pgsql sql_host = 127.0.0.1 sql_user = postgres sql_pass = 1234 sql_db = chang sql_port = 5432 sql_query_pre = #SET NAMES utf8 sql_query_post = sql_query = \ SELECT id, title, body, tags\ FROM sphinx_story sql_query_info = SELECT * FROM `sphinx_story` WHERE `id` = $id } index sphinxtest_chang { source = sphinxtest_chang path = d:/sphinx/data/sphinxtest_chang docinfo = extern morphology = none stopwords = min_word_len = 1 charset_type = 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 # 需要分词的字符,如果要搜索中文,需设置此项 } indexer { mem_limit = 32M } searchd { port = 3312 log = @CONFDIR@/log/searchd.log query_log = @CONFDIR@/log/query.log read_timeout = 5 max_children = 30 pid_file = @CONFDIR@/log/searchd.pid max_matches = 1000 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 }
sphinx 创建索引
cd 到
d:\sphinx\bin\
indexer --config sphinx.conf sphinxtest_chang
开启sphinx服务
cd 到
d:\sphinx\bin\
searchd --config sphinx.conf
最后运行程序,查看结果吧,记得网数据库中添加点数据哦,否则你创建出的索引是搜不到结果的。