How to autocomplete across multiple fields in Solr

2019-07-16 13:42发布

问题:

I am trying to implement auto-complete feature for search using Solr's suggester component. I want to give suggestions across multiple fields. I have 2 fields taxonomy and tag which I want to provide in suggestions. Eg if the search query is neck then it should return:

necklace
neckalce sets
pearl necklace
diamond necklace
pearl necklace sets
diamond necklace sets

where necklace is a taxonomy and pearl and diamond are tags.

Following is my schema.xml:

<field name="suggestion" type="text_auto" indexed="true" stored="false" multiValued="false" />

<copyField source="taxonomy_name" dest="suggestion"/>
<copyField source="tag" dest="suggestion">

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

and my solrconfig.xml:

<searchComponent class="solr.SpellCheckComponent" name="suggest">
<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">suggestion</str>  <!-- the indexed field to derive suggestions from -->
  <float name="threshold">0.005</float>
  <str name="buildOnCommit">true</str>
</lst>
</searchComponent>


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

But this returns:

necklace
necklace sets

How can I fix this. I also tried using :

<fieldType name="text_auto" class="solr.TextField"> 
  <analyzer> 
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt"/>
    <filter class="solr.ShingleFilterFactory" maxShingleSize="2" outputUnigrams="false"/>
    <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
  </analyzer> 
</fieldType> 

But this returns only necklace sets.

回答1:

Autocomplete in SolR only works if the query is the first word.

If the query is "neck" the autocomplete should return terms which begins with "neck". It cannot return "pearl necklace" because "pearl" is before your query.

Check this link for a possible solution : http://www.cominvent.com/2012/01/25/super-flexible-autocomplete-with-solr/



回答2:

Add this to the <searchHandler>

<str name="lookupImpl">AnalyzingInfixLookupFactory</str>

This will enable you to search pearl necklace as well, as it recognizes the text in the middle of a word/phrase/field.



回答3:

Did you try ?

<fieldType name="text_auto" class="solr.TextField"
positionIncrementGap="100">
<analyzer>
<tokenizer class="solr.UAX29URLEmailTokenizerFactory"/>
<filter class="solr.StopFilterFactory" ignoreCase="true"
words="stopwords.txt" enablePositionIncrements="true" />
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.ASCIIFoldingFilterFactory"/>
<filter class="solr.EnglishPossessiveFilterFactory"/>
</analyzer>
</fieldType>