lucene源码分析---1
2016-07-11 11:05
399 查看
lucene源码分析—实例
本章开始分析lucene的源码,版本为目前最新的6.1.0,下面先看一段常见的lucene建立索引和进行搜索的实例,建立索引实例:
String filePath = ...//文件路径 String indexPath = ...//索引路径 File fileDir = new File(filePath); Directory dir = FSDirectory.open(Paths.get(indexPath)); Analyzer luceneAnalyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(luceneAnalyzer); iwc.setOpenMode(OpenMode.CREATE); IndexWriter indexWriter = new IndexWriter(dir,iwc); File[] textFiles = fileDir.listFiles(); for (int i = 0; i < textFiles.length; i++) { if (textFiles[i].isFile()) { String temp = FileReaderAll(textFiles[i].getCanonicalPath(), "GBK"); Document document = new Document(); Field FieldPath = new StringField("path", textFiles[i].getPath(), Field.Store.YES); Field FieldBody = new TextField("body", temp, Field.Store.YES); document.add(FieldPath); document.add(FieldBody); indexWriter.addDocument(document); } } indexWriter.close();
其中,FileReaderAll函数用来从文件中读取字符串。
搜索实例:
String indexPath=...//索引路径 IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath))); IndexSearcher searcher=new IndexSearcher(reader); ScoreDoc[] hits=null; String queryString=...//关键字符串 Query query=null; Analyzer analyzer= new StandardAnalyzer(); try { QueryParser qp=new QueryParser("body",analyzer); query=qp.parse(queryString); } catch (ParseException e) { } if (searcher!=null) { TopDocs results=searcher.search(query, 10); hits=results.scoreDocs; Document document=null; for (int i = 0; i < hits.length; i++) { document=searcher.doc(hits[i].doc); String body=document.get("body"); String path=document.get("path"); String modifiedtime=document.get("modifiField"); } reader.close(); }
后面的章节就会开始分析这两个实例究竟做了哪些工作,以及探究lucene背后的原理。
相关文章推荐
- 自定义Cell中按钮的点击事件
- Shell脚本中计算字符串长度的5种方法
- RTP包里面得到H.264数据和AAC数据的方法
- Core Data 学习:轻量级版本迁移(Lightweight Migration)
- Matlab学习(三)
- Hadoop & HBase 故障排除
- 性能测试中用LambdaProbe监控Tomcat
- 浅谈Java设计模式(二十)状态模式(State)
- JavaScript 由左向右的图片渐变过渡
- JavaScript动态控制图片透明度的变化
- .NET跨平台之旅:在生产环境中上线第一个运行于Linux上的ASP.NET Core站点
- iOS: xcode打包上传iTunes失败,iTunes Store operation failed,this action can not complete .try again
- 关于多线程
- 将秒数转换成天具体的天时分秒
- PHP 处理2038后的日期
- fragment切换
- HDU 1285 比赛排名(拓扑排序)
- Android SDK开发包国内下载地址
- DIV + CSS3 和 html5 + CSS3的区别
- 数据分析的一些思考