2013-01-07 50 views
5

我已經配置了我的solrconfig.xml和schema.xml來查詢建議。如何讓SolrNet中的建議器組件工作?

我能夠從URL中得到的建議

http://localhost:8080/solr/collection1/suggest?q=ha&wt=xml 

我solrconfig.xml中看起來像

Curently,我Solr的查詢看起來像

<fields> 
    <!-- declare fields of entity class --> 
    <!-- type will specify the table name --> 
    <field name="type" type="string" indexed="true" stored="true" /> 

    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
    <field name="name" type="text_general" indexed="true" stored="true" omitNorms="true"/> 

    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/> 
    <field name="_version_" type="long" indexed="true" stored="true"/> 

    <!-- unique field --> 
    <field name="uid" type="uuid" indexed="true" stored="true" /> 

    </fields> 

    <uniqueKey>uid</uniqueKey> 

    <copyField source="name" dest="text"/> 

    <types> 
    <fieldType name="uuid" class="solr.UUIDField" indexed="true" /> 
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> 
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/> 

    <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> 
    ..... 
    </types> 

而我的schema.xml看起來像這樣

<searchComponent name="suggest" class="solr.SpellCheckComponent"> 
    <!-- a spellchecker built from a field of the main index --> 
    <lst name="spellchecker"> 
     <str name="name">suggest</str> 
     <str name="field">name</str> 
     <str name="classname">org.apache.solr.spelling.suggest.Suggester</str> 
     <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str> 
     <str name="buildOnCommit">true</str>   
     <str name="distanceMeasure">internal</str> 
     <float name="accuracy">0.5</float> 
     <int name="maxEdits">2</int> 
     int name="minPrefix">1</int> 
     <int name="maxInspections">5</int> 
     <int name="minQueryLength">4</int> 
     <float name="maxQueryFrequency">0.01</float> 
     <float name="thresholdTokenFrequency">.01</float>  
    </lst> 

    <!-- a spellchecker that can break or combine words. See "/spell" handler below for usage --> 
    <lst name="spellchecker"> 
     <str name="name">wordbreak</str> 
     <str name="classname">solr.WordBreakSolrSpellChecker</str> 
     <str name="field">name</str> 
     <str name="combineWords">true</str> 
     <str name="breakWords">true</str> 
     <int name="maxChanges">10</int> 
    </lst> 
</searchComponent> 

<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy"> 
    <lst name="defaults"> 
     <str name="df">text</str> 
     <!-- Solr will use suggestions from both the 'default' spellchecker 
      and from the 'wordbreak' spellchecker and combine them. 
      collations (re-written queries) can include a combination of 
      corrections from both spellcheckers --> 
     <str name="spellcheck">true</str> 
     <str name="spellcheck.dictionary">suggest</str> 
     <!--<str name="spellcheck.dictionary">wordbreak</str>--> 
     <str name="spellcheck">on</str> 
     <str name="spellcheck.extendedResults">true</str>  
     <str name="spellcheck.count">10</str> 
     <str name="spellcheck.alternativeTermCount">5</str> 
     <str name="spellcheck.maxResultsForSuggest">5</str>  
     <str name="spellcheck.collate">true</str> 
     <str name="spellcheck.collateExtendedResults">true</str> 
     <str name="spellcheck.maxCollationTries">10</str> 
     <str name="spellcheck.maxCollations">5</str>   
    </lst> 
    <arr name="last-components"> 
     <str>spellcheck</str> 
    </arr> 
    </requestHandler> 

我的代碼來調用API SolrNet看起來如下

new SolrBaseRepository.Instance<T>().Start(); 
     var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>(); 
     var options = new QueryOptions 
     { 
      FilterQueries = new ISolrQuery[] { new SolrQueryByField("type", type) } 
     }; 
     var results = solr.Query(keyword, options); 
     return results; 

但是,我沒有得到任何數據。 結果計數爲零。結果中的拼寫檢查也爲零​​。

我也沒有看到結果中的建議列表。

enter image description here

請幫

回答

2

爲了執行你對你已經設置了/suggest請求處理查詢時,您將需要設置使用ExtraParameters的qt Solr的參數在SolrNet QueryOptions象下面這樣:

new SolrBaseRepository.Instance<T>().Start(); 
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>(); 
var options = new QueryOptions 
{ 
    FilterQueries = new ISolrQuery[] { new SolrQueryByField("type", type) }, 
    ExtraParams = new Dictionary<string, string>{{"qt", "suggest"}}, 
}; 
var results = solr.Query(keyword, options); 
return results; 

