You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Jorge Sanchez <xs...@gmail.com> on 2014/01/30 14:18:07 UTC

SOLR suggester with highlighting

Hello,

I am trying to make a typehead autocomplete with SOLR using the suggester.

The search will be done for users and group names which aggregate users.
The search will be done on usernames , bio , web page and other stuff. What
I want to achieve is sort of "facebook" or "twitter" alike search. For this
I need to enrich the result from SOLR with additional data (user type, url
of the profile, his avatar url etc.).

The user and group would have the ID field in SOLR which would correspond
to the ID in the DB to get these information. I am stuck on how to do that.

Currently I have the suggester working but it only returns the suggesting
value, when I try to return some other attribute from the document it
doesn't work.

Here is the part of the solrconfig:

<searchComponent class="solr.SpellCheckComponent" name="suggest">
      <!-- configure the spellchecker used
         for autocomplete (dictionary) -->
      <lst name="spellchecker">
          <str name="name">suggester_dictionary</str>
          <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
          <str name="lookupImpl">org.apache.solr.spelling.suggest.fst.FSTLookupFactory</str>
          <!-- The indexed field to derive suggestions from -->
          <str name="field">autocomplete</str>
          <!-- buildOnCommit must be set to true because
            suggester keeps data in memory -->
          <str name="buildOnCommit">true</str>
      </lst>
  </searchComponent>


<requestHandler class="solr.SearchHandler" name="/suggest">
      <lst name="defaults">
          <!-- by default use the suggester_dictionary -->
          <str name="spellcheck.dictionary">suggester_dictionary</str>
          <str name="spellcheck.count">5</str>
          <str name="spellcheck.onlyMorePopular">false</str>
      </lst>
      <lst name="invariants">
          <!-- always run the Suggester for queries to this handler -->
          <str name="spellcheck">true</str>
          <!-- collate not needed, query if tokenized as keyword, we
need only suggestions for that term -->
          <str name="spellcheck.collate">false</str>
      </lst>
      <!-- this handler uses only the needed component :
        suggest defined above -->
      <arr name="components">
          <str>suggest</str>
          <str>highlight</str>
      </arr>
  </requestHandler>

and scheme:

<field name="groupid" type="int" indexed="true" stored="true"
           required="true" multiValued="false"/>
       <field name="groupusername" type="text_general" indexed="true"
           stored="true" multiValued="true"/>
       <field name="groupname" type="text_general" indexed="true"
           stored="true" multiValued="false"/>
       <field name="grouporuser" type="boolean" indexed="true"
           stored="true" multiValued="false"/>

        <field name="autocomplete" type="text_autocomplete"/>

    <copyField source="groupusername" dest="autocomplete"/>
    <copyField source="groupname" dest="autocomplete"/>

The query: http://gruppu.com:8983/solr/suggest?q=*:*
&spellcheck.q=jo&spellcheck=true&hl=on&hl.fl=groupid

The respond:

<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">0</int>
</lst>
<lst name="spellcheck">
<lst name="suggestions">
<lst name="jo">
<int name="numFound">2</int>
<int name="startOffset">0</int>
<int name="endOffset">2</int>
<arr name="suggestion">
<str>jorge</str>
<str>jorgen</str>
</arr>
</lst>
</lst>
</lst>
</response>

I would like to have the groupid and grouporuser fields returned ... No
luck so far.

Re: SOLR suggester with highlighting

Posted by Areek Zillur <ar...@gmail.com>.
This can be achieved using payloads in the suggester dictionary. The
suggester based on spellcheck component does not support payloads in
dictionary.
You can use the new suggester component (
https://issues.apache.org/jira/browse/SOLR-5378), which allows you to
highlight and return payloads.
The payload can only be one field from your search index (or you could also
use the file-based dictionaries).
Here is a solrconfig snippet that might be similar to what you want:

<searchComponent class="solr.SuggestComponent"
name="highlighting_suggester">
    <lst name="suggester">
      <str name="name">highlighting_suggester</str>
      <str
name="lookupImpl">org.apache.solr.spelling.suggest.fst.AnalyzingInfixLookupFactory</str>
<!-- suggester with highlighting support -->
      <str name="dictionaryImpl">DocumentDictionaryFactory</str> <!--
allows suggester to be built from the search index itself -->
      <str name="field">autocomplete</str> <!-- name of the field that the
suggestions will be based on -->
      <str name="weightField">relevance</str> <!-- (optional) weight used
for suggestions -->
      <str name="payloadField">group_info</str> <!-- payload field that
will be in the suggest results (this field can be something like
groupid:groupuser) -->
      <str name="suggestAnalyzerFieldType">text</str> <!-- field type
defined in schema that will be used to do index and query time processing
on the suggestions -->
      <!-- Suggester properties -->
      <!-- <str name="minPrefixChars">4</str>  -->
    </lst>
</searchComponent>

NOTE: currently the matched words are highlighted by  <b></b> tags

Hope that helps,
Areek