django中间件路径校验(Django中使用Whoosh进行全文检索的方法)
类别:脚本大全 浏览量:2837
时间:2021-10-23 10:33:56 django中间件路径校验
Django中使用Whoosh进行全文检索的方法Whoosh 是纯Python实现的全文搜索引擎,通过Whoosh可以很方便的给文档加上全文索引功能。
什么是全文检索
简单讲分为两块,一块是分词,一块是搜索。比如下面一段话:
上次舞蹈演出直接在上海路的弄堂里
比如我们现在想检索上次的演出,通常我们会直接搜索关键词: 上次演出 ,但是使用传统的SQL like 查询并不能命中上面的这段话,因为在 上次 和 演出 中间还有 舞蹈 。然而全文搜索却将上文切成一个个Token,类似:
上次/舞蹈/演出/直接/在/上海路/的/弄堂/里
切分成Token后做反向索引(inverted indexing),这样我们就可以通过关键字很快查询到了结果了。
解决分词问题
分词是个很有技术难度的活,比如上面的语句中一个难点就是到底是 上海路 还是 上海 呢?Python有个中文分词库: 结巴分词 ,我们可以通过结巴分词来完成索引中分词工作,结巴分词提供了Whoosh的组件可以直接集成,代码示例
遇到的问题
如果是在一些VPS上测试的时候非常慢的话可能是内存不足,比如512MB做一个博客索引非常慢,尝试升级到1GB后可以正常使用了。
代码
|
import logging import os import shutil from django.conf import settings from whoosh.fields import Schema, ID , TEXT, NUMERIC from whoosh.index import create_in, open_dir from whoosh.qparser import MultifieldParser from jieba.analyse import ChineseAnalyzer from .models import Article log = logging.getLogger(__name__) index_dir = os.path.join(settings.BASE_DIR, "whoosh_index" ) indexer = open_dir(index_dir) def articles_search(keyword): mp = MultifieldParser( [ 'content' , 'title' ], schema = indexer.schema, fieldboosts = { 'title' : 5.0 }) query = mp.parse(keyword) with indexer.searcher() as searcher: results = searcher.search(query, limit = 15 ) articles = [] for hit in results: log.debug(hit) articles.append({ 'id' : hit[ 'id' ], 'slug' : hit[ 'slug' ], }) return articles def rebuild(): if os.path.exists(index_dir): shutil.rmtree(index_dir) os.makedirs(index_dir) analyzer = ChineseAnalyzer() schema = Schema( id = ID (stored = True , unique = True ), slug = TEXT(stored = True ), title = TEXT(), content = TEXT(analyzer = analyzer)) indexer = create_in(index_dir, schema) __index_all_articles() def __index_all_articles(): writer = indexer.writer() published_articles = Article.objects.exclude(is_draft = True ) for article in published_articles: writer.add_document( id = str (article. id ), slug = article.slug, title = article.title, content = article.content, ) writer.commit() def article_update_index(article): ''' updating an article to indexer, adding if not. ''' writer = indexer.writer() writer.update_document( id = str (article. id ), slug = article.slug, title = article.title, content = article.content, ) writer.commit() def article_delete_index(article): writer = indexer.writer() writer.delete_by_term( 'id' , str (article. id )) writer.commit() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://www.imzjy.com/blog/2018-10-06-full-text-search-with-whoosh
您可能感兴趣
- django中间件路径校验(Django中使用Whoosh进行全文检索的方法)
- dedecms文章详情表(让Dedecms自带搜索实现全文检索支持标题与内容)
- phpmysql网站开发入门与提高(PHP+MySQL+sphinx+scws实现全文检索功能详解)
- dedecms有哪些功能(织梦DedeCMS v5.7全文检索使用说明sphinx)
- 今年考高会很难吗(今年高考会考试吗)
- 盘古开天地 他创造了世界,谁创造了盘古 盘古是伏羲吗(盘古开天地他创造了世界)
- 关于队徽 你了解这些么 二(关于队徽你了解这些么)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
- 0 1 岁婴儿最强作息指南,照着做养出天使宝宝(01岁婴儿最强作息指南)
- 沪上这16所高校 萌新 礼包开箱 哪一款让你心动(沪上这16所高校萌新)
热门推荐
- amaze算法(amazeui 验证按钮扩展的实现)
- 阿里云mysql如何接入(阿里云服务器安装Mysql数据库的详细教程)
- html5的占位符(html5 冒号分隔符对齐的实现)
- dedecms中如何设置推荐文章(实现dedecms友情链接分栏目调用的方法图)
- python人脸识别库(python3人脸识别的两种方法)
- python中lambda教程(浅析python的Lambda表达式)
- web项目部署到阿里云服务器上(阿里云的WindowsServer2016上部署php+apache)
- 写出好代码的几个建议
- zabbix 数据库优化(快速通过zabbix获取数据库连接的信息及部分扩展)
- docker镜像配置教程(给Docker更换国内镜像源操作)