欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

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

什么是全文檢索

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

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

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

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

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

解決分詞問題

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

遇到的問題

如果是在一些VPS上測試的時候非常慢的話可能是內(nèi)存不足,比如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()

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

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

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

廣告

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

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