最近开发,使用了solr,发现这个东东真是方便。很早就听说了,但是一直觉得配置较难,而且需要学习封装后的组件本来就要费时间,所以知道将lucene弄个熟练后才开始学习solr。
最近写了这样的一段代码,
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
- SolrDocumentList sdl = response.getResults();
- len = (int) sdl.getNumFound();
- System.out.println(len);
- for(int i = 0; i < len; i++)
- {
- SolrDocument d = sdl.get(i);
- for(Iterator<Map.Entry<String, Object>> j = d.iterator(); j.hasNext(); )
- {
- hits.add((String)( j.next().getValue() ) );
- }
- }
结果出现: index out of boundary的错误。原来getNumFound获得的是整个的查询数量。
而sdl中仅仅包含在查询参数rows指定的文档个数。而rows默认是10,所以这里就出现越界错误了。
改成这样就没有问题了:
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
- SolrDocumentList sdl = response.getResults();
- len = (int) sdl.getNumFound();
- System.out.println(len);
- for(int i = 0; i < perPage; i++)
- {
- SolrDocument d = sdl.get(i);
- for(Iterator<Map.Entry<String, Object>> j = d.iterator(); j.hasNext(); )
- {
- hits.add((String)( j.next().getValue() ) );
- }
- }
新的项目基于solr开发,搜索更加灵活,更加方便了用户。