<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 全文檢索 * 全文檢索不同于特定字段的模糊查詢,使用全文檢索的效率更高,并且能夠對于中文進行分詞處理 * haystack:django的一個包,可以方便地對model里面的內容進行索引、搜索,設計為支持whoosh,solr,Xapian,Elasticsearc四種全文檢索引擎后端,屬于一種全文檢索的框架 * whoosh:純Python編寫的全文搜索引擎,雖然性能比不上sphinx、xapian、Elasticsearc等 * jieba:一款免費的中文分詞包 ## 操作 ### 1.安裝所需包 ```text pip install django-haystack pip install whoosh pip install jieba ``` ### 2.修改settings.py文件 * 添加應用 ```text # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', 'tinymce', 'haystack', ] ``` * 添加搜索引擎 ```text # 添加搜索引擎 HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.whoosh_cn_backend.WhooshEngine', 'PATH': os.path.join(BASE_DIR, 'whoosh_index'), } } #自動生成索引 HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' ``` ### 3.在項目的urls.py中添加url ```text urlpatterns = [ .... url(r'^search/$',include('haystack.urls')), ] ``` #### 4.在應用目錄myapp下獎勵search\_indexes.py文件 ```text from haystack import indexes from .models import Grades class GradesIndex(indexes.SearchIndex,indexes.Indexable): text = indexes.CharField(document=True,use_template=True) def get_model(self): return Grades def index_queryset(self, using=None): return self.get_model().objects.all() ``` ### 5.在目錄“templates/search/indexes/應用名稱”下創建"模型類名稱\_text"文件 ```text # grades_text.txt,列出要對那些內容進行檢索 {{object.gname}} {{object.gdate}} ``` ### 6.在目錄"templates/search"下創建search.html ```text <!DOCTYPE html> <html> <head> <title></title> </head> <body> {% if query %} <h3>搜索結果如下:</h3> {% for result in page.object_list %} <a href="/{{ result.object.id }}/">{{ result.object.sname }}</a><br/> {% empty %} <p>啥也沒找到</p> {% endfor %} {% if page.has_previous or page.has_next %} <div> {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; 上一頁{% if page.has_previous %}</a>{% endif %} | {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}下一頁 &raquo;{% if page.has_next %}</a>{% endif %} </div> {% endif %} {% endif %} </body> </html> ``` ### 7.建立ChineseAnalyzer.py文件 * 在python路徑里,保存在haystack的安裝文件夾下,路徑如:"/lib/site-packages/haystack/backends" ```text 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() ``` ### 8.復制whoosh\_backend.py文件,改名為whoosh\_cn\_backend.py {#8復制whooshbackendpy文件,改名為whooshcnbackendpy} * 注意:復制出來的文件名,末尾會有一個空格,記得要刪除這個空格 ```text from .ChineseAnalyzer import ChineseAnalyzer 查找 analyzer=StemmingAnalyzer() 改為 analyzer=ChineseAnalyzer() ``` ### 9.生成索引 {#9生成索引} * 初始化索引數據 ```text python manage.py rebuild_index ``` ### 10.在模板中創建搜索欄 {#10在模板中創建搜索欄} ```text <form method='get' action="/search/" target="_blank"> <input type="text" name="q"> <input type="submit" value="查詢"> </form> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看