欢迎投稿

今日深度:

solr应用,

solr应用,



Solrj已经是很强大的solr客户端了。它本身就包装了httpCliet,以完全对象的方式对solr进行交互。很小很好很强大。
??? 不过在实际使用中,设置SolrQuery 的过程中,为了设置多个搜索条件和排序规则等等参数,我们往往会陷入并接字符串的地步,实在是很丑陋,不符合面向对象的思想。扩展性几乎为0,。基于这点,开发了一个小东西,我们只需要设置搜索对象,将对象扔给后台就可以了。
??? 比如,我们搭建的solr服务支持某10个字段的搜索,我们要搜索其中的一些,那么我们只需要传入要搜索的对象POJO,将要搜索的字段内容,set到POJO对象对应额字段即可。

??? 比如如下一个类:

?

[java]?view plaincopyprint?
  1. package?org.uppower.tnt.biz.core.manager.blog.dataobject;??
  2. ??
  3. /**?
  4. ?*?@author?yingmu?
  5. ?*?@version?2010-7-20?下午01:00:55?
  6. ?*/??
  7. public?class?SolrPropertyDO?{??
  8. ????private?String?auction_id;??
  9. ????private?String?opt_tag;??
  10. ????private?String?exp_tag;??
  11. ????private?String?title;??
  12. ????private?String?desc;??
  13. ????private?String?brand;??
  14. ????private?String?category;??
  15. ????private?String?price;??
  16. ????private?String?add_prov;??
  17. ????private?String?add_city;??
  18. ????private?String?quality;??
  19. ????private?String?flag;??
  20. ????private?String?sales;??
  21. ????private?String?sellerrate;??
  22. ????private?String?selleruid;??
  23. ????private?String?ipv15;??
  24. ??
  25. ????public?String?getAuction_id()?{??
  26. ????????return?auction_id;??
  27. ????}??
  28. ??
  29. ????public?void?setAuction_id(String?auctionId)?{??
  30. ????????auction_id?=?auctionId;??
  31. ????}??
  32. ??
  33. ????……??
  34. ??
  35. ????public?String?getExp_tag()?{??
  36. ????????return?exp_tag;??
  37. ????}??
  38. ??
  39. ????public?void?setExp_tag(String?expTag)?{??
  40. ????????exp_tag?=?expTag;??
  41. ????}??
  42. }??
Java代码??
  1. package?org.uppower.tnt.biz.core.manager.blog.dataobject;??
  2. ??
  3. /**?
  4. ?*?@author?yingmu?
  5. ?*?@version?2010-7-20?下午01:00:55?
  6. ?*/??
  7. public?class?SolrPropertyDO?{??
  8. ????private?String?auction_id;??
  9. ????private?String?opt_tag;??
  10. ????private?String?exp_tag;??
  11. ????private?String?title;??
  12. ????private?String?desc;??
  13. ????private?String?brand;??
  14. ????private?String?category;??
  15. ????private?String?price;??
  16. ????private?String?add_prov;??
  17. ????private?String?add_city;??
  18. ????private?String?quality;??
  19. ????private?String?flag;??
  20. ????private?String?sales;??
  21. ????private?String?sellerrate;??
  22. ????private?String?selleruid;??
  23. ????private?String?ipv15;??
  24. ??
  25. ????public?String?getAuction_id()?{??
  26. ????????return?auction_id;??
  27. ????}??
  28. ??
  29. ????public?void?setAuction_id(String?auctionId)?{??
  30. ????????auction_id?=?auctionId;??
  31. ????}??
  32. ??
  33. ????……??
  34. ??
  35. ????public?String?getExp_tag()?{??
  36. ????????return?exp_tag;??
  37. ????}??
  38. ??
  39. ????public?void?setExp_tag(String?expTag)?{??
  40. ????????exp_tag?=?expTag;??
  41. ????}??
  42. }??

那么我们在定义搜索对象时候,就按照如下设置: [java]?view plaincopyprint?
  1. SolrPropertyDO?propertyDO?=?new?SolrPropertyDO();??
  2. ????????propertyDO.setAdd_city("(杭州AND成都)OR北京");??
  3. ????????propertyDO.setTitle("丝绸OR剪刀");??
  4. ?????????……??
Java代码??
  1. SolrPropertyDO?propertyDO?=?new?SolrPropertyDO();??
  2. ????????propertyDO.setAdd_city("(杭州AND成都)OR北京");??
  3. ????????propertyDO.setTitle("丝绸OR剪刀");??
  4. ?????????……??

设置排序条件,也是类似的做法:

?

[java]?view plaincopyprint?
  1. SolrPropertyDO?compositorDO?=?new?SolrPropertyDO();??
  2. ????????compositorDO.setPrice?("desc");??
  3. ????????compositorDO.setQuality?("asc");??
  4. ?????????……??
Java代码??
  1. SolrPropertyDO?compositorDO?=?new?SolrPropertyDO();??
  2. ????????compositorDO.setPrice?("desc");??
  3. ????????compositorDO.setQuality?("asc");??
  4. ?????????……??

?将定义好的两个对象扔给后面的接口就可以了。

?? ? 接口函数querySolrResult传入四个参数,其中包含搜索字段对象,排序条件对象。为了提供类似limit的操作,用于分页查询,提供了startIndex和pageSize。
???? 函数querySolrResultCount是单纯为了获得搜索条数,配合分页使用。
??? 以下是定义的接口:

?

[java]?view plaincopyprint?
  1. package?org.uppower.tnt.biz.core.manager.blog;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?org.uppower.tnt.biz.core.manager.isearch.dataobject.SolrPropertyDO;??
  6. ??
  7. /**?
  8. ?*?@author?yingmu?
  9. ?*?@version?2010-7-20?下午03:51:15?
  10. ?*/??
  11. public?interface?SolrjOperator?{??
  12. ??
  13. ????/**?
  14. ?????*?获得搜索结果?
  15. ?????*??
  16. ?????*?@param?propertyDO?
  17. ?????*?@param?compositorDO?
  18. ?????*?@param?startIndex?
  19. ?????*?@param?pageSize?
  20. ?????*?@return?
  21. ?????*?@throws?Exception?
  22. ?????*/??
  23. ????public?List?querySolrResult(Object?propertyDO,??
  24. ????????????Object?compositorDO,?Long?startIndex,?Long?pageSize)??
  25. ????????????throws?Exception;??
  26. ??
  27. ????/**?
  28. ?????*?获得搜索结果条数?
  29. ?????*??
  30. ?????*?@param?propertyDO?
  31. ?????*?@param?compositorDO?
  32. ?????*?@return?
  33. ?????*?@throws?Exception?
  34. ?????*/??
  35. ????public?Long?querySolrResultCount(SolrPropertyDO?propertyDO,??
  36. ????????????Object?compositorDO)?throws?Exception;??
  37. ??
  38. }??
Java代码??
  1. package?org.uppower.tnt.biz.core.manager.blog;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?org.uppower.tnt.biz.core.manager.isearch.dataobject.SolrPropertyDO;??
  6. ??
  7. /**?
  8. ?*?@author?yingmu?
  9. ?*?@version?2010-7-20?下午03:51:15?
  10. ?*/??
  11. public?interface?SolrjOperator?{??
  12. ??
  13. ????/**?
  14. ?????*?获得搜索结果?
  15. ?????*??
  16. ?????*?@param?propertyDO?
  17. ?????*?@param?compositorDO?
  18. ?????*?@param?startIndex?
  19. ?????*?@param?pageSize?
  20. ?????*?@return?
  21. ?????*?@throws?Exception?
  22. ?????*/??
  23. ????public?List?querySolrResult(Object?propertyDO,??
  24. ????????????Object?compositorDO,?Long?startIndex,?Long?pageSize)??
  25. ????????????throws?Exception;??
  26. ??
  27. ????/**?
  28. ?????*?获得搜索结果条数?
  29. ?????*??
  30. ?????*?@param?propertyDO?
  31. ?????*?@param?compositorDO?
  32. ?????*?@return?
  33. ?????*?@throws?Exception?
  34. ?????*/??
  35. ????public?Long?querySolrResultCount(SolrPropertyDO?propertyDO,??
  36. ????????????Object?compositorDO)?throws?Exception;??
  37. ??
  38. }??

?实现逻辑为,首先将传入的两个实体对象,解析为结构的Map当中,将解析完成的Map放入solrj实际的搜索对象当中。返回的对象为solrj的API提供的SolrDocument,其中结果数量为直接返回SolrDocumentList对象的getNumFound()
??? 具体实现类:

?

