需要加入BeanUtils的相关jar包 commons-beanutils-1.9.2.jar commons-collections-3.2.1.jar commons-logging-1.1.1.jar |
LuceneUtils.java
package com.rk.lucene.utils; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import org.apache.commons.beanutils.BeanUtils; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import com.rk.lucene.entity.Article; public class LuceneUtils { private static Directory directory; private static Version version; private static Analyzer analyzer; private static MaxFieldLength maxFieldLength; static{ try { directory = FSDirectory.open(new File("D:/rk/indexDB")); version = Version.LUCENE_30; analyzer = new StandardAnalyzer(version); maxFieldLength = MaxFieldLength.LIMITED; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //不让外部new当前帮助类的对象 private LuceneUtils(){} //将JavaBean转成Document对象 public static Document javabean2document(Object obj) throws Exception{ //创建Document对象 Document document = new Document(); //获取obj引用的对象字节码 Class clazz = obj.getClass(); //通过对象字节码获取私有的属性 java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields(); //迭代 for(java.lang.reflect.Field reflectField : reflectFields){ //反射 reflectField.setAccessible(true); //获取字段名,例如id/title/content String name = reflectField.getName(); //手工拼接方法名 //String methodName = "get" + name.substring(0, 1).toUpperCase() +name.substring(1); //获取方法,例如getId()/getTitle()/getContent() //Method method = clazz.getMethod(methodName, null); //执行方法 //String value = method.invoke(obj, null).toString(); String value = reflectField.get(obj).toString(); //加入到Document对象中去,这时javabean的属性与document对象的属性相同 document.add(new Field(name, value, Store.YES, Index.ANALYZED)); } //返回document对象 return document; } //将Document对象转换成JavaBean对象 public static Object document2javabean(Document document,Class clazz) throws Exception{ Object obj = clazz.newInstance(); java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields(); for(java.lang.reflect.Field reflectField : reflectFields){ reflectField.setAccessible(true); String name = reflectField.getName();//id/title/content String value = document.get(name); BeanUtils.setProperty(obj, name, value); } return obj; } public static Directory getDirectory() { return directory; } public static void setDirectory(Directory directory) { LuceneUtils.directory = directory; } public static Version getVersion() { return version; } public static void setVersion(Version version) { LuceneUtils.version = version; } public static Analyzer getAnalyzer() { return analyzer; } public static void setAnalyzer(Analyzer analyzer) { LuceneUtils.analyzer = analyzer; } public static MaxFieldLength getMaxFieldLength() { return maxFieldLength; } public static void setMaxFieldLength(MaxFieldLength maxFieldLength) { LuceneUtils.maxFieldLength = maxFieldLength; } //测试 public static void main(String[] args) throws Exception { Article article = new Article(1, "你好", "欢迎来到我的世界"); Document document = javabean2document(article); Article a2 = (Article) document2javabean(document, Article.class); System.out.println(a2); } }
SecondLucene.java
package com.rk.lucene.secondapp; import java.util.ArrayList; import java.util.List; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.junit.Test; import com.rk.lucene.entity.Article; import com.rk.lucene.utils.LuceneUtils; public class SecondLucene { public void createIndexDB(Article article) throws Exception{ Document document = LuceneUtils.javabean2document(article); IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); indexWriter.addDocument(document); indexWriter.close(); } @Test public void makeData() throws Exception{ List<Article> articleList = new ArrayList<Article>(); articleList.add(new Article(1, "卫星地图", "从卫星上鸟瞰地球,感受前所未有的视觉冲击。")); articleList.add(new Article(2, "未来地球", "2025年的未来地球将面临资源耗尽的难题")); articleList.add(new Article(3, "谷歌地球", "Google Earth(谷歌地球)是一款Google公司开发的虚拟地球仪软件")); articleList.add(new Article(4, "十大科学发现", "2016年十大科学发现 发现另一个地球。")); articleList.add(new Article(5, "纪录片BBC", "电视系列片"行星地球"(2006)的电影版本,拍摄了几个动物家庭的迁徙路线。")); articleList.add(new Article(6, "神秘的地球", "神秘的地球 神秘的地球网站内容涉及宇宙奥秘、航空航天、考古发现")); articleList.add(new Article(7, "科学网", "俄专家确认去年5月探测神秘无线电信号来自地球")); articleList.add(new Article(8, "地球公转", "地球公转就是地球按一定轨道围绕太阳转动")); articleList.add(new Article(9, "黑洞", "人类在宇宙中已经辨识出了许多黑洞,既有恒星级黑洞,也有超大质量黑洞。")); articleList.add(new Article(10, "白洞是如何形成的", "20世纪60年代中期苏联学者诺维柯夫和尼曼又各自提出了白洞理论")); for(Article article : articleList){ createIndexDB(article); } } public void findIndexDB(String keyword) throws Exception{ QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(), "content", LuceneUtils.getAnalyzer()); Query query = queryParser.parse(keyword); IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.getDirectory()); TopDocs topDocs = indexSearcher.search(query, 10); List<Article> articleList = new ArrayList<Article>(); for(int i=0;i<topDocs.scoreDocs.length;i++){ ScoreDoc scoreDoc = topDocs.scoreDocs[i]; int no = scoreDoc.doc; Document document = indexSearcher.doc(no); Article article = (Article) LuceneUtils.document2javabean(document, Article.class); articleList.add(article); } for(Article a : articleList){ System.out.println(a); } } @Test public void search() throws Exception{ findIndexDB("地球"); } }