Lucene搭建和基本使用
来源:互联网 发布:淘宝直播怎么抢优惠券 编辑:程序博客网 时间:2024/06/06 00:33
1.下载及安装
地址:http://lucene.apache.org/ 下载src.tgz包和lucene.xxx.zip
2.查看文档构建eclipse工程
2.1 ucene-4.6.0\docs\index.html 参考文档。点击lucene domo根据导航setting yourclasspath 。
2.2 然后设置build path,把src包关联。打开demo
2.3 打开indexFiles,SearchFiles 的源码,参考源码自己写出例子程序。
大概脉络是:indeFiles中构建indexWriter(需要directory和IndexWriterConfig),然后把需要相应的Filed写进document中,indexWriter.Add(doc)写进文件最后关掉。
SearchFiles的使用也是很简单。利用indexRead.search(query,num),然后遍历document。
以下是快速使用的源码:
public classLuceneDemo { public static voidindexFiles(String indexPath){ File indexFile = newFile(indexPath); try { Directory directory = FSDirectory.open(indexFile); Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46); AnalyzerikAnalyzer = new IKAnalyzer(true);// IndexWriterConfig config = newIndexWriterConfig(Version.LUCENE_46,ikAnalyzer); IndexWriter indexWriter = newIndexWriter(directory, config); //FileInputStreamfis = new FileInputStream(indexFile); Document doc = newDocument(); //Fieldfield = new StringField("path", "this is a test",Field.Store.YES); Field field = newLongField("id", Long.valueOf(1), Field.Store.YES); Field field2 = newTextField("title", "圣诞节日记", Field.Store.YES); Field field3 = newTextField("content", "今天圣诞节过得很开心!因为我收到了圣诞礼物。哈哈!",Field.Store.YES); /* Fieldfield = new LongField("id", Long.valueOf(2), Field.Store.YES); Field field2 = newTextField("title", "平安夜",Field.Store.YES); Field field3 = newTextField("content", "今天平安夜过得很开心!因为我收到了圣诞礼物。哈哈!", Field.Store.YES); */ /*Fieldfield = new LongField("id", Long.valueOf(3), Field.Store.YES); Field field2 = newTextField("title", "圣诞节的礼物",Field.Store.YES); Field field3 = newTextField("content", "今天早上突然发现圣诞节礼物在我的袜子里。",Field.Store.YES); */ doc.add(field); doc.add(field2); doc.add(field3); indexWriter.addDocument(doc); indexWriter.commit(); indexWriter.close(); } catch(Exception e) { e.printStackTrace(); } } public static voidsearchFiles(String indexPath){ File indexFile = newFile(indexPath); try { IndexReader indexReader =DirectoryReader.open(FSDirectory.open(indexFile)); IndexSearcher indexSearcher = newIndexSearcher(indexReader); Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46); //QueryParserparser = new QueryParser(Version.LUCENE_40, "path", analyzer); TermQuery query = newTermQuery(new Term("content","圣诞礼物")); TopDocs topDocs = indexSearcher.search(query,10); ScoreDoc[] scoreDocs = topDocs.scoreDocs; int hits =topDocs.totalHits; System.out.println(hits); for(ScoreDocsd : scoreDocs){ int docID =sd.doc; Document document =indexSearcher.doc(docID); System.out.println(document.get("id")); System.out.println(document.get("title")); System.out.println(document.get("content")); } } catch(Exception e) { // TODOAuto-generated catch block e.printStackTrace(); } } public static voiddelIndexFiles(String indexPath){ File indexFile = newFile(indexPath); try { Directory directory = FSDirectory.open(indexFile); Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_46); Analyzer ikAnalyzer = newIKAnalyzer(true);// IndexWriterConfig config = newIndexWriterConfig(Version.LUCENE_46,ikAnalyzer); IndexWriter indexWriter = newIndexWriter(directory, config); //indexWriter.deleteAll(); indexWriter.deleteDocuments(new Term("content","圣诞礼物")); indexWriter.commit(); indexWriter.close(); } catch(Exception e) { e.printStackTrace(); } }}
2.4 然后在测试程序中输入路径测试程序
@Testpublic void test1(){ String indexPath = "F:/lucene/myretrieve/luceneData"; LuceneDemo.indexFiles(indexPath); //LuceneDemo.searchFiles(indexPath);}@Testpublic void test2(){ String indexPath = "F:/lucene/myretrieve/luceneData"; //LuceneDemo.indexFiles(indexPath); LuceneDemo.searchFiles(indexPath);}
0 0
- Lucene搭建和基本使用
- 【Lucene学习笔记】基本使用和认识
- Lucene基本使用介绍
- lucene 基本使用
- lucene 基本使用
- Lucene基本使用介绍
- lucene基本使用
- lucene的基本使用
- lucene的基本使用
- Git服务器的搭建和基本使用
- Mongo数据库搭建和基本使用
- Lucene入门和使用
- lucene-使用QueryParser和IndexSearcher
- Lucene的使用和优化
- Android NDK的开发环境搭建和基本使用
- Android 开发环境搭建和基本的使用
- GCC/GTK环境搭建和一些基本控件的使用
- Android Studio下NDK环境的搭建和基本使用
- lua中实现倒计时
- nginx+tomcat取得远程用户IP问题的解决办法
- Using python-keystoneclient.v3 API
- 光源旋转照射游戏角色
- 对equals和hashcode值的理解
- Lucene搭建和基本使用
- VS 2008 打开SDF数据库文件
- 正则表达式的学习
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- JS 数组去重
- 面向对象01-java基础
- Android Volley完全解析(二),使用Volley加载网络图片
- >/dev/null 2>&1
- 常被忽略的小技巧