compass使用的步骤
打入lucene,paoding分词器,compass,common-log.jar相关的jar包, 可以使用配置或者注释的方式来使用compass. 使用xml配置方式: 01. 02. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 26. 27. 29. 31. 32. 33. 34. 35. 使用注释方式: Compass compass=new CompassAnnotationsConfiguration() .setSetting("compass.engine.analyzer.default.type","net.paoding.analysis.analyzer.PaodingAnalyzer") .setSetting(CompassEnvironment.CONNECTION,"c:/lucene") .setSetting("compass.engine.highlighter.default.formatter.simple.pre", "") .setSetting("compass.engine.highlighter.default.formatter.simple.post", "") .addScan("com.domain").buildCompass(); Message message=new Message(); message.setId(1L); message.setTitle("当你孤单你会想起谁"); message.setContent("当你孤单你会想起谁,你想不想找个人来陪"); message.setCreateTime(new Date()); CompassSession session=compass.openSession(); session.create(message); session.commit(); 相关Message的注解: @Searchable public class Message { @SearchableId private Long id; @SearchableProperty(index=Index.ANALYZED,store=Store.YES) private String title; @SearchableProperty(index=Index.ANALYZED,store=Store.YES) private String content; @SearchableProperty(index=Index.NO,store=Store.YES) private Date createTime; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } } @Test public void test3(){ Compass compass=new CompassConfiguration().configure().buildCompass(); CompassSession session=compass.openSession(); CompassHits hits=session.find("title:孤单"); for(int i=0;i 2. 4. 5. 6. 7. 8. 9. 10. com.core.persistence.po.Article 11. com.core.persistence.po.Author 12. 13. 14. 15. 16. 17. 18. 19. /lucene/indexes 20. 23. 24. org.compass.spring.transaction.SpringSyncTransactionFactory 25. 26. 27. 28. ]]> 29. 30. 32. ]]> 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 47. 48. hibernateDevice 49. 50. 51. 52. true 53. 54. 55. 56. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75.public class CompassIndexBuilder implements InitializingBean { 76. 77. private static final Logger log = Logger.getLogger(CompassIndexBuilder.class); 78. 79. // 是否需要建立索引,可被设置为false使本Builder失效. 80. private boolean buildIndex = true; 81. 82. // 索引操作线程延时启动的时间,单位为秒 83. private int lazyTime = 10; 84. 85. // Compass封装 86. private CompassGps compassGps; 87. 88. // 索引线程 89. private Thread indexThread = new Thread() { 90. 91. @Override 92. public void run() { 93. try { 94. Thread.sleep(lazyTime * 1000); 95. 96. log.info("begin compass index..."); 97. long beginTime = System.currentTimeMillis(); 98. // 重建索引. 99. // 如果compass实体中定义的索引文件已存在,索引过程中会建立临时索引, 100. // 索引完成后再进行覆盖. 101. compassGps.index(); 102. long costTime = System.currentTimeMillis() - beginTime; 103. log.info("compss index finished."); 104. log.info("costed " + costTime + " milliseconds"); 105. } catch (InterruptedException e) { 106. } 107. } 108. }; 109. 110. /* 111. * 实现InitializingBean接口,在完成注入后调用启动索引线程. 112. * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 113. */ 114. public void afterPropertiesSet() throws Exception { 115. if (buildIndex) { 116. indexThread.setDaemon(true); 117. indexThread.setName("Compass Indexer"); 118. indexThread.start(); 119. } 120. } 121. 122. public void setBuildIndex(boolean buildIndex) { 123. this.buildIndex = buildIndex; 124. } 125. 126. public void setLazyTime(int lazyTime) { 127. this.lazyTime = lazyTime; 128. } 129. 130. public void setCompassGps(CompassGps compassGps) { 131. this.compassGps = compassGps; 132. } 133.} 1.Compass compass = compassTemplate.getCompass(); 2.CompassSession session=compass.openSession(); 3.List list = new ArrayList(); 4.CompassHits hits= session.queryBuilder().queryString(queryString).toQuery().hits(); 5.for(int i=0;i*