簡(jiǎn)介
全文檢索主要用在大數(shù)據(jù)量時(shí)多字段模糊檢索上能較大的提高檢索效率。django實(shí)現(xiàn)全文檢索功能主要靠haystack框架盛卡,而用的最多的全文檢索引擎就是whoosh,jieba主要用于中文分詞筑凫,whoosh自帶的分詞是英文的滑沧。要實(shí)現(xiàn)以上組合的全文檢索,首先要安裝這些模塊:
pip install django-haystack
pip install whoosh
pip install jieba
配置haystack框架和whoosh引擎
安裝好以上模塊后巍实,接下來要到項(xiàng)目的settings.py
中添加haystack
應(yīng)用滓技,配置whoosh搜索引擎。
#settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'haystack',#全文檢索框架
'book',
'user',
'recommend',
'library',
'comment',
]
HAYSTACK_CONNECTIONS = {
'default': {
#使用whoosh引擎
'ENGINE': 'haystack.backends.whoosh_cn_backend.WhooshEngine',
#索引文件路徑
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
}
}
接下來到項(xiàng)目的urls.py
中添加全文檢索的路由棚潦。
#項(xiàng)目的urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('book.urls')),
path('book/', include('book.urls')),
path('user/', include('user.urls')),
path('recommend/', include('recommend.urls')),
path('library/', include('library.urls')),
path('comment/', include('comment.urls')),
path('search/', include('haystack.urls')),#全文檢索路由
]
然后在要做全文檢索的appbook
下創(chuàng)建search_indexes.py
文件令漂,該文件是固定命名,內(nèi)容如下:
from haystack import indexes#導(dǎo)入索引
from .models import Book_info#導(dǎo)入模型
#Book_infoIndex是固定格式命名,Book_info是你models.py中的類名
class Book_infoIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Book_info
def index_queryset(self, using=None):
return self.get_model().objects.all()
然后到項(xiàng)目下的templates文件夾中依次創(chuàng)建search/indexes/book/目錄叠必,book
是你需要使用全文檢索的app名荚孵,根據(jù)實(shí)際情況更改為自己的app名,book
前面的目錄名是固定寫法纬朝,不能更改收叶。
在book目錄下以"類名_text.txt
"格式創(chuàng)建搜索引擎的索引文件,如"book_info_text.txt
"共苛,book_info是book
app下的models.py中的一個(gè)類class Book_info(models.Model):
判没。
創(chuàng)建好txt文件后,在文件中輸入要索引的字段隅茎。
{{ object.book_name }}
{{ object.book_author }}
{{ object.book_press }}
上面的內(nèi)容我設(shè)置了Book_info數(shù)據(jù)庫(kù)中的book_name
澄峰、book_author
、book_press
三個(gè)字段的全文索引患膛。
中文分詞設(shè)置
接下來設(shè)置中文分詞摊阀,在系統(tǒng)根目錄中查找haystack(windows系統(tǒng)查找對(duì)象是我的電腦,linux系統(tǒng)使用find / -name haystack
)踪蹬,找到C:\Users\cg\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\haystack\backends
python安裝包下的這個(gè)文件夾胞此,在該文件夾下創(chuàng)建ChineseAnalyzer.py
,內(nèi)容如下:
import jieba
from whoosh.analysis import Tokenizer, Token
class ChineseTokenizer(Tokenizer):
def __call__(self, value, positions=False, chars=False,
keeporiginal=False, removestops=True,
start_pos=0, start_char=0, mode='', **kwargs):
t = Token(positions, chars, removestops=removestops, mode=mode,
**kwargs)
seglist = jieba.cut(value, cut_all=True)
for w in seglist:
t.original = t.text = w
t.boost = 1.0
if positions:
t.pos = start_pos + value.find(w)
if chars:
t.startchar = start_char + value.find(w)
t.endchar = start_char + value.find(w) + len(w)
yield t
def ChineseAnalyzer():
return ChineseTokenizer()
復(fù)制whoosh_backend.py
跃捣,將名稱改為whoosh_cn_backend.py
漱牵,打開該文件,引入中文分析類:
from .ChineseAnalyzer import ChineseAnalyzer
查找文件中analyzer=StemmingAnalyzer()
疚漆,將其改為analyzer=ChineseAnalyzer()
完成以上的配置就可以建立索引文件了酣胀,在項(xiàng)目終端下輸入命令重建索引:
python manage.py rebuild_index
創(chuàng)建好索引文件后通過python manage.py update_index
來更新索引文件。
全文索引的使用
更改原來的檢索模板文件:
<form action="/search/" method="GET">
<div class="input-group mb-3">
<input type="text" name="q" class="form-control" autocomplete="off" required placeholder="可檢索字段-書名/作者/出版社">
<div class="input-group-append">
<button class="btn btn-info" type="submit">搜索</button>
</div>
</div>
</form>
上面的action
參數(shù)對(duì)應(yīng)上文在項(xiàng)目urls.py
中設(shè)置的路由娶聘,代表表單提交到全文檢索路由闻镶,input
輸入中的name=q
參數(shù)是haystack的固定寫法,q
代表查詢的關(guān)鍵詞丸升。
用戶提交檢索后铆农,系統(tǒng)將檢索詞提交給haystack,經(jīng)過haystack查詢后狡耻,默認(rèn)結(jié)果返回到項(xiàng)目根目錄下templates/search/search.html
文件墩剖,結(jié)果中主要包含以下關(guān)鍵參數(shù):
query
:查詢的關(guān)鍵詞。
page
:當(dāng)前頁(yè)的page對(duì)象夷狰,通過該對(duì)象獲取查詢的數(shù)據(jù)岭皂。
paginator
:分頁(yè)對(duì)象。
模板中主要代碼如下:
<div class="clearfix">
<div class="alert alert-info">
檢索到關(guān)于: <b>“{{ query }}”</b> 的圖書,當(dāng)前第 <b>{{ page.number }} </b> 頁(yè)
</div>
{# 遍歷檢索圖書結(jié)果 #}
{% for item in page %}
<a href="{% url 'book_detail' %}?ids={{ item.object.book_id }}" style="color:black">
<div class="responsive">
<div class="img">
<img src="{{ item.object.book_pic }}" alt="" width="300" height="200">
<div class="desc">{{ item.object.book_name }}</div>
<div class="desc"><span>{{ item.object.book_press }}</span></div>
</div>
</div>
</a>
{% endfor %}
</div>
{# 分頁(yè) #}
<div class="center">
<ul class="pagination">
{% if page.has_previous %}
<li class="page-item"><a class="page-link" href="?q={{query}}&page={{ page.previous_page_number }}">上一頁(yè)</a></li>
{% endif %}
{% if page.has_next %}
<li class="page-item"><a class="page-link" href="?q={{query}}&page={{ page.next_page_number }}">下一頁(yè)</a></li>
{% endif %}
</ul>
</div>
需要注意的是:通過page對(duì)象遍歷獲取的對(duì)象屬性沼头,需要在中間增加object
爷绘,否則獲取不到對(duì)象的屬性书劝。檢索結(jié)果如下: