<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 4. 全文檢索系統之 pg\_search 實現 #### 1. 前言1 [pg\_search](https://github.com/Casecommons/pg_search)是一個用于**PostgreSQL**全文檢索的gem,它使用起來簡單,功能也很強大。 以本站為例,文章都是放在articles這張表中,文章有標題和內容,即title和body,現在要對這兩個做全文檢索。 #### 2. 安裝和使用 安裝gem ``` gem 'pg_search' ``` 查看**pg\_search**的readme文檔就可以知道。它主要有兩種使用方式,分別是Multi-search和search scopes。Multi-search是適合于網站比較復雜,例如多張表,要把多張表揉在一起,放到一張表來做查找。現在我們的網站簡單,不需要這個功能,所以我們來看看search scopes的用法。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => [:title, :body] end ``` 用`rails console`創建一些文檔,之后就能用`Article.search_by_title_or_body`來搜索了。 這樣再結合表單就能實現一個搜索系統的。 #### 3. 其他功能 **pg\_search**還有其他強大的功能。我們來介紹一下。 ##### 3.1 關聯(Searching through associations) 我們現在是在articles這張上做查詢,那是因為我們的網站簡單,但有時候是要跨表的,那也很簡單。比如,article是has\_many :tags的,就可以這樣。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => [:title, :body], :associated_against => { :tags => [:name], } end ``` 可以查看log看具體做了什么操作。其實就是joins之類。 ##### 3.2 字典和中文支持(dictionary) 這篇文章[PostgreSQL的全文檢索系統之中文支持(三)](http://www.rails365.net/articles/postgresql-de-quan-wen-jian-suo-xi-tong-zhi-zhong-wen-zhi-chi-san)有介紹**PostgreSQL**中文支持。 安裝好那個中文插件,和pg\_search結合那太簡單了,指定**dictionary**就好了。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => [:title, :body], :associated_against => { :tags => [:name], }, :using => { :tsearch => {:dictionary => "testzhcfg"} } end ``` ##### 3.3 權重(Weighting) 可以給需要搜索的項加上優先級,比如,標題要優先于內容。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => { :title => 'A', :body => 'B' }, :associated_against => { :tags => [:name], }, :using => { :tsearch => {:dictionary => "testzhcfg"} } end ``` 可以看看日志,如果有類似于"setweight"的輸出,說明成功了。 ##### 3.4 前綴(prefix) 假如要搜索一個詞,例如rails,但是忘了怎么拼寫,只記得前兩個單詞,那就是ra,但輸入ra時也能找到關于rails的文章,這就是前綴的作用。使用也很簡單。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => { :title => 'A', :body => 'B' }, :associated_against => { :tags => [:name], }, :using => { :tsearch => {:dictionary => "testzhcfg", :prefix => true} } end ``` ##### 3.5 否定(negation) 否定就是可以搜索不包含的內容,比如!ruby,就是不搜索ruby,其他的都搜索,一個相反過程啦。 ``` class Article < ActiveRecord::Base include PgSearch pg_search_scope :search_by_title_or_body, :against => { :title => 'A', :body => 'B' }, :associated_against => { :tags => [:name], }, :using => { :tsearch => {:dictionary => "testzhcfg", :prefix => true, :negation => true} } ``` 還有其他各種用法,normalization用于排序,any\_word是否匹配任何一個,dmetaphone模糊匹配等。 完結。
                  <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>

                              哎呀哎呀视频在线观看