php solr 智能提示,solr搜索智能提示Suggest

solr1.4以后实现的智能提示,方便用户输入搜索

Suggest配置

string

suggest

org.apache.solr.spelling.suggest.Suggester

org.apache.solr.spelling.suggest.tst.TSTLookup

name

0.0001

freq

true

true

true

suggest

8

true

false

true

suggest

说明:

1.solr的suggest基于solr.SpellCheckComponent

2.queryAnalyzerFieldType 参数为string,在这不要定义复杂分词,如果是根据某一个索引字段,意义不大

3.field字段名,表示基于schema中的某一个索引字段

4.threshold限制一些不常用的词出现,值越大过滤纸越多

5.sourceLocation用于设置字典,如果有一个字典能记录用户常搜索的字,那提示更更好

6.spellcheckIndexDir如果已经设置spellcheck,那么可以在此制定目录

7.字典格式如下

# This is a sample dictionary file.

acquire

accidentally\t2.0

accommodate\t3.0

文本格式utf-8,#开头表示注释,被忽略

每一个词一行,后面带权重

8.配置词典后在requestHandler中设置spellcheck.onlyMorePopular为true,可以根据权重排序

9.spellcheck.count返回行

配置完成重启服务后,设置参数suggest/?spellcheck.build=true来创建spellchecker的索引

然后输入:http://ip:port/corename/suggest?q=xxx进行搜索了

接下来就是前台js实现的问题了。

当然也可以通过solrj来进行搜索

CommonsHttpSolrServer server = new CommonsHttpSolrServer(

"http://ip:port/corename/");

SolrQuery params = new SolrQuery();

String token = "牛";

params.set("qt", "/suggest");

params.set("q", token);

params.set("spellcheck.build", "true");

QueryResponse response = null;

try {

response = server.query(params);

System.out.println("查询耗时:" + response.getQTime());

} catch (SolrServerException e) {

System.err.println(e.getMessage());

e.printStackTrace();

} catch (Exception e) {

System.err.println(e.getMessage());

e.printStackTrace();

} finally {

}

SpellCheckResponse spellCheckResponse = response

.getSpellCheckResponse();

if (spellCheckResponse != null) {

List suggestionList = spellCheckResponse

.getSuggestions();

for (Suggestion suggestion : suggestionList) {

System.out.println("Suggestions NumFound: "

+ suggestion.getNumFound());

System.out.println("Token: " + suggestion.getToken());

System.out.print("Suggested: ");

List suggestedWordList = suggestion.getAlternatives();

for (String word : suggestedWordList) {

System.out.println(word + ", ");

}

System.out.println();

}

}

这样就可以。

对于Suggest,个人的想法,字典是一种好的方式,但是增加字典比较麻烦,还有可以独立一个字段,对该字段做一些特殊的分词,比如像商品搜索类,整个标题可以是一个词,中文,数字,英文,符号等间隔的分开,这个具体还是看应用了。

现在我在诚交网易货商品搜索上用的是字段的方式,新建一个单独的排序字段,然后把需要提示的字段拷贝过来,效果还算可以,大家可以上诚交网产品搜索体验一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值