Solr Suggester“自动完成”带有 php 的组件

发布于 2025-01-06 08:02:58 字数 2346 浏览 0 评论 0原文

我使用 apache-solr-3.5.0,我想做一个类似的东西: http://www.kaufda.de/ Berlin

(短语建议)

我使用了 Suggester -(Solr 的灵活“自动完成”组件)

如本文所述:http://css.dzone.com/news/solr-and-autocomplete-part -2

这是我的 solrconfig :

<searchComponent name="suggest" class="solr.SpellCheckComponent">
 <lst name="spellchecker">
  <str name="name">suggest</str>
  <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
  <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
  <str name="field">name_autocomplete</str>
 </lst>
</searchComponent>

<requestHandler name="/suggest" class="org.apache.solr.handler.component.SearchHandler">
 <lst name="defaults">
  <str name="spellcheck">true</str>
  <str name="spellcheck.dictionary">suggest</str>
  <str name="spellcheck.count">10</str>
 </lst>
 <arr name="components">
  <str>suggest</str>
 </arr>
</requestHandler>

Shema.xml

<fieldType class="solr.TextField" name="text_auto">
 <analyzer>
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.LowerCaseFilterFactory"/>
 </analyzer>
</fieldType>

<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="name" type="text" indexed="true" stored="true" multiValued="false" />
<field name="name_autocomplete" type="text_auto" indexed="true" stored="true" multiValued="false" />
<field name="description" type="text" indexed="true" stored="true" multiValued="false" />

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

在我的 php 代码上:

$solr = $this->getSolr();
$response = NULL;

if (!$solr) {
 return;
}

$params = array();
$params['spellcheck.build'] = 'true';
$params['spellcheck'] = 'true';
$params['qt'] = '';

$result = $solr->search( 'har', 0, 10, $params );

结果是一个没有建议的数组。

我如何将 Suggester 与 php 一起使用?

预先感谢您的帮助

干杯

I use apache-solr-3.5.0 and i want make an something like : http://www.kaufda.de/Berlin

(Phrase suggestion)

I used the Suggester - (a flexible "autocomplete" component for Solr)

Like described on this article : http://css.dzone.com/news/solr-and-autocomplete-part-2

This is my solrconfig :

<searchComponent name="suggest" class="solr.SpellCheckComponent">
 <lst name="spellchecker">
  <str name="name">suggest</str>
  <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
  <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
  <str name="field">name_autocomplete</str>
 </lst>
</searchComponent>

<requestHandler name="/suggest" class="org.apache.solr.handler.component.SearchHandler">
 <lst name="defaults">
  <str name="spellcheck">true</str>
  <str name="spellcheck.dictionary">suggest</str>
  <str name="spellcheck.count">10</str>
 </lst>
 <arr name="components">
  <str>suggest</str>
 </arr>
</requestHandler>

Shema.xml

<fieldType class="solr.TextField" name="text_auto">
 <analyzer>
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.LowerCaseFilterFactory"/>
 </analyzer>
</fieldType>

<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="name" type="text" indexed="true" stored="true" multiValued="false" />
<field name="name_autocomplete" type="text_auto" indexed="true" stored="true" multiValued="false" />
<field name="description" type="text" indexed="true" stored="true" multiValued="false" />

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

On my php code :

$solr = $this->getSolr();
$response = NULL;

if (!$solr) {
 return;
}

$params = array();
$params['spellcheck.build'] = 'true';
$params['spellcheck'] = 'true';
$params['qt'] = '';

$result = $solr->search( 'har', 0, 10, $params );

The result is an array without suggestion.

How can i use Suggester with php ?

Thank's in advance for help

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗化ㄋ丶相逢 2025-01-13 08:02:59

您可以使用建议器组件本身,而不是使用拼写检查组件。

<searchComponent name="suggest" class="solr.SuggestComponent">
        <lst name="suggester">
  <str name="name">mySuggester</str>
  <str name="lookupImpl">AnalyzingInfixLookupFactory</str>      <!-- org.apache.solr.spelling.suggest.fst -->
  <str name="lookupImpl">FuzzyLookupFactory</str>  
  <str name="dictionaryImpl">DocumentDictionaryFactory</str>     <!-- org.apache.solr.spelling.suggest.HighFrequencyDictionaryFactory -->
  <str name="field">name</str>
  <str name="weightField">price</str>
  <str name="suggestAnalyzerFieldType">text_general</str>
  <int name="maxEdits">2</int>
 </lst>
