如何在Django中使用Whoosh進(jìn)行全文檢索-創(chuàng)新互聯(lián)

這篇文章給大家介紹如何在Django中使用Whoosh進(jìn)行全文檢索,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)公司專注于伊州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供伊州營(yíng)銷型網(wǎng)站建設(shè),伊州網(wǎng)站制作、伊州網(wǎng)頁(yè)設(shè)計(jì)、伊州網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開發(fā)服務(wù),打造伊州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供伊州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

Whoosh 是純Python實(shí)現(xiàn)的全文搜索引擎,通過(guò)Whoosh可以很方便的給文檔加上全文索引功能。

什么是全文檢索

簡(jiǎn)單講分為兩塊,一塊是分詞,一塊是搜索。比如下面一段話:

上次舞蹈演出直接在上海路的弄堂里

比如我們現(xiàn)在想檢索上次的演出,通常我們會(huì)直接搜索關(guān)鍵詞: 上次演出 ,但是使用傳統(tǒng)的SQL like 查詢并不能命中上面的這段話,因?yàn)樵?上次 和 演出 中間還有 舞蹈 。然而全文搜索卻將上文切成一個(gè)個(gè)Token,類似:

上次/舞蹈/演出/直接/在/上海路/的/弄堂/里

切分成Token后做反向索引(inverted indexing),這樣我們就可以通過(guò)關(guān)鍵字很快查詢到了結(jié)果了。

解決分詞問(wèn)題

分詞是個(gè)很有技術(shù)難度的活,比如上面的語(yǔ)句中一個(gè)難點(diǎn)就是到底是 上海路 還是 上海 呢?Python有個(gè)中文分詞庫(kù): 結(jié)巴分詞 ,我們可以通過(guò)結(jié)巴分詞來(lái)完成索引中分詞工作,結(jié)巴分詞提供了Whoosh的組件可以直接集成,代碼示例

遇到的問(wèn)題

如果是在一些VPS上測(cè)試的時(shí)候非常慢的話可能是內(nèi)存不足,比如512MB做一個(gè)博客索引非常慢,嘗試升級(jí)到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()

關(guān)于如何在Django中使用Whoosh進(jìn)行全文檢索就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)頁(yè)題目:如何在Django中使用Whoosh進(jìn)行全文檢索-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://muchs.cn/article22/dchejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、小程序開發(fā)、服務(wù)器托管、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作