2013-02-14 77 views
0

我想提供一個文本搜索來查找城市,根據他們的名字和他們屬於的國家的名稱。模糊「開始」搜索Solr和太陽黑子在多個詞

舉例來說,我想回到Milan, Italy與條款mil ita,或者也Los Angeles, United Stateslos ang uni搜索的搜索。

這些都是當前車型我已經(驗證略):

class City < ActiveRecord::Base 
    belongs_to :country 

    searchable do 
    text :name 
    text :country do 
     country.name 
    end 
    end 
end 

class Country < ActiveRecord::Base 
end 

這是我的Solr schema.xml文件的相關部分:

<fieldtype class="solr.TextField" positionIncrementGap="100" name="text"> 
    <analyzer type="index"> 
    <tokenizer class="solr.StandardTokenizerFactory"/> 
    <filter class="solr.StandardFilterFactory"/> 
    <filter class="solr.LowerCaseFilterFactory"/> 
    <!--<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>--> 
    <filter class="solr.ISOLatin1AccentFilterFactory"/> 
    <filter class="solr.TrimFilterFactory" /> 
    <filter class="solr.EdgeNGramFilterFactory" 
      minGramSize="3" 
      maxGramSize="30"/> 
    </analyzer> 
    <analyzer type="query"> 
    <tokenizer class="solr.StandardTokenizerFactory"/> 
    <filter class="solr.StandardFilterFactory"/> 
    <filter class="solr.LowerCaseFilterFactory"/> 
    <!--<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>--> 
    <filter class="solr.ISOLatin1AccentFilterFactory"/> 
    <filter class="solr.TrimFilterFactory" /> 
    </analyzer> 
</fieldtype> 

我在控制器中執行搜索:

search = City.search do 
    fulltext params[:q] 
    paginate page: 1, per_page: 10 
end 

這裏會發生什麼,如果是我尋找mil我會得到Milan, Italy結果,但搜索mil ita將返回什麼。

任何想法?

預先感謝您,

r。

回答

0

找到一個解決方案,我想我會分享。

所有你需要的是建立一個「自定義字段」所有你想要啓用搜索,像這樣(在城市模型)的文本:

searchable do 
    text :composite_name do 
    "#{name} #{country.name}" 
    end 
end 

這工作。

r。