[java]?view plaincopyprint?
  1. package?org.uppower.tnt.biz.core.manager.blog;??
  2. ??
  3. import?java.util.ArrayList;??
  4. import?java.util.HashMap;??
  5. import?java.util.List;??
  6. import?java.util.Map;??
  7. import?java.util.TreeMap;??
  8. ??
  9. import?org.apache.solr.common.SolrDocumentList;??
  10. ??
  11. import?org.uppower.tnt.biz.core.manager.isearch.common.SolrjCommonUtil;??
  12. import?org.uppower.tnt.biz.core.manager.isearch.dataobject.SolrPropertyDO;??
  13. import?org.uppower.tnt.biz.core.manager.isearch.solrj.SolrjQuery;??
  14. ??
  15. /**?
  16. ?*?@author?yingmu?
  17. ?*?@version?2010-7-20?下午03:51:15?
  18. ?*/??
  19. public?class?DefaultSolrOperator?implements?SolrjOperator?{??
  20. ??????
  21. ????private?Logger?logger?=?LoggerFactory.getLogger(this.getClass());??
  22. ????private?SolrjQuery?solrjQuery;??
  23. ??
  24. ????public?void?setSolrjQuery(SolrjQuery?solrjQuery)?{??
  25. ????????this.solrjQuery?=?solrjQuery;??
  26. ????}??
  27. ??
  28. ????@Override??
  29. ????public?List?querySolrResult(Object?propertyDO,??
  30. ????????????Object?compositorDO,?Long?startIndex,?Long?pageSize)??
  31. ????????????throws?Exception?{??
  32. ????????Map?propertyMap?=?new?TreeMap();??
  33. ????????//排序有顺序,使用TreeMap???
  34. ????????Map?compositorMap?=?new?TreeMap();??
  35. ????????try?{??
  36. ????????????propertyMap?=?SolrjCommonUtil.getSearchProperty(propertyDO);??
  37. ????????????compositorMap?=?SolrjCommonUtil.getSearchProperty(compositorDO);??
  38. ????????}?catch?(Exception?e)?{??
  39. ????????????logger.error("SolrjCommonUtil.getSearchProperty()?is?error?!"+?e);??
  40. ????????}??
  41. ????????SolrDocumentList?solrDocumentList?=?solrjQuery.query(propertyMap,?compositorMap,??
  42. ????????????????startIndex,?pageSize);??
  43. ????????List?resultList?=?new?ArrayList();??
  44. ????????for?(int?i?=?0;?i?
  45. ????????????resultList.add(solrDocumentList.get(i));??
  46. ????????}??
  47. ????????return?resultList;??
  48. ????}??
  49. ??
  50. ????@Override??
  51. ????public?Long?querySolrResultCount(SolrPropertyDO?propertyDO,??
  52. ????????????Object?compositorDO)?throws?Exception?{??
  53. ????????Map?propertyMap?=?new?TreeMap();??
  54. ????????Map?compositorMap?=?new?TreeMap();??
  55. ????????try?{??
  56. ????????????propertyMap?=?SolrjCommonUtil.getSearchProperty(propertyDO);??
  57. ????????????compositorMap?=?SolrjCommonUtil.getSearchProperty(compositorDO);??
  58. ????????}?catch?(Exception?e)?{??
  59. ????????????logger.error("SolrjCommonUtil.getSearchProperty()?is?error?!"?+?e);??
  60. ????????}??
  61. ????????SolrDocumentList?solrDocument?=?solrjQuery.query(propertyMap,?compositorMap,??
  62. ????????????????null,?null);??
  63. ????????return?solrDocument.getNumFound();??
  64. ????}??
  65. ??
  66. }??
Java代码??
  1. package?org.uppower.tnt.biz.core.manager.blog;??
  2. ??
  3. import?java.util.ArrayList;??
  4. import?java.util.HashMap;??
  5. import?java.util.List;??
  6. import?java.util.Map;??
  7. import?java.util.TreeMap;??
  8. ??
  9. import?org.apache.solr.common.SolrDocumentList;??
  10. ??
  11. import?org.uppower.tnt.biz.core.manager.isearch.common.SolrjCommonUtil;??
  12. import?org.uppower.tnt.biz.core.manager.isearch.dataobject.SolrPropertyDO;??
  13. import?org.uppower.tnt.biz.core.manager.isearch.solrj.SolrjQuery;??
  14. ??
  15. /**?
  16. ?*?@author?yingmu?
  17. ?*?@version?2010-7-20?下午03:51:15?
  18. ?*/??
  19. public?class?DefaultSolrOperator?implements?SolrjOperator?{??
  20. ??????
  21. ????private?Logger?logger?=?LoggerFactory.getLogger(this.getClass());??
  22. ????private?SolrjQuery?solrjQuery;??
  23. ??
  24. ????public?void?setSolrjQuery(SolrjQuery?solrjQuery)?{??
  25. ????????this.solrjQuery?=?solrjQuery;??
  26. ????}??
  27. ??
  28. ????@Override??
  29. ????public?List?querySolrResult(Object?propertyDO,??
  30. ????????????Object?compositorDO,?Long?startIndex,?Long?pageSize)??
  31. ????????????throws?Exception?{??
  32. ????????Map?propertyMap?=?new?TreeMap();??
  33. ????????//排序有顺序,使用TreeMap??
  34. ????????Map?compositorMap?=?new?TreeMap();??
  35. ????????try?{??
  36. ????????????propertyMap?=?SolrjCommonUtil.getSearchProperty(propertyDO);??
  37. ????????????compositorMap?=?SolrjCommonUtil.getSearchProperty(compositorDO);??
  38. ????????}?catch?(Exception?e)?{??
  39. ????????????logger.error("SolrjCommonUtil.getSearchProperty()?is?error?!"+?e);??
  40. ????????}??
  41. ????????SolrDocumentList?solrDocumentList?=?solrjQuery.query(propertyMap,?compositorMap,??
  42. ????????????????startIndex,?pageSize);??
  43. ????????List?resultList?=?new?ArrayList();??
  44. ????????for?(int?i?=?0;?i?
  45. ????????????resultList.add(solrDocumentList.get(i));??
  46. ????????}??
  47. ????????return?resultList;??
  48. ????}??
  49. ??
  50. ????@Override??
  51. ????public?Long?querySolrResultCount(SolrPropertyDO?propertyDO,??
  52. ????????????Object?compositorDO)?throws?Exception?{??
  53. ????????Map?propertyMap?=?new?TreeMap();??
  54. ????????Map?compositorMap?=?new?TreeMap();??
  55. ????????try?{??
  56. ????????????propertyMap?=?SolrjCommonUtil.getSearchProperty(propertyDO);??
  57. ????????????compositorMap?=?SolrjCommonUtil.getSearchProperty(compositorDO);??
  58. ????????}?catch?(Exception?e)?{??
  59. ????????????logger.error("SolrjCommonUtil.getSearchProperty()?is?error?!"?+?e);??
  60. ????????}??
  61. ????????SolrDocumentList?solrDocument?=?solrjQuery.query(propertyMap,?compositorMap,??
  62. ????????????????null,?null);??
  63. ????????return?solrDocument.getNumFound();??
  64. ????}??
  65. ??
  66. }??

其中,对象的解析式利用反射原理,将实体对象中不为空的值,以映射的方式,转化为一个Map,其中排序对象在转化的过程中,使用TreeMap,保证其顺序性。
??? 解析公共类实现如下:

?

[java]?view plaincopyprint?
  1. package?org.uppower.tnt.biz.core.manager.blog.common;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. import?java.lang.reflect.InvocationTargetException;??
  5. import?java.lang.reflect.Method;??
  6. import?java.util.HashMap;??
  7. import?java.util.Map;??
  8. ??
  9. /**?
  10. ?*?@author?yingmu?
  11. ?*?@version?2010-7-20?下午01:07:15?
  12. ?*/??
  13. public?class?SolrjCommonUtil?{??
  14. ??
  15. ????public?static?Map?getSearchProperty(Object?model)??
  16. ????????????throws?NoSuchMethodException,?IllegalAccessException,??
  17. ????????????IllegalArgumentException,?InvocationTargetException?{??
  18. ????????Map?resultMap?=?new?TreeMap();??
  19. ????????//?获取实体类的所有属性,返回Field数组???
  20. ????????Field[]?field?=?model.getClass().getDeclaredFields();??
  21. ????????for?(int?i?=?0;?i?//?遍历所有属性???
  22. ????????????String?name?=?field[i].getName();?//?获取属性的名字???
  23. ????????????//?获取属性的类型???
  24. ????????????String?type?=?field[i].getGenericType().toString();??
  25. ????????????if?(type.equals("class?java.lang.String"))?{?//?如果type是类类型,则前面包含"class?",后面跟类名???
  26. ????????????????Method?m?=?model.getClass().getMethod(??
  27. ????????????????????????"get"?+?UpperCaseField(name));??
  28. ????????????????String?value?=?(String)?m.invoke(model);?//?调用getter方法获取属性值???
  29. ????????????????if?(value?!=?null)?{??
  30. ????????????????????resultMap.put(name,?value);??
  31. ????????????????}??
  32. ????????????}??
  33. ????????}??
  34. ????????return?resultMap;??
  35. ????}??
  36. ??
  37. ????//?转化字段首字母为大写???
  38. ????private?static?String?UpperCaseField(String?fieldName)?{??
  39. ????????fieldName?=?fieldName.replaceFirst(fieldName.substring(0,?1),?fieldName??
  40. ????????????????.substring(0,?1).toUpperCase());??
  41. ????????return?fieldName;??
  42. ????}??
  43. ??
  44. }??
Java代码??
  1. package?org.uppower.tnt.biz.core.manager.blog.common;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. import?java.lang.reflect.InvocationTargetException;??
  5. import?java.lang.reflect.Method;??
  6. import?java.util.HashMap;??
  7. import?java.util.Map;??
  8. ??
  9. /**?
  10. ?*?@author?yingmu?
  11. ?*?@version?2010-7-20?下午01:07:15?
  12. ?*/??
  13. public?class?SolrjCommonUtil?{??
  14. ??
  15. ????public?static?Map?getSearchProperty(Object?model)??
  16. ????????????throws?NoSuchMethodException,?IllegalAccessException,??
  17. ????????????IllegalArgumentException,?InvocationTargetException?{??
  18. ????????Map?resultMap?=?new?TreeMap();??
  19. ????????//?获取实体类的所有属性,返回Field数组??
  20. ????????Field[]?field?=?model.getClass().getDeclaredFields();??
  21. ????????for?(int?i?=?0;?i?//?遍历所有属性??
  22. ????????????String?name?=?field[i].getName();?//?获取属性的名字??
  23. ????????????//?获取属性的类型??
  24. ????????????String?type?=?field[i].getGenericType().toString();??
  25. ????????????if?(type.equals("class?java.lang.String"))?{?//?如果type是类类型,则前面包含"class?",后面跟类名??
  26. ????????????????Method?m?=?model.getClass().getMethod(??
  27. ????????????????????????"get"?+?UpperCaseField(name));??
  28. ????????????????String?value?=?(String)?m.invoke(model);?//?调用getter方法获取属性值??
  29. ????????????????if?(value?!=?null)?{??
  30. ????????????????????resultMap.put(name,?value);??
  31. ????????????????}??
  32. ????????????}??
  33. ????????}??
  34. ????????return?resultMap;??
  35. ????}??
  36. ??
  37. ????//?转化字段首字母为大写??
  38. ????private?static?String?UpperCaseField(String?fieldName)?{??
  39. ????????fieldName?=?fieldName.replaceFirst(fieldName.substring(0,?1),?fieldName??
  40. ????????????????.substring(0,?1).toUpperCase());??
  41. ????????return?fieldName;??
  42. ????}??
  43. ??
  44. }??

