2012-11-01 54 views
2

我是Solr的新手,并希望基于两个字段标题和说明实现自动完成功能。另外,结果集应该被其他字段进一步限制,如id和category。样本数据:来自多个字段的solr自动完成关键字

Title: The brown fox lives in the woods 
Description: The fox is found in the woods where brown leaves cover the ground. The animal's fur is brown in color and has a long tail. 

期望中的自动完成结果:

brown fox 
brown leaves 
brown color 

下面是schema.xml中的相关条目:

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


<field name="id" type="int" indexed="true" stored="true"/> 
<field name="category" type="string" indexed="true" stored="true"/> 
<field name="title" type="text_general" indexed="true" stored="true"/> 
<field name="description" type="text_general" indexed="true" stored="true"/> 

<field name="ac-terms" type="autocomplete" indexed="true" stored="false" multiValued="true" omitNorms="true" omitTermFreqAndPositions="false" /> 
<copyField source="title" dest="ac-terms"/> 
<copyField source="description" dest="ac-terms"/> 

查询请求:

http://localhost:9090/solr/select?q=(ac-terms:brown) 

回答

4

使用ShingleFilterFactory具有以下配置解决:

<fieldType name="autocomplete" class="solr.TextField" positionIncrementGap="100"> 
    <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> 

<field name="ac-terms" type="autocomplete" indexed="true" stored="false" multiValued="true" omitNorms="true" omitTermFreqAndPositions="false" /> 
<copyField source="title" dest="ac-terms"/> 
<copyField source="description" dest="ac-terms"/> 

查询请求:

http://localhost:9090/solr/select?q=&facet=true&facet.field=ac-terms&facet.prefix=brown 

结果:

brown color 
brown fox 
brown leaves 

希望这可以帮助别人

+0

感谢这样一个很好的例子。有没有办法让这些结果具有权重来订购它们? – lennard

0

什么使领域spellcheck_text并使用复制字段功能,以便titledescription自动注定为spellcheck_text

...指示Solr的,你想让它在复制该文件的“目标” 场它认为在被添加到索引文件 “源”字段中的任何数据。 ... 原始文本在启动或目标字段的任何已配置的分析仪 被调用之前,从 “源”字段发送到“dest”字段。

http://wiki.apache.org/solr/SchemaXml#Copy_Fields

+0

我没有通过声明ac-terms来做到这一点吗?我错过了什么? – Boyan