否則您的查詢與標準/select請求處理程序仍在執行(或者你有DEFI在solrconfig.xml中默認爲默認)。

+0

我按建議進行了更改,但仍未得到結果 – Prasad

+0

您在SolrNet查詢中爲關鍵字和類型使用了什麼值?此外,你是否直接針對Solr運行相同的查詢。我從您的問題中看到,您使用'q = ha'運行了一個,嘗試添加篩選器查詢以確保其按預期工作......可能會出現這種情況,當您使用不想過濾的建議程序查詢結果... –

1

請參閱http://wiki.apache.org/solr/SolrRequestHandler,特別是舊的handleSelect = true行爲部分。如果你正在運行一個新的Solr服務器,這很可能是你的問題。 (即設置爲「QT」有沒有效果,無論是在SolrNet的默認處理程序必須改變,否則Solr的配置需要設置handleSelect = TRUE)。以下是我解決了我的情況下,這個問題:

ISolrConnection connection = ServiceLocator.Current.GetInstance<ISolrConnection>(); 
List<KeyValuePair<string, string>> termsParams = new List<KeyValuePair<string, string>>(); 
termsParams.Add(new KeyValuePair<string, string>("terms.fl", "name")); 
termsParams.Add(new KeyValuePair<string, string>("terms.prefix", mySearchString)); 
termsParams.Add(new KeyValuePair<string, string>("terms.sort", "count")); 
string xml = connection.Get("/terms", termsParams); 

ISolrAbstractResponseParser<Document> parser = ServiceLocator.Current.GetInstance<ISolrAbstractResponseParser<Document>>(); 
SolrQueryResults<Document> results = new SolrQueryResults<Document>(); 
parser.Parse(System.Xml.Linq.XDocument.Parse(xml), results); 

TermsResults termResults = results.Terms; 
foreach (TermsResult result in termResults) 
{ 
    foreach (KeyValuePair<string, int> kvp in result.Terms) 
    { 
     //... do something with keys 
    } 
} 

基本上我使用SolrNet解析器和連接代碼,但不是查詢的東西。希望這可以幫助。

5

我有完全相同的要求,但無法找到任何方式來輕鬆處理SolrNet的Suggester結果。不幸的是,SolrNet似乎圍繞默認的/select請求處理程序構建,並且目前不支持任何其他處理程序,包括用於對象類型映射的/suggestT)。它期望所有映射都與索引的Solr文檔結果一起發生,而不是建議結果。因此,@Paige Cook's answer不適合我。帶映射的T類型與建議者結果響應不兼容。從初始化請求(Startup.Init<T>())到查詢(ISolrQueryResults<T> results = solr.Query())的所有標準管道代碼都需要映射的Solr文檔類型,而不是建議者提供的簡單字符串數組。

因此,(類似於@dfay)我提出了Web請求並解析了XML Web響應中的建議結果。使用了SolrConnection類此:

string searchTerm = "ha"; 
string solrUrl = "http://localhost:8080/solr/collection1"; 
string relativeUrl = "/suggest"; 
var parameters = new Dictionary<string, string> 
                { 
                    {"q", searchTerm}, 
                    {"wt", "xml"}, 
                }; 

var solrConnection = new SolrConnection(solrUrl); 
string response = solrConnection.Get(relativeUrl, parameters); 
// then use your favorite XML parser to extract 
// suggestions from the reponse string 

或者,代替XML,該請求可以使用wt=json參數返回的JSON響應:

var parameters = new Dictionary<string, string> 
                { 
                    {"q", searchTerm}, 
                    {"wt", "json"}, // change this! 
                }; 
// then use your favorite JSON parser 
0

傳遞qt的參數不工作,至少即使在SolrConfig中使用handleSelect = true,也不在Solr 4.7中。您可以通過指定與默認/選擇非常不同的自定義處理程序進行驗證,例如讓您使用edismax並在ExtraParams中發送debugQuery = true並在Fiddler中捕獲結果。

此外,如果您閱讀了handleSelect標誌的解釋,它會說「如果請求使用」/選擇「但沒有該名稱的請求處理程序」。

您不想觸摸或禁用/ select處理程序,因爲Solr自己使用它。

我最終使用ExtraParams來傳遞我在自定義處理程序中定義的所有值,但沒有那麼多。看起來不僅僅是使用部分SolrNET,然後進行結果分析。