首页 > 代码库 > Lucene系列:(3)LuceneUtils之CRUD
Lucene系列:(3)LuceneUtils之CRUD
第二版 |
LuceneUtils.java package com.rk.lucene.utils; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; 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; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter.MaxFieldLength; 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.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; private static final String LUCENE_DIRECTORY= "D:/rk/indexDB"; static{ try { directory = FSDirectory.open(new File(LUCENE_DIRECTORY)); version = Version.LUCENE_30; analyzer = new StandardAnalyzer(version); maxFieldLength = MaxFieldLength.LIMITED; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //不让外部new当前帮助类的对象 private LuceneUtils(){} public static <T> void add(T t) throws Exception{ Document document = javabean2document(t); IndexWriter indexWriter = new IndexWriter(getDirectory(), getAnalyzer(), getMaxFieldLength()); indexWriter.addDocument(document); indexWriter.close(); } public static <T> void addAll(List<T> list) throws Exception{ IndexWriter indexWriter = new IndexWriter(getDirectory(), getAnalyzer(), getMaxFieldLength()); for(T t : list){ Document doc = javabean2document(t); indexWriter.addDocument(doc); } indexWriter.close(); } public static <T> void update(String field,String value,T t) throws Exception{ Document document = javabean2document(t); IndexWriter indexWriter = new IndexWriter(getDirectory(), getAnalyzer(), getMaxFieldLength()); indexWriter.updateDocument(new Term(field,value), document); indexWriter.close(); } public static <T> void delete(String field,String value) throws Exception{ IndexWriter indexWriter = new IndexWriter(getDirectory(), getAnalyzer(), getMaxFieldLength()); indexWriter.deleteDocuments(new Term(field,value)); indexWriter.close(); } public static void deleteAll() throws Exception { IndexWriter indexWriter = new IndexWriter(getDirectory(), getAnalyzer(), getMaxFieldLength()); indexWriter.deleteAll(); indexWriter.close(); } public static <T> List<T> search(String field,String keyword,int topN) throws Exception{ List<T> list = new ArrayList<T>(); QueryParser queryParser = new QueryParser(getVersion(), field, getAnalyzer()); Query query = queryParser.parse(keyword); IndexSearcher indexSearcher = new IndexSearcher(getDirectory()); TopDocs topDocs = indexSearcher.search(query, topN); for(int i=0;i<topDocs.scoreDocs.length;i++){ ScoreDoc scoreDoc = topDocs.scoreDocs[i]; int docIndex = scoreDoc.doc; System.out.println("文档索引号" + docIndex + ",文档得分:" + scoreDoc.score); Document document = indexSearcher.doc(docIndex); T entity = (T) document2javabean(document, Article.class); list.add(entity); } indexSearcher.close(); return list; } public static <T> void printList(List<T> list){ if(list != null && list.size()>0){ for(T t : list){ System.out.println(t); } } } //将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); //获取字段名 String name = reflectField.getName(); //获取字段值 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 <T> T document2javabean(Document document,Class<T> clazz) throws Exception{ T obj = clazz.newInstance(); java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields(); for(java.lang.reflect.Field reflectField : reflectFields){ reflectField.setAccessible(true); String name = reflectField.getName(); 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); } } ArticleDao2.java package com.rk.lucene.crud; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.rk.lucene.entity.Article; import com.rk.lucene.utils.LuceneUtils; public class ArticleDao2 { @Test public void testAdd() 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年代中期苏联学者诺维柯夫和尼曼又各自提出了白洞理论")); LuceneUtils.addAll(articleList); } @Test public void testSearch() throws Exception{ List<Article> list = LuceneUtils.search("content", "地球", 10); LuceneUtils.printList(list); } @Test public void testUpdate() throws Exception{ Article article = new Article(3, "你好", "欢迎来到我的地球"); LuceneUtils.update("id","3",article); } @Test public void testDelete() throws Exception{ LuceneUtils.delete("id","3"); } } |
第一版 |
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); //获取字段名 String name = reflectField.getName(); //获取字段值 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 <T> T document2javabean(Document document,Class<T> clazz) throws Exception{ T obj = clazz.newInstance(); java.lang.reflect.Field[] reflectFields = clazz.getDeclaredFields(); for(java.lang.reflect.Field reflectField : reflectFields){ reflectField.setAccessible(true); String name = reflectField.getName(); 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); } } ArticleDao.java package com.rk.lucene.crud; import java.util.ArrayList; import java.util.List; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; 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 ArticleDao { public void add(Article article) throws Exception{ Document document = LuceneUtils.javabean2document(article); IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); indexWriter.addDocument(document); indexWriter.close(); } public void addAll(List<Article> articleList) throws Exception{ IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); for(Article article : articleList){ Document doc = LuceneUtils.javabean2document(article); indexWriter.addDocument(doc); } indexWriter.close(); } public void update(Article article) throws Exception{ Document document = LuceneUtils.javabean2document(article); IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); indexWriter.updateDocument(new Term("id",document.get("id")), document); indexWriter.close(); } public void delete(String field,String txt) throws Exception{ IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); indexWriter.deleteDocuments(new Term(field,txt)); indexWriter.close(); } public void deleteAll() throws Exception { IndexWriter indexWriter = new IndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength()); indexWriter.deleteAll(); indexWriter.close(); } public <T> List<T> search(String field,String keyword,int topN) throws Exception{ List<T> list = new ArrayList<T>(); QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(), field, LuceneUtils.getAnalyzer()); Query query = queryParser.parse(keyword); IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.getDirectory()); TopDocs topDocs = indexSearcher.search(query, topN); for(int i=0;i<topDocs.scoreDocs.length;i++){ ScoreDoc scoreDoc = topDocs.scoreDocs[i]; System.out.println(scoreDoc.score); int docIndex = scoreDoc.doc; Document document = indexSearcher.doc(docIndex); T entity = (T) LuceneUtils.document2javabean(document, Article.class); list.add(entity); } indexSearcher.close(); return list; } public <T> void print(List<T> list){ if(list != null && list.size()>0){ for(T t : list){ System.out.println(t); } } } @Test public void testAdd() 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年代中期苏联学者诺维柯夫和尼曼又各自提出了白洞理论")); addAll(articleList); } @Test public void testSearch() throws Exception{ List<Article> list = search("content", "地球", 10); print(list); } @Test public void testUpdate() throws Exception{ Article article = new Article(3, "你好", "欢迎来到我的地球"); update(article); } @Test public void testDelete() throws Exception{ //delete("id", "1"); delete("id","3"); } } |
Lucene系列:(3)LuceneUtils之CRUD
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。