2

I am trying to implement auto complete feature using Solr 5.3.0

solrconfig.xml looks like this

<searchComponent name="suggest" class="solr.SuggestComponent">
  <lst name="suggester">
    <str name="name">default</str>
    <str name="lookupImpl">FuzzyLookupFactory</str>
    <str name="dictionaryImpl">DocumentDictionaryFactory</str>
    <str name="field">suggest_ngram</str>
    <str name="weightField">price</str>
    <str name="suggestAnalyzerFieldType">text_suggest_ngram</str>
    <str name="buildOnStartup">true</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>

managed-schema looks like this:

<fieldType name="text_suggest_ngram" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.StandardTokenizerFactory"/>
  <filter class="solr.LowerCaseFilterFactory"/>
  <filter class="solr.EdgeNGramFilterFactory" maxGramSize="10" minGramSize="2" />
</analyzer>
<analyzer type="query">
  <tokenizer class="solr.StandardTokenizerFactory"/>
  <filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>


<field name="suggest_ngram" type="text_suggest_ngram" indexed="true" stored="false"/>
<field name="name" type="string" multiValued="false" indexed="true" stored="true"/>
<field name="price" type="tlong" multiValued="false" indexed="true" stored="true"/>
<copyField source="name" dest="suggest_ngram"/>

Now when I use the analyzer from the admin panel of Solr, I can see the indexed ngrams. And it successfully points out the match.

However when I use the query:

http://localhost:8983/solr/products/suggest?suggest=true&suggest.build=true&wt=json&suggest.q=Jind

I get 0 suggestions. The response is here: https://api.myjson.com/bins/47r3i

There exists a value "Jindal Panther" for the name key in one of the docs.

Moreover, I have found that if I create a dummy copyfield "suggest" with type as "String", with source as "name", any suggestion that works fine on "name" will not work on "suggest". Can this be any misconfiguration of copyfield to enable suggestions?

Any help would be appreciated. Thanks in advance.

EDIT: Got the solution. See the accepted answer and its comments below. There is a blog that I encountered that beautifully explains Suggesters. It is definitely worth reading for a newbie to Solr Search.

https://lucidworks.com/blog/2015/03/04/solr-suggester/

Nikhil Sahu
  • 2,463
  • 2
  • 32
  • 48

1 Answers1

4

The field on which you want to configure the suggester should be store=true. It need not to be indexed. The suggester configuration will build a dictionary according to the provide configuration in the suggestComponet. The name field have stored as true where as suggest_ngram is not. You need to update the schema configuration like this:

<field name="suggest_ngram" type="text_suggest_ngram" indexed="false" stored="true"/>

Also you need to provide the parameter suggest.dictionary, the dictionary you are using for suggestions. For you it is names as default.

http://localhost:8983/solr/products/suggest?suggest=true&
       suggest.build=true&
       wt=json&
       suggest.dictionary=default&
       suggest.q=Jind

OR you can provide the dictionary configuration in requestHandler of /suggest:

<str name="suggest.dictionary">default</str>
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • I am able to get the suggestions now from the start of the value. But I also want to enable suggestions from a word present in the middle. But the query : `http://localhost:8983/solr/products/suggest?suggest=true&suggest.build=true&wt=json&suggest.q=Pan` does not gives any results. – Nikhil Sahu Oct 08 '15 at 09:55
  • what is the current `lookupImpl` implementation you are using? – YoungHobbit Oct 08 '15 at 09:57
  • FuzzyLookupFactory Since the above mentioned config did not work, i switched to getting suggestions from the name field and not from the copy field that has n grams enabled. – Nikhil Sahu Oct 08 '15 at 09:59
  • `AnalyzingInfixLookupFactory` can you please try with this. This suggests matches based on prefix matches to any tokens in the indexed text. You need to reload the core, don't forget that – YoungHobbit Oct 08 '15 at 10:00
  • 1
    That did it. Thanks a lot :) – Nikhil Sahu Oct 08 '15 at 10:14
  • Can I not configure two separate dictionaries on different fields using `AnalyzingInfixLookupFactory`? It throws exception when I try to. `msg=SolrCore 'products' is not available due to init failure: org.apache.lucene.store.LockObtainFailedException: Lock held by this virtual machine: /home/nikhil/Documents/solr-5.3.0/server/solr/products/data/analyzingInfixSuggesterIndexDir/write.lock` – Nikhil Sahu Oct 09 '15 at 14:22
  • Got it now. Needed to specify different directories for storing indexes. – Nikhil Sahu Oct 09 '15 at 14:40
  • @nikhil24 Yes, using the `indexPath` parameter. – YoungHobbit Oct 09 '15 at 15:44
  • Can I not return the other data (like doc id etc.) alongwith the suggestion? Do I need to implement suggest differently from as above in order to do so? – Nikhil Sahu Oct 13 '15 at 14:24
  • @nikhil24 You can send one field in the payload tag like `YourField` in the `searchComponent` definition. – YoungHobbit Oct 13 '15 at 14:26
  • 1
    seems the limitation is of one single field. I will have to find another way to implement suggestions. – Nikhil Sahu Oct 13 '15 at 14:38
  • I am also facing the same issue. Please comment here if you find any solution. – YoungHobbit Oct 13 '15 at 14:40
  • @nikhil24 Have you seen my post for the solr multiple payload. – YoungHobbit Oct 13 '15 at 14:41
  • This: http://stackoverflow.com/questions/32434186/solr-suggestion-with-multiple-payloads ? yeah. – Nikhil Sahu Oct 13 '15 at 14:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92156/discussion-between-younghobbit-and-nikhil24). – YoungHobbit Oct 13 '15 at 14:48