</searchComponent>

<requestHandler name="/suggest" class="solr.SearchHandler"  startup="lazy">
<lst name="defaults">
<str name="df">text</str>
  <str name="suggest">true</str>
  <str name="suggest.count">10</str>
  <str name="suggest.dictionary">mySuggester</str>
</lst>
<arr name="last-components">
  <str>suggest</str>
</arr>
</requestHandler>

注意:AnalyzingInfixLookupFactory 还允许您搜索中缀。假设您的搜索项是 Squash 并且用户输入 uash,Squash 将作为建议提供。

即使用户犯了拼写错误,FuzzyLookupFactory也将允许您提供建议。

这些是在 solrconfig.xml 文件中进行的更改,从 php 创建实例也在本页的其他答案中得到了很好的解释。所以我跳过那部分。希望这有帮助。

编辑:如果您想将实例创建为 $result,您还必须在 Service.php 中编写一个建议服务,该服务与搜索服务非常相似= $solr->**suggest**( 'har', 0, 10, $params ); 而不是 $result = $solr->**search**( 'har' , 0, 10, $参数);

Instead of using the spellcheck component, you can use the suggester component itself.

<searchComponent name="suggest" class="solr.SuggestComponent">
        <lst name="suggester">
  <str name="name">mySuggester</str>
  <str name="lookupImpl">AnalyzingInfixLookupFactory</str>      <!-- org.apache.solr.spelling.suggest.fst -->
  <str name="lookupImpl">FuzzyLookupFactory</str>  
  <str name="dictionaryImpl">DocumentDictionaryFactory</str>     <!-- org.apache.solr.spelling.suggest.HighFrequencyDictionaryFactory -->
  <str name="field">name</str>
  <str name="weightField">price</str>
  <str name="suggestAnalyzerFieldType">text_general</str>
  <int name="maxEdits">2</int>
 </lst>
</searchComponent>

<requestHandler name="/suggest" class="solr.SearchHandler"  startup="lazy">
<lst name="defaults">
<str name="df">text</str>
  <str name="suggest">true</str>
  <str name="suggest.count">10</str>
  <str name="suggest.dictionary">mySuggester</str>
</lst>
<arr name="last-components">
  <str>suggest</str>
</arr>
</requestHandler>

Note: AnalyzingInfixLookupFactory allows you to search for infixes as well. Suppose your search item is Squash and the user types uash, Squash will be provided as a suggestion.

FuzzyLookupFactory will allow you to provide suggestion even when the user does a spelling mistake.

These are the changes to be done in the solrconfig.xml file, creating an instance from php are well explained in the other answers on this page as well. So I skip that part. Hope this helps.

EDIT: You will also have to write a suggest service in Service.php which is very similar to the search service if you want to create the instance as $result = $solr->**suggest**( 'har', 0, 10, $params ); instead of $result = $solr->**search**( 'har', 0, 10, $params );

那小子欠揍 2025-01-13 08:02:59

您是否尝试直接在 solr 上进行测试?最好看看这些值是否正确生成,然后您就可以调试 PHP 代码。

您可以通过访问以下内容查看这些值:

http://localhost:8983/solr/suggest?q=har&spellcheck=true&spellcheck.collat​​e=true&spellcheck.build=true

您可能需要如果您不使用默认配置,请更改端口。

Did you try to do a test directly on solr? That would be best to see if the values are generated properly and then you can debug the PHP code.

You can see the values by accessing:

http://localhost:8983/solr/suggest?q=har&spellcheck=true&spellcheck.collate=true&spellcheck.build=true

You might need to change the port if you are not using the default configuration.

宁愿没拥抱 2025-01-13 08:02:59

查看 http://www.cominvent.com/ 2012/01/25/super-flexible-autocomplete-with-solr/ 更灵活的建议器。

也不建议每次询问建议时都构建字典(spellcheck.build=true)。

Check out http://www.cominvent.com/2012/01/25/super-flexible-autocomplete-with-solr/ for an even more flexible suggester.

Also building dictionary (spellcheck.build=true) every time you ask for suggestion is not recommended.

别低头,皇冠会掉 2025-01-13 08:02:59

经过搜索并在您的帮助下,我找到了解决方案。

参数是正确的。

