You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solr实现商品搜索联想建议的schema与solrconfig配置示例咨询

Solr 指定搜索建议功能配置方案

1. 需求说明

现有产品名称如下:

  • Bella cycling gloves for women white and graphite size L
  • Daisy cycling gloves for children white and blue size XS
  • Bars cycling gloves for men red size XL

需要实现的搜索建议效果:用户输入cycling gloves时,返回以下6条建议:

  • cycling gloves for children
  • cycling gloves for men
  • cycling gloves for women
  • bella cycling gloves
  • daisy cycling gloves
  • bars cycling gloves

2. schema(或managed-schema)配置

首先配置用于生成建议的字段类型、目标字段,以及字段复制规则:

<!-- 配置专门用于生成短语建议的字段类型 -->
<fieldType name="text_suggest" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <!-- 生成2-3个相邻词的短语,刚好覆盖需要的建议组合 -->
    <filter class="solr.ShingleFilterFactory"
            minShingleSize="2"
            maxShingleSize="3"
            outputUnigrams="false"
            outputUnigramsIfNoShingles="false"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

<!-- 原有产品名称存储字段,可保留你原本的配置 -->
<field name="product_name" type="text_general" indexed="true" stored="true"/>

<!-- 搜索建议专用字段,自动同步产品名称内容 -->
<field name="suggest_field" type="text_suggest" indexed="true" stored="true"/>
<copyField source="product_name" dest="suggest_field"/>

3. solrconfig.xml 配置

配置建议组件和对应请求处理器:

<!-- 搜索建议组件配置 -->
<searchComponent name="suggest" class="solr.SuggestComponent">
  <lst name="suggester">
    <str name="name">productSuggester</str>
    <str name="lookupImpl">AnalyzingInfixLookupFactory</str>
    <str name="dictionaryImpl">DocumentDictionaryFactory</str>
    <str name="field">suggest_field</str>
    <str name="suggestAnalyzerFieldType">text_suggest</str>
    <!-- 提交数据时自动构建建议索引,也可手动触发 -->
    <str name="buildOnCommit">true</str>
    <str name="buildOnStartup">false</str>
    <str name="highlight">false</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>
    <str name="suggest.dictionary">productSuggester</str>
  </lst>
  <arr name="components">
    <str>suggest</str>
  </arr>
</requestHandler>

4. 生效步骤

  1. 保存配置后重启Solr实例
  2. 重新全量导入产品数据,触发建议索引构建
  3. 调用测试:请求http://你的Solr服务地址/核心名/suggest?suggest.q=cycling gloves即可返回要求的6条建议,调整suggest.count参数可修改返回建议数量

内容的提问来源于stack exchange,提问作者IrcDirk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 18:48:02