搜索直接调用solr客户端solrj,基本逻辑为循环两个解析之后的TreeMap,设置到SolrQuery当中,最后直接调用solrj的API,获得搜索结果。最终将搜索结构以List的形式返回。
??? 具体实现:

?

[java]?view plaincopyprint?
  1. package?org.uppower.tnt.biz.core.manager.blog.solrj;??
  2. ??
  3. import?java.net.MalformedURLException;??
  4. import?java.util.Map;??
  5. ??
  6. import?org.apache.solr.client.solrj.SolrQuery;??
  7. import?org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;??
  8. import?org.apache.solr.client.solrj.response.QueryResponse;??
  9. import?org.apache.solr.common.SolrDocumentList;??
  10. ??
  11. /**?
  12. ?*?@author?yingmu?
  13. ?*?@version?2010-7-20?下午02:57:04?
  14. ?*/??
  15. public?class?SolrjQuery?{??
  16. ????private?String?url;??
  17. ????private?Integer?soTimeOut;??
  18. ????private?Integer?connectionTimeOut;??
  19. ????private?Integer?maxConnectionsPerHost;??
  20. ????private?Integer?maxTotalConnections;??
  21. ????private?Integer?maxRetries;??
  22. ????private?CommonsHttpSolrServer?solrServer?=?null;??
  23. ????private?final?static?String?ASC?=?"asc";??
  24. ??
  25. ????public?void?init()?throws?MalformedURLException?{??
  26. ????????solrServer?=?new?CommonsHttpSolrServer(url);??
  27. ????????solrServer.setSoTimeout(soTimeOut);??
  28. ????????solrServer.setConnectionTimeout(connectionTimeOut);??
  29. ????????solrServer.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);??
  30. ????????solrServer.setMaxTotalConnections(maxTotalConnections);??
  31. ????????solrServer.setFollowRedirects(false);??
  32. ????????solrServer.setAllowCompression(true);??
  33. ????????solrServer.setMaxRetries(maxRetries);??
  34. ????}??
  35. ??
  36. ????public?SolrDocumentList?query(Map?propertyMap,??
  37. ????????????Map?compositorMap,?Long?startIndex,?Long?pageSize)??
  38. ????????????throws?Exception?{??
  39. ????????SolrQuery?query?=?new?SolrQuery();??
  40. ????????//?设置搜索字段???
  41. ????????if?(null?==?propertyMap)?{??
  42. ????????????throw?new?Exception("搜索字段不可为空!");??
  43. ????????}?else?{??
  44. ????????????for?(Object?o?:?propertyMap.keySet())?{??
  45. ????????????????StringBuffer?sb?=?new?StringBuffer();??
  46. ????????????????sb.append(o.toString()).append(":");??
  47. ????????????????sb.append(propertyMap.get(o));??
  48. ????????????????String?queryString?=?addBlank2Expression(sb.toString());??
  49. ????????????????query.setQuery(queryString);??
  50. ????????????}??
  51. ????????}??
  52. ????????//?设置排序条件???
  53. ????????if?(null?!=?compositorMap)?{??
  54. ????????????for?(Object?co?:?compositorMap.keySet())?{??
  55. ????????????????if?(ASC?==?compositorMap.get(co)??
  56. ????????????????????????||?ASC.equals(compositorMap.get(co)))?{??
  57. ????????????????????query.addSortField(co.toString(),?SolrQuery.ORDER.asc);??
  58. ????????????????}?else?{??
  59. ????????????????????query.addSortField(co.toString(),?SolrQuery.ORDER.desc);??
  60. ????????????????}??
  61. ????????????}??
  62. ????????}??
  63. ??????????
  64. ????????if?(null?!=?startIndex)?{??
  65. ????????????query.setStart(Integer.parseInt(String.valueOf(startIndex)));??
  66. ????????}??
  67. ????????if?(null?!=?pageSize?&&?0L?!=?pageSize.longValue())?{??
  68. ????????????query.setRows(Integer.parseInt(String.valueOf(pageSize)));??
  69. ????????}??
  70. ????????try?{??
  71. ????????????QueryResponse?qrsp?=?solrServer.query(query);??
  72. ????????????SolrDocumentList?docs?=?qrsp.getResults();??
  73. ????????????return?docs;??
  74. ????????}?catch?(Exception?e)?{??
  75. ????????????throw?new?Exception(e);??
  76. ????????}??
  77. ????}??
  78. ??
  79. ????private?String?addBlank2Expression(String?oldExpression)?{??
  80. ????????String?lastExpression;??
  81. ????????lastExpression?=?oldExpression.replace("AND",?"?AND?").replace("NOT",??
  82. ????????????????"?NOT?").replace("OR",?"?OR?");??
  83. ????????return?lastExpression;??
  84. ????}??
  85. ??
  86. ????public?Integer?getMaxRetries()?{??
  87. ????????return?maxRetries;??
  88. ????}??
  89. ??
  90. ????……??
  91. ????????
  92. ????????public?void?setMaxTotalConnections(Integer?maxTotalConnections)?{??
  93. ????????this.maxTotalConnections?=?maxTotalConnections;??
  94. ????}??
  95. }??
Java代码??
  1. package?org.uppower.tnt.biz.core.manager.blog.solrj;??
  2. ??
  3. import?java.net.MalformedURLException;??
  4. import?java.util.Map;??
  5. ??
  6. import?org.apache.solr.client.solrj.SolrQuery;??
  7. import?org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;??
  8. import?org.apache.solr.client.solrj.response.QueryResponse;??
  9. import?org.apache.solr.common.SolrDocumentList;??
  10. ??
  11. /**?
  12. ?*?@author?yingmu?
  13. ?*?@version?2010-7-20?下午02:57:04?
  14. ?*/??
  15. public?class?SolrjQuery?{??
  16. ????private?String?url;??
  17. ????private?Integer?soTimeOut;??
  18. ????private?Integer?connectionTimeOut;??
  19. ????private?Integer?maxConnectionsPerHost;??
  20. ????private?Integer?maxTotalConnections;??
  21. ????private?Integer?maxRetries;??
  22. ????private?CommonsHttpSolrServer?solrServer?=?null;??
  23. ????private?final?static?String?ASC?=?"asc";??
  24. ??
  25. ????public?void?init()?throws?MalformedURLException?{??
  26. ????????solrServer?=?new?CommonsHttpSolrServer(url);??
  27. ????????solrServer.setSoTimeout(soTimeOut);??
  28. ????????solrServer.setConnectionTimeout(connectionTimeOut);??
  29. ????????solrServer.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);??
  30. ????????solrServer.setMaxTotalConnections(maxTotalConnections);??
  31. ????????solrServer.setFollowRedirects(false);??
  32. ????????solrServer.setAllowCompression(true);??
  33. ????????solrServer.setMaxRetries(maxRetries);??
  34. ????}??
  35. ??
  36. ????public?SolrDocumentList?query(Map?propertyMap,??
  37. ????????????Map?compositorMap,?Long?startIndex,?Long?pageSize)??
  38. ????????????throws?Exception?{??
  39. ????????SolrQuery?query?=?new?SolrQuery();??
  40. ????????//?设置搜索字段??
  41. ????????if?(null?==?propertyMap)?{??
  42. ????????????throw?new?Exception("搜索字段不可为空!");??
  43. ????????}?else?{??
  44. ????????????for?(Object?o?:?propertyMap.keySet())?{??
  45. ????????????????StringBuffer?sb?=?new?StringBuffer();??
  46. ????????????????sb.append(o.toString()).append(":");??
  47. ????????????????sb.append(propertyMap.get(o));??
  48. ????????????????String?queryString?=?addBlank2Expression(sb.toString());??
  49. ????????????????query.setQuery(queryString);??
  50. ????????????}??
  51. ????????}??
  52. ????????//?设置排序条件??
  53. ????????if?(null?!=?compositorMap)?{??
  54. ????????????for?(Object?co?:?compositorMap.keySet())?{??
  55. ????????????????if?(ASC?==?compositorMap.get(co)??
  56. ????????????????????????||?ASC.equals(compositorMap.get(co)))?{??
  57. ????????????????????query.addSortField(co.toString(),?SolrQuery.ORDER.asc);??
  58. ????????????????}?else?{??
  59. ????????????????????query.addSortField(co.toString(),?SolrQuery.ORDER.desc);??
  60. ????????????????}??
  61. ????????????}??
  62. ????????}??
  63. ??????????
  64. ????????if?(null?!=?startIndex)?{??
  65. ????????????query.setStart(Integer.parseInt(String.valueOf(startIndex)));??
  66. ????????}??
  67. ????????if?(null?!=?pageSize?&&?0L?!=?pageSize.longValue())?{??
  68. ????????????query.setRows(Integer.parseInt(String.valueOf(pageSize)));??
  69. ????????}??
  70. ????????try?{??
  71. ????????????QueryResponse?qrsp?=?solrServer.query(query);??
  72. ????????????SolrDocumentList?docs?=?qrsp.getResults();??
  73. ????????????return?docs;??
  74. ????????}?catch?(Exception?e)?{??
  75. ????????????throw?new?Exception(e);??
  76. ????????}??
  77. ????}??
  78. ??
  79. ????private?String?addBlank2Expression(String?oldExpression)?{??
  80. ????????String?lastExpression;??
  81. ????????lastExpression?=?oldExpression.replace("AND",?"?AND?").replace("NOT",??
  82. ????????????????"?NOT?").replace("OR",?"?OR?");??
  83. ????????return?lastExpression;??
  84. ????}??
  85. ??
  86. ????public?Integer?getMaxRetries()?{??
  87. ????????return?maxRetries;??
  88. ????}??
  89. ??
  90. ????……??
  91. ????????
  92. ????????public?void?setMaxTotalConnections(Integer?maxTotalConnections)?{??
  93. ????????this.maxTotalConnections?=?maxTotalConnections;??
  94. ????}??
  95. }??

