如何让SolrNet中的建议器组件工作?

Pra*_*sad 5 .net c# solr solrnet

我已经配置了solrconfig.xml和schema.xml来查询建议.

我能够从网址获得建议

http://localhost:8080/solr/collection1/suggest?q=ha&wt=xml
Run Code Online (Sandbox Code Playgroud)

我的SolrConfig.xml看起来像

当然,我的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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我调用SolrNet API的代码如下所示

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;
Run Code Online (Sandbox Code Playgroud)

但是,我没有得到任何数据.结果计数为零.而且结果中的拼写检查也是零.

我也没有在结果中看到建议清单.

在此输入图像描述

请帮忙

Ray*_*ega 5

我有完全相同的要求,但找不到任何方法可以轻松处理SolrNet的建议结果.不幸的是,SolrNet似乎是围绕默认/select请求处理程序构建的,目前不支持任何其他处理程序,包括 /suggest对象类型映射(T).它希望所有映射都与索引的Solr文档结果一起发生,而不是建议结果.

因此,@ Paige Cook的答案对我不起作用.T带映射的类型与建议者结果响应不兼容.从初始化request(Startup.Init<T>())到querying(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
Run Code Online (Sandbox Code Playgroud)

或者,请求可以使用以下wt=json参数返回JSON响应,而不是XML :

var parameters = new Dictionary<string, string>
????????????????{
????????????????????{"q", searchTerm},
????????????????????{"wt", "json"}, // change this!
????????????????};
// then use your favorite JSON parser
Run Code Online (Sandbox Code Playgroud)


Pai*_*ook 2

为了针对/suggest您设置的请求处理程序执行查询,您需要qt使用 SolrNet QueryOptions 中的 ExtraParameters 设置 Solr 参数,如下所示:

 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;
Run Code Online (Sandbox Code Playgroud)

否则,您的查询仍在针对标准/select请求处理程序(或您在 solrconfig.xml 中定义为默认值的任何处理程序)执行。