1

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

ZendMind
  • 101
  • 1
  • 3
  • 7

5 Answers5

2

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

Anusha
  • 647
  • 11
  • 29
0

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

0

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.

Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
  • Hi, thanks for quick response, it works good but when i use this parameters in php i can't get this result – ZendMind Feb 16 '12 at 12:04
  • Hi, i is this '$result = $solr->search( 'har', 0, 10, $params );' wrong ? is there another parameter for solr suggester ? – ZendMind Feb 17 '12 at 11:14
  • I don't know what library you are using to interact with solr. You need to make sure it sends the write query. Check out this http://www.php.net/manual/en/solrquery.setquery.php – Stelian Matei Feb 17 '12 at 11:50
  • Hi, i don't use the solr extension for PHP but i use Apache Solr 3.5.0. that can be downloaded from [link](http://www.apache.org/dyn/closer.cgi/lucene/solr/) – ZendMind Feb 17 '12 at 12:01
0

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.

Okke Klein
  • 2,549
  • 17
  • 9
  • Hi Klein, thank's for this very interessent link. i don't have problem with my solr but PHP parameters. – ZendMind Feb 21 '12 at 20:56
0

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

ZendMind
  • 101
  • 1
  • 3
  • 7