整个实现是在Spring的基础上完成的,其中SolrjQuery的init()方法在Spring容器启动是初始化。Init()方法内的属性,也是直接注入的。上层与下层之间也完全用注入的方式解决。具体配置就不贴不出来了,大家都会。

整个代码很简陋,但是几乎支持了你想要搜索的条件设置,而且不会暴露任何与solr相关的内容给上层调用,使整个搜索几乎以sql语言的思想在设置条件。


http://www.iteye.com/topic/315330


?

[java]?view plaincopyprint?
  1. package?org.nstcrm.person.util;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. import?java.lang.reflect.Method;??
  5. import?java.net.MalformedURLException;??
  6. import?java.util.ArrayList;??
  7. import?java.util.List;??
  8. import?java.util.Map;??
  9. import?java.util.TreeMap;??
  10. ??
  11. import?org.apache.solr.client.solrj.SolrQuery;??
  12. import?org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;??
  13. import?org.apache.solr.client.solrj.response.QueryResponse;??
  14. import?org.apache.solr.common.SolrDocumentList;??
  15. ??
  16. public?class?SolrHttpServer?{??
  17. ????//private?Logger?logger?=?LoggerFactory.getLogger(this.getClass());???
  18. ????private?final?static?String?URL?=?"http://localhost:8080/solr";??
  19. ????private?final?static?Integer?SOCKE_TTIMEOUT?=?1000;?//?socket?read?timeout???
  20. ????private?final?static?Integer?CONN_TIMEOUT?=?100;??
  21. ????private?final?static?Integer?MAXCONN_DEFAULT?=?100;??
  22. ????private?final?static?Integer?MAXCONN_TOTAL?=?100;??
  23. ????private?final?static?Integer?MAXRETRIES?=?1;??
  24. ????private?static?CommonsHttpSolrServer?server?=?null;??
  25. ????private?final?static?String?ASC?=?"asc";??
  26. ??????
  27. ????public?void?init()?throws?MalformedURLException?{??
  28. ????????????server?=?new?CommonsHttpSolrServer(?URL?);??
  29. ????????????//server.setParser(new?XMLResponseParser());???
  30. ????????????server.setSoTimeout(SOCKE_TTIMEOUT);??
  31. ????????????server.setConnectionTimeout(CONN_TIMEOUT);??
  32. ????????????server.setDefaultMaxConnectionsPerHost(MAXCONN_DEFAULT);??
  33. ????????????server.setMaxTotalConnections(MAXCONN_TOTAL);??
  34. ????????????server.setFollowRedirects(false);??
  35. ????????????server.setAllowCompression(true);??
  36. ????????????server.setMaxRetries(MAXRETRIES);??
  37. ????}??
  38. ??????
  39. ????public?static?SolrDocumentList?query(Map?property,?Map?compositor,?Integer?pageSize)?throws?Exception?{??
  40. ????????SolrQuery?query?=?new?SolrQuery();??
  41. ????????//?设置搜索字段???
  42. ????????if(null?==?property)?{??
  43. ????????????throw?new?Exception("搜索字段不可为空!");??
  44. ????????}?else?{??
  45. ????????????for(Object?obj?:?property.keySet())?{??
  46. ????????????????StringBuffer?sb?=?new?StringBuffer();??
  47. ????????????????sb.append(obj.toString()).append(":");??
  48. ????????????????sb.append(property.get(obj));??
  49. ????????????????String?sql?=?(sb.toString()).replace("AND",?"?AND?").replace("OR",?"?OR?").replace("NOT",?"?NOT?");??
  50. ????????????????query.setQuery(sql);??
  51. ????????????}??
  52. ????????}??
  53. ????????//?设置结果排序???
  54. ????????if(null?!=?compositor)?{??
  55. ????????????for(Object?obj?:?compositor.keySet())?{??
  56. ????????????????if(ASC?==?compositor.get(obj)?||?ASC.equals(compositor.get(obj)))?{??
  57. ????????????????????query.addSortField(obj.toString(),?SolrQuery.ORDER.asc);??
  58. ????????????????}?else?{??
  59. ????????????????????query.addSortField(obj.toString(),?SolrQuery.ORDER.desc);??
  60. ????????????????}??
  61. ????????????}??
  62. ????????}??
  63. ????????if(null?!=?pageSize?&&?0?
  64. ????????????query.setRows(pageSize);??
  65. ????????}??
  66. ????????QueryResponse?qr?=?server.query(query);??
  67. ????????SolrDocumentList?docList?=?qr.getResults();??
  68. ????????return?docList;??
  69. ????}??
  70. ??????
  71. ????public?static?Map?getQueryProperty(Object?obj)?throws?Exception?{??
  72. ????????Map?result?=?new?TreeMap();??
  73. ????????//?获取实体类的所有属性,返回Fields数组???
  74. ????????Field[]?fields?=?obj.getClass().getDeclaredFields();??
  75. ????????for(Field?f?:?fields)?{??
  76. ????????????String?name?=?f.getName();//?获取属性的名字???
  77. ????????????String?type?=?f.getGenericType().toString();??
  78. ????????????if("class?java.lang.String".equals(type))?{//?如果type是类类型,则前面包含"class?",后面跟类名???
  79. ????????????????Method?me?=?obj.getClass().getMethod("get"?+?UpperCaseField(name));??
  80. ????????????????String?tem?=?(String)?me.invoke(obj);??
  81. ????????????????if(null?!=?tem)?{??
  82. ????????????????????result.put(name,?tem);??
  83. ????????????????}??
  84. ????????????}??
  85. ????????}??
  86. ????????return?result;??
  87. ????}??
  88. ??????
  89. ????public?static?List?querySolrResult(Object?propertyObj,?Object?compositorObj,?Integer?pageSize)?throws?Exception?{??
  90. ????????Map?propertyMap?=?new?TreeMap();??
  91. ????????Map?compositorMap?=?new?TreeMap();??
  92. ????????propertyMap?=?getQueryProperty(propertyObj);??
  93. ????????compositorMap?=?getQueryProperty(compositorObj);??
  94. ????????SolrDocumentList?docList?=?query(propertyMap,?compositorMap,?pageSize);??
  95. ????????List?list?=?new?ArrayList();??
  96. ????????for(Object?obj?:?docList)?{??
  97. ????????????list.add(obj);??
  98. ????????}??
  99. ????????return?list;??
  100. ????}??
  101. ??????
  102. ????private?static?String?UpperCaseField(String?name)?{??
  103. ????????return?name.replaceFirst(name.substring(0,?1),?name.substring(0,?1).toUpperCase());??
  104. ????}??
  105. ??
  106. ????public?CommonsHttpSolrServer?getServer()?{??
  107. ????????return?server;??
  108. ????}??
  109. ??
  110. ????public?void?setServer(CommonsHttpSolrServer?server)?{??
  111. ????????this.server?=?server;??
  112. ????}??
  113. ??
  114. }??