网址: http://本地主机:8983/solr/suggest?q=har&spellcheck=true&spellcheck.collat​​e=true&spellcheck.build=true
给出良好的结果

在我的 php 代码中,我添加了带有“/suggest”值的 qt 参数

新代码

$solr = $this->getSolr();
$response = NULL;

if (!$solr) {
 return;
}

$params = array();
$params['spellcheck.build'] = 'true';
$params['spellcheck'] = 'true';
$params['qt'] = '/suggest';

$result = $solr->search( 'har', 0, 10, $params );

我感谢您的帮助,

干杯

After searching and with all your help, i found the solution.

The params are correct.

The url : http://localhost:8983/solr/suggest?q=har&spellcheck=true&spellcheck.collate=true&spellcheck.build=true
give the good result

On my php code i added the qt param with the '/suggest' value

The new code

$solr = $this->getSolr();
$response = NULL;

if (!$solr) {
 return;
}

$params = array();
$params['spellcheck.build'] = 'true';
$params['spellcheck'] = 'true';
$params['qt'] = '/suggest';

$result = $solr->search( 'har', 0, 10, $params );

I appreciate your help,

Cheers

深居我梦 2025-01-13 08:02:59

使用搜索组件而不是拼写检查

在 solrconfig.xml 中添加此代码 在 Solarconfig.xml 中

<searchComponent name="suggest" class="solr.SuggestComponent">
    <lst name="suggester">
      <str name="name">mainSuggester</str>
      <str name="lookupImpl">FuzzyLookupFactory</str>      
      <str name="dictionaryImpl">DocumentDictionaryFactory</str>
      <str name="field">searchable_field</str>
      <str name="weightField">price</str>
      <str name="suggestAnalyzerFieldType">text_general</str>
      <str name="queryAnalyzerFieldType">phrase_suggest</str>
      <str name="buildOnStartup">false</str>
    </lst>
  </searchComponent>

指定建议处理程序

<requestHandler name="/suggest" class="solr.SearchHandler" 
  startup="lazy" >
  <lst name="defaults">
    <str name="suggest">true</str>
    <str name="suggest.count">10</str>
  </lst>
  <arr name="components">
    <str>suggest</str>
  </arr>
</requestHandler>

在 try 块内的 php 中使用以下代码

try {
        $input = $request->all();
        $query_param = $input['search'];
        $query = $this->client->createSuggester();
        $query->setQuery($query_param); //multiple terms
        $query->setDictionary('mainSuggester');
        $query->setCount(10);
        $query->setBuild(true);
        $resultset = $this->client->suggester($query);
        $docs = $resultset->getResponse();
        $data = json_decode($docs->getBody());$message, $code);
    }

其中 $query_param 是请求。 mainSuggester 是 solrconfig.xml 文件中指定的字典名称
这里 $data 将返回整个建议的数组对象

Use the search component instead of spell check

Add this code in your solrconfig.xml

<searchComponent name="suggest" class="solr.SuggestComponent">
    <lst name="suggester">
      <str name="name">mainSuggester</str>
      <str name="lookupImpl">FuzzyLookupFactory</str>      
      <str name="dictionaryImpl">DocumentDictionaryFactory</str>
      <str name="field">searchable_field</str>
      <str name="weightField">price</str>
      <str name="suggestAnalyzerFieldType">text_general</str>
      <str name="queryAnalyzerFieldType">phrase_suggest</str>
      <str name="buildOnStartup">false</str>
    </lst>
  </searchComponent>

Specify a handler for suggest in solarconfig.xml

<requestHandler name="/suggest" class="solr.SearchHandler" 
  startup="lazy" >
  <lst name="defaults">
    <str name="suggest">true</str>
    <str name="suggest.count">10</str>
  </lst>
  <arr name="components">
    <str>suggest</str>
  </arr>
</requestHandler>

And in php inside your try block use the follwoing code

try {
        $input = $request->all();
        $query_param = $input['search'];
        $query = $this->client->createSuggester();
        $query->setQuery($query_param); //multiple terms
        $query->setDictionary('mainSuggester');
        $query->setCount(10);
        $query->setBuild(true);
        $resultset = $this->client->suggester($query);
        $docs = $resultset->getResponse();
        $data = json_decode($docs->getBody());$message, $code);
    }

Where $query_param is the request. And mainSuggester is the dictionary name as specified in the solrconfig.xml file
Here $data will return you the entire suggested array object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文