如何让建议组件在 SolrNet 中工作? [英] How to get the suggester component working in SolrNet?

查看:21
本文介绍了如何让建议组件在 SolrNet 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have configured my solrconfig.xml and schema.xml to query for the suggestions.

我能够从 url 获得建议

I am able to get the suggestions from the url

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

我的 SolrConfig.xml 看起来像

My SolrConfig.xml looks like

目前,我的 solr 查询看起来像

Curently, My solr query looks like

<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 看起来像这样

And my schema.xml looks like this

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

我调用 SolrNet API 的代码如下

My code to call the SolrNet API looks as below

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;

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

However, I am not getting any data. results count is zero. And also the spellcheck in the results is also zero.

我也没有在结果中看到建议列表.

I also dont see the suggestion list inside the results.

请帮忙

推荐答案

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

I had the exact same requirement but could not find any way to easily handle Suggester results with SolrNet. Unfortunately, SolrNet seems to be built around the default /select request handler and does not currently support any other handler including /suggest for object type mappings (T). It expects all mappings to occur with indexed Solr document results and not suggester results.

因此,@Paige Cook 的回答 对我不起作用.T 类型与映射与建议结果响应不兼容.从初始化请求(Startup.Init())到查询(ISolrQueryResults results = solr.Query())的所有标准管道代码都需要一个映射 Solr 文档类型,而不是建议者提供的简单字符串数组.

Hence, @Paige Cook's answer did not work for me. T type with mappings is not compatible with a suggester results response. All the standard plumbing code from initializing the request (Startup.Init<T>()) to querying (ISolrQueryResults<T> results = solr.Query()) needs a mapped Solr document type and not a simple array of strings which the suggester provides.

因此,(类似于 @dfay)我发起了一个网络请求并从XML 网络响应.SolrConnection 类用于此:

Therefore, (similar to @dfay) I went with making a web request and parsing out the suggested results from the XML web response. The SolrConnection class was used for this:

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

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

Alternatively, instead of XML, the request can return a JSON response using the wt=json parameter:

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

这篇关于如何让建议组件在 SolrNet 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