Java代码??
  1. package?org.nstcrm.person.util;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. import?java.lang.reflect.Method;??
  5. import?java.net.MalformedURLException;??
  6. import?java.util.ArrayList;??
  7. import?java.util.List;??
  8. import?java.util.Map;??
  9. import?java.util.TreeMap;??
  10. ??
  11. import?org.apache.solr.client.solrj.SolrQuery;??
  12. import?org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;??
  13. import?org.apache.solr.client.solrj.response.QueryResponse;??
  14. import?org.apache.solr.common.SolrDocumentList;??
  15. ??
  16. public?class?SolrHttpServer?{??
  17. ????//private?Logger?logger?=?LoggerFactory.getLogger(this.getClass());??
  18. ????private?final?static?String?URL?=?"http://localhost:8080/solr";??
  19. ????private?final?static?Integer?SOCKE_TTIMEOUT?=?1000;?//?socket?read?timeout??
  20. ????private?final?static?Integer?CONN_TIMEOUT?=?100;??
  21. ????private?final?static?Integer?MAXCONN_DEFAULT?=?100;??
  22. ????private?final?static?Integer?MAXCONN_TOTAL?=?100;??
  23. ????private?final?static?Integer?MAXRETRIES?=?1;??
  24. ????private?static?CommonsHttpSolrServer?server?=?null;??
  25. ????private?final?static?String?ASC?=?"asc";??
  26. ??????
  27. ????public?void?init()?throws?MalformedURLException?{??
  28. ????????????server?=?new?CommonsHttpSolrServer(?URL?);??
  29. ????????????//server.setParser(new?XMLResponseParser());??
  30. ????????????server.setSoTimeout(SOCKE_TTIMEOUT);??
  31. ????????????server.setConnectionTimeout(CONN_TIMEOUT);??
  32. ????????????server.setDefaultMaxConnectionsPerHost(MAXCONN_DEFAULT);??
  33. ????????????server.setMaxTotalConnections(MAXCONN_TOTAL);??
  34. ????????????server.setFollowRedirects(false);??
  35. ????????????server.setAllowCompression(true);??
  36. ????????????server.setMaxRetries(MAXRETRIES);??
  37. ????}??
  38. ??????
  39. ????public?static?SolrDocumentList?query(Map?property,?Map?compositor,?Integer?pageSize)?throws?Exception?{??
  40. ????????SolrQuery?query?=?new?SolrQuery();??
  41. ????????//?设置搜索字段??
  42. ????????if(null?==?property)?{??
  43. ????????????throw?new?Exception("搜索字段不可为空!");??
  44. ????????}?else?{??
  45. ????????????for(Object?obj?:?property.keySet())?{??
  46. ????????????????StringBuffer?sb?=?new?StringBuffer();??
  47. ????????????????sb.append(obj.toString()).append(":");??
  48. ????????????????sb.append(property.get(obj));??
  49. ????????????????String?sql?=?(sb.toString()).replace("AND",?"?AND?").replace("OR",?"?OR?").replace("NOT",?"?NOT?");??
  50. ????????????????query.setQuery(sql);??
  51. ????????????}??
  52. ????????}??
  53. ????????//?设置结果排序??
  54. ????????if(null?!=?compositor)?{??
  55. ????????????for(Object?obj?:?compositor.keySet())?{??
  56. ????????????????if(ASC?==?compositor.get(obj)?||?ASC.equals(compositor.get(obj)))?{??
  57. ????????????????????query.addSortField(obj.toString(),?SolrQuery.ORDER.asc);??
  58. ????????????????}?else?{??
  59. ????????????????????query.addSortField(obj.toString(),?SolrQuery.ORDER.desc);??
  60. ????????????????}??
  61. ????????????}??
  62. ????????}??
  63. ????????if(null?!=?pageSize?&&?0?
  64. ????????????query.setRows(pageSize);??
  65. ????????}??
  66. ????????QueryResponse?qr?=?server.query(query);??
  67. ????????SolrDocumentList?docList?=?qr.getResults();??
  68. ????????return?docList;??
  69. ????}??
  70. ??????
  71. ????public?static?Map?getQueryProperty(Object?obj)?throws?Exception?{??
  72. ????????Map?result?=?new?TreeMap();??
  73. ????????//?获取实体类的所有属性,返回Fields数组??
  74. ????????Field[]?fields?=?obj.getClass().getDeclaredFields();??
  75. ????????for(Field?f?:?fields)?{??
  76. ????????????String?name?=?f.getName();//?获取属性的名字??
  77. ????????????String?type?=?f.getGenericType().toString();??
  78. ????????????if("class?java.lang.String".equals(type))?{//?如果type是类类型,则前面包含"class?",后面跟类名??
  79. ????????????????Method?me?=?obj.getClass().getMethod("get"?+?UpperCaseField(name));??
  80. ????????????????String?tem?=?(String)?me.invoke(obj);??
  81. ????????????????if(null?!=?tem)?{??
  82. ????????????????????result.put(name,?tem);??
  83. ????????????????}??
  84. ????????????}??
  85. ????????}??
  86. ????????return?result;??
  87. ????}??
  88. ??????
  89. ????public?static?List?querySolrResult(Object?propertyObj,?Object?compositorObj,?Integer?pageSize)?throws?Exception?{??
  90. ????????Map?propertyMap?=?new?TreeMap();??
  91. ????????Map?compositorMap?=?new?TreeMap();??
  92. ????????propertyMap?=?getQueryProperty(propertyObj);??
  93. ????????compositorMap?=?getQueryProperty(compositorObj);??
  94. ????????SolrDocumentList?docList?=?query(propertyMap,?compositorMap,?pageSize);??
  95. ????????List?list?=?new?ArrayList();??
  96. ????????for(Object?obj?:?docList)?{??
  97. ????????????list.add(obj);??
  98. ????????}??
  99. ????????return?list;??
  100. ????}??
  101. ??????
  102. ????private?static?String?UpperCaseField(String?name)?{??
  103. ????????return?name.replaceFirst(name.substring(0,?1),?name.substring(0,?1).toUpperCase());??
  104. ????}??
  105. ??
  106. ????public?CommonsHttpSolrServer?getServer()?{??
  107. ????????return?server;??
  108. ????}??
  109. ??
  110. ????public?void?setServer(CommonsHttpSolrServer?server)?{??
  111. ????????this.server?=?server;??
  112. ????}??
  113. ??
  114. }??


?

