---
title: "solr搜索智能提示Suggest"
author: "lavafree"
date: "2012-09-27T15:41:01Z"
tags: []
categories: ["编程语言"]
slug: "solr-suggest"
draft: false
lastmod: "2012-12-21T17:08:29Z"
id: 1687415
url: "https://lavafree.iteye.com/blog/1687415"
---

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

 

Suggest配置

 

```
<searchComponent name="suggest" class="solr.SpellCheckComponent">
	<str name="queryAnalyzerFieldType">string</str>
	<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">name</str>
		<float name="threshold">0.0001</float>
		<!--<str name="sourceLocation">suggest_dict.txt</str>-->
		<!--<str name="spellcheckIndexDir">spellchecker</str>-->
        <str name="comparatorClass">freq</str>
        <str name="buildOnOptimize">true</str>
		<str name="buildOnCommit">true</str>     
	</lst>  
  </searchComponent>    
 
  <requestHandler name="/suggest" class="org.apache.solr.handler.component.SearchHandler">  
	<lst name="defaults">  
		<str name="spellcheck">true</str>  
		<str name="spellcheck.dictionary">suggest</str>  
		<str name="spellcheck.count">8</str>  
		<str name="spellcheck.onlyMorePopular">true</str>
		<str name="spellcheck.extendedResults">false</str>		
		<str name="spellcheck.collate">true</str>
		<!--<str name="spellcheck.build">true</str>	-->	
	</lst>  
	<arr name="components">  
		<str>suggest</str>  
	</arr>  
  </requestHandler>
```

 说明：

 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<Suggestion> 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<String> suggestedWordList = suggestion.getAlternatives();
				for (String word : suggestedWordList) {
					System.out.println(word + ", ");
				}
				System.out.println();
			}
		}
```

 这样就可以。

 

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

 

现在我在[诚交网易货商品搜索](http://www.chj360.com "诚交网易货商品搜索")上用的是字段的方式，新建一个单独的排序字段，然后把需要提示的字段拷贝过来，效果还算可以，大家看下图或去诚交网体验一下。

![](http://dl.iteye.com/upload/attachment/0078/0692/ceaa6ff2-53f4-3635-ae7b-ced4d1a7145b.png)