2013-01-07 9 views
5

Ho configurato il mio solrconfig.xml e schema.xml per richiedere i suggerimenti.Come far funzionare il componente suggester in SolrNet?

io sono in grado di ottenere i suggerimenti dal URL

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

mio SolrConfig.xml sembra

curently, La mia domanda solr sembra

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

E il mio schema.xml assomiglia a questo

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

Il mio codice per chiamare l'API SolrNet appare come di seguito

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; 

Tuttavia, io non sto ottenendo alcun dato. Il conteggio dei risultati è zero. Anche il controllo ortografico nei risultati è zero.

Inoltre non vedo l'elenco di suggerimenti all'interno dei risultati.

enter image description here

Si prega di aiutare

risposta

2

Per eseguire la query contro il gestore /suggest richiesta di avere l'installazione, è necessario impostare il parametro qt Solr utilizzando i ExtraParameters nelle vostre QueryOptions SolrNet come qui di seguito:

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; 

In caso contrario la vostra richiesta è ancora in esecuzione nei confronti del gestore di serie /select richiesta (o qualsiasi altra cosa si ha defi ned come predefinito nel tuo solrconfig.xml).

+0

Ho apportato le modifiche come suggerito, ma non ottengo ancora i risultati – Prasad

+0

Quali valori stai utilizzando per la parola chiave e il tipo nella tua query SolrNet? Inoltre, hai eseguito la stessa query direttamente contro Solr. Vedo dalla tua domanda che ne esegui una usando 'q ​​= ha', prova ad aggiungere la query del filtro per assicurarti che funzioni come previsto ... Potrebbe essere nel caso in cui usi il suggeritore che non vuoi filtrare i risultati della query ... –

1

Vedere http://wiki.apache.org/solr/SolrRequestHandler, in particolare la sezione sul vecchio handleSelect = true behavior. Se stai andando contro un nuovo server Solr, questo è probabilmente il tuo problema. (Vale a dire l'impostazione "qt" non ha alcun effetto e né il gestore predefinito in SolrNet deve essere cambiato o la configurazione Solr ha bisogno di impostare handleSelect = true.) Ecco come ho risolto questo problema nel mio caso:

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

Fondamentalmente io utilizzare il parser SolrNet e il codice di connessione, ma non le query. Spero che questo ti aiuti.

5

Ho avuto esattamente lo stesso requisito ma non ho trovato alcun modo per gestire facilmente i risultati Suggester con SolrNet. Sfortunatamente, SolrNet sembra essere costruito attorno al gestore di richieste di default /select e al momento non supporta nessun altro gestore incluso /suggest per i mapping dei tipi di oggetto (T). Si aspetta che tutte le mappature si verifichino con i risultati del documento Solr indicizzati e non con i risultati suggeriti.

Quindi, @Paige Cook's answer non ha funzionato per me. Il tipo T con mapping non è compatibile con una risposta ai risultati suggeritore. Tutto il codice idraulico standard dall'inizializzazione della richiesta (Startup.Init<T>()) alla query (ISolrQueryResults<T> results = solr.Query()) richiede un tipo di documento Solr mappato e non un semplice array di stringhe fornito da suggester.

Pertanto, (simile a @dfay) sono andato con una richiesta Web e analizzando i risultati suggeriti dalla risposta Web XML. La classe SolrConnection è stato utilizzato per questo:

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 

In alternativa, invece di XML, la richiesta può restituire una risposta JSON utilizzando il parametro wt=json:

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

passando il parametro qt non funziona, almeno non in Solr 4.7 anche con handleSelect = true in SolrConfig. Puoi verificare specificando un gestore personalizzato che è molto diverso da quello predefinito/seleziona, ad esempio fai usare il tuo edismax e invia debugQuery = true in ExtraParams e cattura i risultati in Fiddler.

Inoltre, se si legge la spiegazione sul flag handleSelect, viene indicato "se la richiesta utilizza"/select "ma non esiste alcun gestore di richieste con tale nome".

Non si desidera toccare o disattivare il gestore/select perché Solr lo utilizza da solo.

Ho finito per usare ExtraParams per passare tutti i valori che ho definito nel mio gestore personalizzato, non ce n'erano molti. Sembrava meglio di usare solo parte di SolrNET e poi fare l'analisi dei risultati.