最近,又回到老本行了. 呵呵 iOS 折腾好, 开始折腾 python . 话说, 我在微博上 发起山寨一个 pinterset 需要多少时间. 那么 solr 也是 pinterset 搜索的 一项核心技术. 下面完全实在 solr 已经打架完毕的状态下 实现的 参考文档: http://www.slideshare.net/simon/advanced-aspects-of-the-django-ecosystem- haystack-celery-fabric 注: solr 跑在 localhost:8983

从 github 下载 solango(命名挺有意思的):

git clone https://github.com/screeley/solango.git

可以通过 python setup.py install 安装, 也可以 cp -r solango myproject/ 下

配置 Django setting.py

INSTALLED_APPS = (
    'solango',
)

SOLR_ROOT = "/path/to/apache-solr-1.4.1/example/"
SOLR_SCHEMA_PATH = SOLR_ROOT + 'solr/conf/schema.xml'
SOLR_DATA_DIR = SOLR_ROOT + 'solr/data'
SOLR_DEFAULT_OPERATOR = 'or'
SOLR_INDEXER = "solango.indexing.DBQueuedIndexer"

定义 schema:

import solango

class Cards(models.Model):
    user = models.ForeignKey(User, related_name='user')
    desc = models.TextField('描述', blank=True, null=True)
    pub_time = models.DateTimeField('发布时间',auto_now=False, editable=True, db_index=True)

class CardsDocument(solango.SearchDocument):
    desc = solango.fields.TextField(copy=True)
    pub_time = solango.fields.DateTimeField(copy=True)
solango.register(Cards, CardsDocument)

建立索引:

python manage.py solr --fields #查看索引字段
python manage.py solr --flush #清除旧的索引, 必须关闭 solr
python manage.py solr --schema #建立 solr schema 配置
python manage.py solr --reindex #建索引

应用 solr

from solango import connection

def search(request, template='search/documents.html'):
    keywords = request.GET.get('q', None)
    results = connection.select(q=keywords)
#    count = len(docs)
    return render_to_response(template, {'documents':results.documents, 'count':results.count}, context_instance=RequestContext(request))

search.documents.html 模版

一共找到 {{ count }} 个商品





{% for doc in documents %}


{{ doc.fields.pub_time.value }}[{{ doc.highlight|safe }}]({% url qin.views.carddetail doc.fields.id.value %})


    {{ doc.fields.url.value }}