[java]?view plaincopyprint?
  1. Solr?1.4.1配置和SolrJ的用法??
  2. 一、??Solr基本安装和配置??
  3. 1,从官网镜像服务器下载最新版本apache-solr-1.4.1。下载地址:???
  4. http://apache.etoak.com//lucene/solr/,并解压缩???
  5. 2,在D盘建立一个SolrHome文件夹来存放solr的配置文件等,例如:在D盘WORK目录下穿件一个SolrHome文件夹:?D:\WORK\SolrHome,??
  6. 3,在刚解压的apache-solr-1.4.1,找到apache-solr-1.4.1\example下找到solr文件夹,复制到SolrHome下.??
  7. 4,将apache-solr-1.4.1\dist\apache-solr-1.4.1.war中的apache-solr-1.4.1.war复制到tomcat中的\webapps下并重命名为solr,启动tomcat,解压war包后,停止tomcat.??
  8. 5,在解压后的solr中找到web.xml,打开:将的值设为SolrHome的目录地址??
  9. ??????
  10. ???????solr/home??
  11. ???????D:\WORK\SolrHome\solr??
  12. ???????java.lang.String??
  13. ??????
  14. 6,在D:\WORK\SolrHome\solr\conf找到solrconfig.xml文件,打开,修改??
  15. ????${solr.data.dir:./solr/data}??
  16. ?其中solr.data.dir存放的是索引目录.??
  17. 7,添加中文支持,修改tomcat的配置文件server.xml,如下:??
  18. ????????????<>"80"?protocol="HTTP/1.1"???
  19. ???????????????maxThreads="150"?connectionTimeout="20000"???
  20. ???????????????redirectPort="8443"?URIEncoding="UTF-8"/>??
  21. 8,启动tomcat,IE中输入:http://localhost:80/solr?即可浏览solr服务器.???
  22. 二、Solr服务器复制的配置??
  23. 1,首先测试在本机上开启三个tomcat的服务器:一个端口是80,另一个是9888??
  24. 2,按照标题一的配置对第二和第三个tomcat服务器进行类似的配置,注意SolrHome的目录不要相同即可,其他的配置不变.?例如:以本机为例??
  25. ??
  26. ??
  27. tomcat命名?????????URL????SolrHome目录URI?????????web.xml配置??
  28. tomcat0?(master)????http://localhost:80/solr????D:\WORK\SolrHome\solr???D:\WORK\SolrHome\solr???
  29. tomcat1?(slave)?http://localhost:9888/solr??E:\WORK\SolrHome\solr???E:\WORK\SolrHome\solr???
  30. tomcat2??
  31. (salve)?http://localhost:9008/solr??F:\WORK\SolrHome\solr???F:\WORK\SolrHome\solr???
  32. ??
  33. ??
  34. ??
  35. ??
  36. 3,以上两步配置好之后,在主服务器tomcat0(master)的SolrHome中找到solrconfig.xml文件,加入如下配置.??
  37. ????<>"/replication"?class="solr.ReplicationHandler"?>??
  38. ????<>"master">??
  39. ??????<>"replicateAfter">commit??
  40. ??????<>"replicateAfter">startup??
  41. ??????<>"confFiles">schema.xml,stopwords.txt??
  42. ??????
  43. ???
  44. 在从服务器tomcat1(slave)和tomcat1(slave)的SolrHome中找到solrconfig.xml文件加入如下配置:??
  45. ????<>"/replication"?class="solr.ReplicationHandler"?>????
  46. ????????<>"slave">????
  47. ??????????<>"masterUrl">http://localhost/solr/replication?????
  48. ??????????<>"pollInterval">00:00:60????
  49. ????????????
  50. ??????
  51. 4,在tomcat0上创建索引,使用客户端solrj创建索引,jar包在apache-solr-1.4.1压缩包中.代码如下:??
  52. ??public?class?SlorTest3?{??
  53. ????private?static?CommonsHttpSolrServer??server?=?null;??
  54. //??private?static?SolrServer?server?=?null;???
  55. ????public?SlorTest3(){??
  56. ????????try?{??
  57. ????????????server?=?new?CommonsHttpSolrServer("http://localhost/solr");??
  58. ????????????server.setConnectionTimeout(100);??
  59. ????????????server.setDefaultMaxConnectionsPerHost(100);??
  60. ????????????server.setMaxTotalConnections(100);??
  61. ????????}?catch?(IOException?e)?{??
  62. ????????????e.printStackTrace();??
  63. ????????}??
  64. ??????????????
  65. ????}??
  66. ????@Test??
  67. ????public?void?testIndexCreate(){??
  68. ????????List?docs?=?new?ArrayList();??
  69. ????????for(int?i=300;i<500;i++){??
  70. ????????????SolrInputDocument?doc?=?new?SolrInputDocument();??
  71. ????????????doc.addField("zjid",?i);??//需要在sechma.xml中配置字段???
  72. ????????????doc.addField("title",?"云状空化多个气泡的生长和溃灭");??
  73. ????????????doc.addField("ssid",?"ss"+i);??
  74. ????????????doc.addField("dxid",?"dx"+i);??
  75. ????????????docs.add(doc);??
  76. ????????}??
  77. ????????????try?{??
  78. ????????????????server.add(docs);??
  79. ????????????????server.commit();??
  80. ????????????????System.out.println("----索引创建完毕!!!----");??
  81. ????????????}?catch?(SolrServerException?e)?{??
  82. ????????????????e.printStackTrace();??
  83. ????????????}?catch?(IOException?e)?{??
  84. ????????????????e.printStackTrace();??
  85. ????????????}??
  86. ????}??
  87. }??
  88. 5,分别启动三台tomcat服务器.打开IE浏览器分别输入:其中localhost=192.168.169.121??
  89. http://localhost:9888/solr?点击?即可从主solr服务器上复制索引???
  90. ?????
  91. ??
  92. 三、Solr服务器分发(shard)配置??
  93. 1,??开启四台tomcat服务器,其中三台在本机上,一台在远端.清单如下:??
  94. ?注意:四台服务器的配置要相同.其中的schema.xml字段配置一定要一致.??
  95. ??
  96. Name?????????URL????SolrHome目录URI?????
  97. tomcatQuery?http://localhost:80/solr????D:\WORK\SolrHome\solr??????
  98. tomcat0?(shard)?http://localhost:9888/solr??E:\WORK\SolrHome\solr??????
  99. tomcat1?(shard)?http://localhost:9008/solr??F:\WORK\SolrHome\solr??????
  100. tomcat2?(shard)?http://192.168.169.48:9888/solr?D:\WORK\SolrHome\solr??????
  101. ??
  102. ??
  103. ??
  104. 2,??配置较简单,只需要在tomcatQuery上的SoleHome的solrconfig.xml文件中修改??
  105. 其他的solr服务器不需要配置。??
  106. ??<>"standard"?class="solr.SearchHandler"?default="true">??
  107. ??????
  108. ?????<>"defaults">??
  109. ???????<>"echoParams">explicit??
  110. ???????<>"shards">localhost:9088/solr,localhost:9888/solr,192.168.169.48:9888/solr??
  111. ?????????
  112. ???????
  113. ????
  114. 3,??使用slorj的清除原有的索引.或者手动删除。??
  115. 4,??编写代码,将lucene建立的索引(1G左右,874400条记录),按照比例通过solrj分发到三台solr(shard)服务器上,代码如下:??
  116. public?class?IndexCreate{??
  117. ????private?static?CommonsHttpSolrServer?server;??
  118. ????public??CommonsHttpSolrServer?getServer(String?hostUrl){??
  119. ????????CommonsHttpSolrServer?server?=?null;??
  120. ????????try?{??
  121. ????????????server?=?new?CommonsHttpSolrServer(hostUrl);??
  122. ????????????server.setConnectionTimeout(100);??
  123. ????????????server.setDefaultMaxConnectionsPerHost(100);??
  124. ????????????server.setMaxTotalConnections(100);??
  125. ????????}?catch?(IOException?e)?{??
  126. ????????????System.out.println("请检查tomcat服务器或端口是否开启!");??
  127. ????????}??
  128. ????????return?server;??
  129. ????}??
  130. ????@SuppressWarnings("deprecation")??
  131. ????public?void?readerHostCreate(String[]?hosts)?throws?CorruptIndexException,?IOException{??
  132. ????????IndexReader?reader?=?IndexReader.open("c:\\index");??
  133. ????????System.out.println("总记录数:?"+reader.numDocs());??
  134. ????????int?hostNum?=?hosts.length;??
  135. ????????int?lengh?=?reader.numDocs()/hostNum;?//根据主机数平分索引长度???
  136. ????????int?j?=?reader.numDocs()%hostNum;?????//取余???
  137. ????????for(int?i?=?0;i<>
  138. ????????????long?startTime?=?new?Date().getTime();??
  139. ????????????String?url?=?hosts[i].substring(hosts[i].indexOf("//")+2,hosts[i].lastIndexOf("/"));??
  140. ????????????System.out.println("第"+(i+1)+"次,在主机:"+url+"?上创建索引,创建时间"+new?Date());??
  141. ????????????if(i==(hosts.length-1)){??
  142. ????????????????hostlist(reader,lengh*i,lengh*(i+1)+j,hosts[i]);??
  143. ??????????????
  144. ????????????}else{??
  145. ????????????????hostlist(reader,lengh*i,lengh*(i+1),hosts[i]);??
  146. ????????????}??
  147. ????????????System.out.println("结束时间"+new?Date());??
  148. ????????????long?endTime?=?new?Date().getTime();??
  149. ????????????long?ms?=?(endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;??
  150. ????????????System.out.println("本次索引创建完毕,一共用了"+(endTime-startTime)/60000+"分"?+??
  151. ????????????????????""+((endTime-startTime)%60000)/1000+"秒"+ms+"毫秒");??
  152. ????????????System.out.println("****************************");??
  153. ????????}??
  154. ????????reader.close();??
  155. ????}??
  156. ????@SuppressWarnings("static-access")??
  157. ????public?void?hostlist(IndexReader?reader,int?startLengh,int?endLengh,String?hostUrl)?throws?CorruptIndexException,?IOException{??
  158. ????????List?beans?=?new?LinkedList();??
  159. ????????int?count?=?0;??
  160. ????????this.server?=?getServer(hostUrl);??
  161. ????????for(int?i=startLengh;i<>
  162. ????????????Document?doc?=?reader.document(i);??
  163. ????????????BookIndex?book?=?new?BookIndex();??
  164. ????????????book.setZjid(doc.getField("zjid").stringValue());??
  165. ????????????book.setTitle(doc.getField("title").stringValue());??
  166. ????????????book.setSsid(doc.getField("ssid").stringValue());??
  167. ????????????book.setDxid(doc.getField("dxid").stringValue());??
  168. ????????????book.setBookname(doc.getField("bookname").stringValue());??
  169. ????????????book.setAuthor(doc.getField("author").stringValue());??
  170. ????????????book.setPublisher(doc.getField("publisher").stringValue());??
  171. ????????????book.setPubdate(doc.getField("pubdate").stringValue());??
  172. ????????????book.setYear(doc.getField("year").stringValue());??
  173. ????????????book.setFenlei(doc.getField("fenlei").stringValue());??
  174. ????????????book.setscore1(doc.getField("score").stringValue());??
  175. ????????????book.setIsbn(doc.getField("isbn").stringValue());??
  176. ????????????book.setFenleiurl(doc.getField("fenleiurl").stringValue());??
  177. ????????????book.setMulu(doc.getField("mulu").stringValue());??
  178. ????????????book.setIsp(doc.getField("isp").stringValue());??
  179. ????????????book.setIep(doc.getField("iep").stringValue());??
  180. ????????????beans.add(book);??
  181. ????????????if(beans.size()%3000==0){??
  182. ????????????????createIndex(beans,hostUrl,server);??
  183. ????????????????beans.clear();??
  184. ????????????????System.out.println("---索引次数:"+(count+1)+"---");??
  185. ????????????????count++;??
  186. ????????????}??
  187. ????????}??
  188. ????????System.out.println("beansSize?的大小?"+beans.size());??
  189. ????????if(beans.size()>0){??
  190. ????????????createIndex(beans,hostUrl,server);??
  191. ????????????beans.clear();??
  192. ????????}??
  193. ??????????
  194. ????}??
  195. ????public?void?createIndex(List?beans,String?hostUrl,CommonsHttpSolrServer?server){??
  196. ????????try?{??
  197. ????????????????server.addBeans(beans);??
  198. ????????????????server.commit();??
  199. ????????}?catch?(SolrServerException?e)?{??
  200. ????????????e.printStackTrace();??
  201. ????????}?catch?(IOException?e)?{??
  202. ????????????e.printStackTrace();??
  203. ????????}??
  204. ????}??
  205. ????public?static?void?main(String[]?args)?throws?CorruptIndexException,?IOException{??
  206. ????????IndexCreate?as?=?new?IndexCreate();??
  207. ????????String[]?hosts?=?new?String[]?{"http://192.168.169.121:9888/solr","http://192.168.169.121:9088/solr","http://192.168.169.48:9888/solr"};??
  208. ????????long?startTime?=?new?Date().getTime();??
  209. ????????as.readerHostCreate(hosts);??
  210. ????????long?endTime?=?new?Date().getTime();??
  211. ????????System.out.println("-------------------");??
  212. ????????long?ms?=?(endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;??
  213. ????????System.out.println("全部索引创建完毕,一共用了"+(endTime-startTime)/60000+"分"?+??
  214. ????????????????""+((endTime-startTime)%60000)/1000+"秒"+ms+"毫秒");??
  215. ????}??
  216. ??
  217. }??
  218. JavaBean类BookIndex.java代码如下:??
  219. 说明变量名与sechma.xml中的配置要相同.注意:不能使用score这个变量或者字段,与slor配置冲突,报exception。??
  220. import?org.apache.solr.client.solrj.beans.Field;??
  221. ??
  222. public?class?BookIndex?{??
  223. ????@Field????
  224. ????private?String?zjid?;??
  225. ????@Field????
  226. ????private?String?title;??
  227. ????@Field????
  228. ????private?String?ssid;??
  229. ????@Field????
  230. ????private?String?dxid;??
  231. ????@Field???
  232. ????private?String?bookname;??
  233. ????@Field???
  234. ????private?String?author;??
  235. ????@Field???
  236. ????private?String?publisher;??
  237. ????@Field???
  238. ????private?String?pubdate;??
  239. ????@Field???
  240. ????private?String?year;??
  241. ????@Field???
  242. ????private?String?fenlei;??
  243. ????@Field???
  244. ????private?String?score1;??
  245. ????@Field???
  246. ????private?String?isbn;??
  247. ????@Field???
  248. ????private?String?fenleiurl;??
  249. ????@Field???
  250. ????private?String?mulu;??
  251. ????@Field???
  252. ????private?String?isp;??
  253. ????@Field???
  254. ????private?String?iep;??
  255. ????public?getters();//get方法???
  256. ????public?setters();//set方法???
  257. }??
  258. ??
  259. ??
  260. 5,??同时开启四台服务器,运行上面代码:??
  261. 6,??打开IE查询??
  262. 打开:http://localhost/solr???
  263. 打开:http://localhost:9888/solr???
  264. 打开http://localhost:9008/solr???
  265. 打开http://192.168.168.48:9888/solr???
  266. ??
  267. 四、Solr的Multicore(分片)配置??
  268. 第一步,将apache-solr-1.4.1\example下的multicore复制到先前配置的solr/home下。??
  269. ????其中multicore下的solr.xml配置如下:??
  270. <>"false">??
  271. ???<>"/admin/cores">??
  272. ????<>"core0"?instanceDir="core0">????
  273. ??????<>"dataDir"?value="/data/core0"?/>????
  274. ??????
  275. ????<>"core1"?instanceDir="core1">??
  276. ??????<>"dataDir"?value="/data/core1"?/>????
  277. ??????
  278. ????<>"core2"?instanceDir="core2">??
  279. ??????<>"dataDir"?value="/data/core2"?/>????
  280. ??????
  281. ????
  282. ??
  283. 第二步,修改Tomcat?6.0\webapps\solr\WEB-INF下的web.xml文件如下??
  284. D:\WORK\SolrHome\multicore??
  285. 第三步,开启服务器,打开IEhttp://localhost/solr???
  286. ???
  287. 五、SolrJ的用法??
  288. 1,SolrJ的用法??
  289. ??solrJ是与solr服务器交互的客户端工具.使用solrJ不必考虑solr服务器的输出格式,以及对文档的解析。solrJ会根据用户发送的请求将数据结果集(collection)返回给用户.??
  290. 2,使用SolrJ建立索引??
  291. ??事例代码:??
  292. //得到连接solr服务器的CommonsHttpSolrServer对象.通过这个对象处理用户提交的请求:???
  293. ?CommonsHttpSolrServer?server?=?new?CommonsHttpSolrServer("http://localhost:9888/solr");??
  294. public?void?testIndexCreate(){??
  295. ????????List?docs?=?new?ArrayList();??
  296. //?SolrInputDocumentle类似与Document类,用于创建索引文档,和向文档中添加字段???
  297. ????????for(int?i=300;i<500;i++){??
  298. ????????????SolrInputDocument?doc?=?new?SolrInputDocument();??
  299. ????????????doc.addField("zjid",?i+"_id");????
  300. ????????????doc.addField("title",?i+"_title");??
  301. ????????????doc.addField("ssid",?"ss_"+i);??
  302. ????????????doc.addField("dxid",?"dx_"+i);??
  303. ????????????docs.add(doc);??
  304. ????????}??
  305. ????????????try?{??
  306. ????????????????server.add(docs);??
  307. ????????????????server.commit();//以更新(update)的方式提交???
  308. ????????????????System.out.println("----索引创建完毕!!!----");??
  309. ????????????}?catch?(SolrServerException?e)?{??
  310. ????????????????e.printStackTrace();??
  311. ????????????}?catch?(IOException?e)?{??
  312. ????????????????e.printStackTrace();??
  313. ????????????}??
  314. ????}??
  315. 以javaBean的方式创建索引:??
  316. public?class?BookIndex?{??
  317. ????@Field??
  318. ????private?String?zjid;??
  319. ????@Field??
  320. ????private?String?zhangjie;??
  321. ????@Field??
  322. ????private?String?ssid;??
  323. ????@Field??
  324. ????private?String?qwpos;??
  325. ????@Field??
  326. ????private?String?publishDate;??
  327. ????@Field??
  328. ????private?String?mulu;??
  329. ????@Field??
  330. ????private?String?fenleiurl;??
  331. ????@Field??
  332. ????private?String?fenlei;??
  333. ????@Field??
  334. ????private?String?dxid;??
  335. ????@Field??
  336. ????private?String?author;??
  337. ????@Field??
  338. ????private?String?address;??
  339. ????@Field??
  340. ????private?String?bookname;??
  341. …………………??
  342. }??
  343. public?void?testBean(){??
  344. ????????List?beans?=?new?ArrayList();??
  345. ????????for(int?i=0;i<10;i++){??
  346. ????????????BookIndex?book?=?new?BookIndex();??
  347. ????????????book.setZjid(i+"id");??
  348. ????????????book.setTitle(i+"title");??
  349. ????????????//set方法???
  350. ????????????beans.add(book);??
  351. ????????}??
  352. ????????try?{??
  353. ????????????server.addBeans(beans);??
  354. ????????????server.commit();??
  355. ????????????System.out.println("----索引创建完毕!!!----");??
  356. ????????}?catch?(SolrServerException?e)?{??
  357. ????????????e.printStackTrace();??
  358. ????????}?catch?(IOException?e)?{??
  359. ????????????e.printStackTrace();??
  360. ????????}??
  361. ????}??
  362. 3,??SolrJ常见的查询???
  363. ??a,??查询索引中的全部字段的内容:??SolrQuery?query?=?new?SolrQuery("*:*");??
  364. ???事例代码:??
  365. ???public?void?testQuery1(){??
  366. ????????SolrQuery?query?=?new?SolrQuery("*:*");??
  367. ????????query.setStart(20);//设置起始位置???
  368. ????????query.setRows(10);?//查询组数???
  369. ????????QueryResponse?response?=?null;???
  370. ????????try?{??
  371. ????????????response?=?server.query(query);//响应向服务器提交的查询请求???
  372. ????????????System.out.println(response);??
  373. ????????}?catch?(SolrServerException?e)?{??
  374. ????????????e.printStackTrace();??
  375. ????????}??
  376. ??????????
  377. ????????List?docs?=?response.getResults();//得到结果集???
  378. ????????for(SolrDocument?doc:docs){//遍历结果集???
  379. ????????????for(Iterator?iter?=?doc.iterator();iter.hasNext();){??
  380. Map.Entry?entry?=?(Entry)iter.next();??
  381. ????????????????System.out.print("Key?:"+entry.getKey()+"??");??
  382. ????????????????System.out.println("Value?:"+entry.getValue());??
  383. ????????????}??
  384. ????????????System.out.println("------------");??
  385. ????????}?????
  386. }??
  387. b,?查询某一字段的内容??
  388. ????String?queryString??=?“zjid:5_id”;//写法为字段名:查询的内容???
  389. ????SolrQuery?query?=?new?SolrQuery(queryString);??
  390. c,?查询copyField的内容??
  391. ???copyField是查询的默认字段,当不指明查询字段的field的时。查询的请求会在copyField匹配:?详细参见schema..xml的配置.??
  392. ????String?queryString??=?“XXX”??
  393. ???SolrQuery?query?=?new?SolrQuery(queryString);??
  394. 4,??索引删除??
  395. 事例代码?:??
  396. ?public?void?testClear()?{??
  397. ????????server.setRequestWriter(new?BinaryRequestWriter());//提高性能采用流输出方式???
  398. ????????try?{??
  399. ????????????server.deleteByQuery("*:*");??
  400. ????????????server.commit();??
  401. ????????????System.out.println("----清除索引---");??
  402. ????????}?catch?(SolrServerException?e)?{??
  403. ????????????e.printStackTrace();??
  404. ????????}?catch?(IOException?e)?{??
  405. ????????????e.printStackTrace();??
  406. ????????}??
  407. ????}??
  408. 5,??高亮的使用Highlight??
  409. public?List?getQueryString(String?queryString,int?start,int?pageSize)?{??
  410. ????????SolrQuery?query?=?new?SolrQuery(queryString);??
  411. ????????query.setHighlight(true);?//开启高亮组件???
  412. ????????query.addHighlightField("mulu");//高亮字段???
  413. ????????query.setHighlightSimplePre("");//标记???
  414. ????????query.setHighlightSimplePost("");??
  415. ????????query.set("hl.usePhraseHighlighter",?true);??
  416. ????????query.set("hl.highlightMultiTerm",?true);??
  417. ????????query.set("hl.snippets",?3);//三个片段,默认是1???
  418. ????????query.set("hl.fragsize",?50);//每个片段50个字,默认是100???
  419. ????????//???
  420. ????????query.setStart(start);?//起始位置?…分页???
  421. ????????query.setRows(pageSize);//文档数???
  422. ??????????
  423. ????????try?{??
  424. ????????????response?=?server.query(query);??
  425. ????????}?catch?(SolrServerException?e)?{??
  426. ????????????e.printStackTrace();??
  427. ????????}??
  428. ????????List?bookLists?=?response.getBeans(BookIndex.class);??
  429. ????????Map<><>>>?h1?=?response.getHighlighting();??
  430. ??????????
  431. ????????List?books?=?new?ArrayList();??
  432. ????????for(BookIndex?bookIndex?:?bookLists){??
  433. ????????????Map<>>??map?=?h1.get(bookIndex.getZjid());??
  434. //以文档的唯一id作为Map<><>>>的key值.???
  435. ????????????Book?book?=?new?Book();??
  436. ????????????//copy字段???
  437. ????????????book.setBookname(bookIndex.getBookname());??
  438. ????????????book.setZjid(bookIndex.getZjid());??
  439. ????????????if(map.get("mulu")!=null){??
  440. ????????????????List?strMulu?=?map.get("mulu");??
  441. ????????????????StringBuffer?buf??=?new?StringBuffer();??
  442. ????????????????for(int?i?=0;i<>
  443. ????????????????????buf.append(strMulu.get(i));??
  444. ????????????????????buf.append("...");??
  445. ????????????????????if(i>3){??
  446. ????????????????????????break;??
  447. ????????????????????}??
  448. ????????????????}??
  449. ????????????????book.setSummary(buf.toString());??
  450. ????????????}else{??
  451. ????????????????if(bookIndex.getMulu().length()>100){??
  452. ????????????????????book.setSummary(bookIndex.getMulu().substring(0,100)+"...");??
  453. ????????????????}else{??
  454. ????????????????????book.setSummary(bookIndex.getMulu()+"...");??
  455. ????????????????}??
  456. ??????????????????
  457. ????????????}??
  458. ????????????????books.add(book);??
  459. ????????}??
  460. ????????return?books;??
  461. ??????
  462. ????}??
  463. 6,?分组Fact??
  464. //需要注意的是参与分组的字段是不需要分词的,比如:产品的类别.kind???
  465. public?void?testFact(){??
  466. ????????String?queryString?=?"kind:儿童图书";??
  467. ????????SolrQuery?query?=?new?SolrQuery().setQuery(queryString);??
  468. ????????query.setFacet(true);?//开启分组???
  469. ????????query.addFacetField("bookname");//分组字段???
  470. query.addFacetField("title");??
  471. ????????query.setFacetMinCount(1);??
  472. query.addSortField(?"zjid",?SolrQuery.ORDER.asc?);//排序字段???
  473. ??
  474. ????????query.setRows(10);??
  475. ????????QueryResponse?response?=?null;??
  476. ????????try?{??
  477. ????????????response?=?server.query(query);??
  478. ????????????System.out.println(response);??
  479. ????????}?catch?(SolrServerException?e)?{??
  480. ????????????e.printStackTrace();??
  481. ????????}??
  482. ????????List?facets?=?response.getFacetFields();??
  483. ????????for?(FacetField?facet?:?facets)?{??
  484. ??????????System.out.println("Facet:"?+?facet);??
  485. ????????}??
  486. ??????????
  487. ????}???
  488. ??
  489. 六、一个简单的web引用:??
  490. ?首先说明的是索引来源,是根据已有的lucene索引上开发的,又因为lucene的索引直接用solrJ应用效果不好,会出现很多问题,找不到类似的解决办法,比如全字段查询,和高亮分组等,和多级索引目录…。但用solrJ创建的索引不存在类似的问题.??
  491. ?大致的思路是,读取已有的lucene索引?,再用solrJ创建索引并分发到多台机器上,最后再做开发.??
  492. ?第一步:读取lucene的多级索引目录,用solrJ创建和分发索引;??
  493. 需注意的是:要加大虚拟机的内存,因为采用的map做为缓存,理论上虚拟机的内存大,map的存储的索引文档数也就多.主要是针对内存溢出.??
  494. ??代码?:??
  495. package?org.readerIndex;??
  496. import?org.apache.solr.client.solrj.beans.Field;??
  497. public?class?BookIndex2?{??
  498. ????@Field??
  499. ????private?String?zjid;??
  500. ????@Field??
  501. ????private?String?zhangjie;??
  502. ????@Field??
  503. ????private?String?ssid;??
  504. ????@Field??
  505. ????private?String?qwpos;??
  506. ????@Field??
  507. ????private?String?publishDate;??
  508. ????@Field??
  509. ????private?String?mulu;??
  510. ????@Field??
  511. ????private?String?fenleiurl;??
  512. ????@Field??
  513. ????private?String?fenlei;??
  514. ????@Field??
  515. ????private?String?dxid;??
  516. ????@Field??
  517. ????private?String?author;??
  518. ????@Field??
  519. ????private?String?address;??
  520. ????@Field??
  521. ????private?String?bookname;??
  522. ????public?String?getZjid()?{??
  523. ????????return?zjid;??
  524. ???…………………………………………??
  525. ????}??
  526. package?org.readerIndex;??
  527. ??
  528. import?java.io.File;??
  529. import?java.io.IOException;??
  530. import?java.text.SimpleDateFormat;??
  531. import?java.util.ArrayList;??
  532. import?java.util.Date;??
  533. import?java.util.List;??
  534. ??
  535. ??
  536. import?org.apache.lucene.document.Document;??
  537. import?org.apache.lucene.index.CorruptIndexException;??
  538. import?org.apache.lucene.index.IndexReader;??
  539. import?org.apache.lucene.store.LockObtainFailedException;??
  540. import?org.apache.solr.client.solrj.SolrServerException;??
  541. import?org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;??
  542. ??
  543. ??
  544. ??
  545. ??
  546. public?class?ReaderIndex?{??
  547. ????public??CommonsHttpSolrServer?getServer(String?hostUrl){??
  548. ????????CommonsHttpSolrServer?server?=?null;??
  549. ????????try?{??
  550. ????????????server?=?new?CommonsHttpSolrServer(hostUrl);??
  551. ????????????server.setConnectionTimeout(100);??
  552. ????????????server.setDefaultMaxConnectionsPerHost(100);??
  553. ????????????server.setMaxTotalConnections(100);??
  554. ????????}?catch?(IOException?e)?{??
  555. ????????????System.out.println("请检查tomcat服务器或端口是否开启!");??
  556. ????????}??
  557. ????????return?server;??
  558. ????}??
  559. ????public?void?indexDocuements(String?path,String[]?hostUrls)?throws?CorruptIndexException,?LockObtainFailedException,?IOException{??
  560. ????????File?pareFile?=?new?File(path);??
  561. ????????List?list?=?new?ArrayList();??
  562. ????????getFile(pareFile,list);?//递归方法得到路径保存到list中???
  563. ????????System.out.println("***程序一共递归到"+list.size()+"个索引目录***");??
  564. ????????int?arevageSize?=?list.size()/hostUrls.length;//?根据主机数平分目录???
  565. ????????int?remainSize??=?list.size()%hostUrls.length;//取余???
  566. ????????SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");??
  567. ????????for(int?i=0;i<>
  568. ????????????Date?startDate?=?new?Date();??
  569. ????????????String?url?=?hostUrls[i].substring(hostUrls[i].indexOf("//")+2,hostUrls[i].lastIndexOf("/"));??
  570. ????????????System.out.println("第"+(i+1)+"次,在主机:"+url+"?上创建索引,创建时间??"+sdf.format(startDate));??
  571. ????????????if(i==(hostUrls.length-1)){??
  572. ????????????????list(list,arevageSize*i,arevageSize*(i+1)+remainSize,hostUrls[i]);??
  573. ????????????}else{??
  574. ????????????????list(list,arevageSize*i,arevageSize*(i+1),hostUrls[i]);??
  575. ????????????}??
  576. ????????????Date?endDate?=?new?Date();??
  577. ????????????System.out.println("本次索引结束时间为:"+sdf.format(endDate));??
  578. ????????}??
  579. ??
  580. ????}??
  581. ????public?void?list(List?list,int?start,int?end,String?url){??
  582. ????????CommonsHttpSolrServer?server?=?getServer(url);??
  583. ????????for(int?j=start;j<>
  584. ????????????try?{??
  585. ????????????????long?startMs?=?System.currentTimeMillis();??
  586. ????????????????hostCreate(list.get(j),server);??
  587. ????????????????long?endMs?=?System.currentTimeMillis();??
  588. ????????????????System.out.println("程序第"+(j+1)+"个目录处理完毕,目录路径:"+list.get(j)+",?耗时"+(endMs-startMs)+"ms");??
  589. ????????????}?catch?(CorruptIndexException?e)?{??
  590. ????????????????e.printStackTrace();??
  591. ????????????}?catch?(IOException?e)?{??
  592. ????????????????e.printStackTrace();??
  593. ????????????}??
  594. ????????}??
  595. ????}??
  596. ????public?void?getFile(File?fileDirectory,List?list){??
  597. ????????if(fileDirectory.isDirectory()){??
  598. ????????????File[]?files?=?fileDirectory.listFiles();??
  599. ????????????for(File?file?:files){??
  600. ????????????getFile(file,list);??
  601. ????????????}??
  602. ????????}else?if(fileDirectory.isFile()){??
  603. ????????????String?filePath?=?fileDirectory.getPath();??
  604. ????????????String?path?=?filePath.replace('\\',?'/');??
  605. ????????????if(path.endsWith(".cfs")){??
  606. ????????????????int?lastIndex?=?path.lastIndexOf("/");??
  607. ????????????????String?directory?=?path.substring(0,lastIndex);??
  608. ????????????????list.add(directory);??
  609. ????????????}??
  610. ????????}??
  611. ????}??
  612. ????@SuppressWarnings("deprecation")??
  613. ????public?void?hostCreate(String?directory,CommonsHttpSolrServer?server)?throws?CorruptIndexException,?IOException{??
  614. ????????IndexReader?reader?=?IndexReader.open(directory);??
  615. ????????List?beans?=?new?ArrayList();??
  616. ????????for(int?i=0;i<>
  617. ????????????Document?doc?=?reader.document(i);??
  618. ????????????BookIndex2?book?=?new?BookIndex2();??
  619. ????????????book.setZjid(doc.getField("zjid").stringValue());??
  620. ????????????book.setAddress(doc.getField("address").stringValue());??
  621. ????????????book.setAuthor(doc.getField("author").stringValue());??
  622. ????????????book.setbookname(doc.getField("bookname").stringValue());??
  623. ????????????book.setDxid(doc.getField("dxid").stringValue());??
  624. ????????????book.setFenlei(doc.getField("fenlei").stringValue());??
  625. ????????????book.setFenleiurl(doc.getField("fenleiurl").stringValue());??
  626. ????????????book.setMulu(doc.getField("mulu").stringValue());??
  627. ????????????book.setPublishDate(doc.getField("publishDate").stringValue());??
  628. ????????????book.setQwpos(doc.getField("qwpos").stringValue());??
  629. ????????????book.setSsid(doc.getField("ssid").stringValue());??
  630. ????????????book.setZhangjie(doc.getField("zhangjie").stringValue());??
  631. ????????????beans.add(book);??
  632. ????????????}??
  633. ????????createIndex(beans,server);??
  634. ????????beans.clear();??
  635. ????????reader.close();??
  636. ????}??
  637. ????public?void?createIndex(List?beans,CommonsHttpSolrServer?server){??
  638. ????????try?{??
  639. ????????????????server.addBeans(beans);??
  640. ????????????????server.commit();??
  641. //??????????????server.optimize();???
  642. ????????}?catch?(SolrServerException?e)?{??
  643. ????????????e.printStackTrace();??
  644. ????????}?catch?(IOException?e)?{??
  645. ????????????e.printStackTrace();??
  646. ????????}??
  647. ????}??
  648. ??????
  649. ????public?static?void?main(String[]?args)?throws?CorruptIndexException,?IOException{??
  650. ????????ReaderIndex?reader?=?new?ReaderIndex();??
  651. ????????String?path?=?"D:\\91";??
  652. ????????String[]?hosts?=?new?String[]?{"http://192.168.169.121:9888/solr","http://192.168.169.121:9088/solr","http://192.168.169.48:9888/solr"};??
  653. ??????????
  654. ????????long?startTime?=?new?Date().getTime();??
  655. ????????reader.indexDocuements(path,hosts);??
  656. ????????long?endTime?=?new?Date().getTime();??
  657. ????????System.out.println("-------------------");??
  658. ????????long?ms?=?(endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;??
  659. ????????System.out.println("全部文档索引完毕,一共用了"+(endTime-startTime)/60000+"分"?+??
  660. ????????????????""+((endTime-startTime)%60000)/1000+"秒"+ms+"毫秒");??
  661. ??????????
  662. ????}??
  663. ??????????
  664. ??????
  665. }??
  666. ??
  667. ??
  668. ??
  669. ??
  670. ??
  671. ??
  672. ??
  673. ??
  674. ??
www.htsjk.Com true http://www.htsjk.com/solr/37737.html NewsArticle solr应用, Solrj已经是很强大的solr客户端了。它本身就包装了httpCliet,以完全对象的方式对solr进行交互。很小很好很强大。 不过在实际使用中,设置SolrQuery 的过程中,为了设置多个搜...
相关文章
    暂无相关文章
评论暂时关闭