提供检索服务,从数据库提取字段并建立索引,检索效率比直接查库高。
服务端安装配置
- 下载http://labs.renren.com/apache-mirror/lucene/solr/3.6.0/apache-solr-3.6.0.zip
- 复制并配置tomcat:给Connector添加属性URIEncoding="UTF-8";也可以配置自带的jetty。
- 建立上下文vi tomcat/conf/Catalina/localhost/solr.xml
<?xml version="1.0" encoding="UTF-8"?><Context docBase="/usr/local/tomcat-solr/webapps/solr.war" debug="0" crossContext="true" ><Environment name="solr/home" type="java.lang.String" value="/usr/local/tomcat-solr/solr" override="true" /></Context> - 配置:schema.xml,基本类型和分词Text,fields字段要和索引的bean一致。
<types><fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/><fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/><fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/><fieldType name="text" class="solr.TextField"><analyzer type="index"><tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" useSmart="false"/></analyzer><analyzer type="query"><tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" useSmart="false"/></analyzer></fieldType></types><fields><field name="bIDNo" type="string" indexed="true" stored="true" multiValued="false" required="true"/><field name="bNameCode" type="text" indexed="true" stored="true" multiValued="false" /></fields> - 启动java -jar start.jar,访问http://localhost:port/solr/admin
客户端使用
- 获得服务:
HttpSolrServer server = new HttpSolrServer(url);server.setSoTimeout(10000); // socket read timeoutserver.setConnectionTimeout(30000);server.setDefaultMaxConnectionsPerHost(100);server.setMaxTotalConnections(100);server.setFollowRedirects(false); // defaults to false// allowCompression defaults to false.// Server side must support gzip or deflate for this to have any effect.server.setAllowCompression(true); - 建立或删除索引:使用pinyin4j提取部分字段拼音
SolrServer server = getSolrServer();server.deleteByQuery("*:*");//删除旧索引
server.deleteById(ids);//删除指定索引server.addBeans(list);//添加搜索实体,字段用@Field注解server.optimize();//优化server.commit();//提交索引 - 执行查询
SolrServer server = getSolrServer();SolrQuery query = new SolrQuery();query.setQuery(key+":"+value);//设置查询条件,*:*表示所有。query.setStart(pager.getStartRow());//分页query.setRows(pager.getPageSize());//每页数量query.addFilterQuery("bOnlineState:1 OR bOnlineState:3");//过滤query.addSortField("bIDNo", ORDER.asc);//排序
query.setFields("bIDNo", "bNameCode");//返回部分字段QueryResponse rsp = server.query(query);if(pager.getTotalPages() == 0) {long numFound = rsp.getResults().getNumFound();//结果总数,页面展示需要知道pager.setTotalRows((int)numFound);pager.setTotalPages(pager.getTotalRows() / pager.getPageSize() + (pager.getTotalRows() % pager.getPageSize() > 0 ? 1 : 0));}List<BookSearch> list = rsp.getBeans(BookSearch.class);//提取结果实体pager.setElements(list); - 高亮显示
query.addHighlightField(key);query.setHighlight(true); // 开启高亮组件query.setHighlightSimplePre("<font color='red'>");//标记,高亮关键字前缀query.setHighlightSimplePost("</font>");//后缀Map<String, Map<String, List<String>>> highlighting = rsp.getHighlighting(); - 搜索提示:配合jQuery.autocomplete使用
query.setFacet(true);// 分组查询query.addFacetField("bNameCode");// 增加分组字段query.addFacetField("nameCodePinYin");//支持拼音提示
query.setFacetPrefix(input);//对前缀input提示query.setFacetMinCount(1);//返回分数大于0(检索有结果)的int suggestNumber = 5;query.setFacetLimit(suggestNumber + 1);//限制5个返回结果,去掉首字所以多找一个
FacetField facetField = rsp.getFacetField(type);List<Count> countList = facetField != null ? facetField.getValues() : null;
suggestions[i] = StringUtils.trim(count.getName());hits[i] = (int)count.getCount();