lucene源码分析---1
来源:互联网 发布:我想做淘宝客怎么弄 编辑:程序博客网 时间:2024/04/29 09:13
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背后的原理。
0 0
- lucene源码分析---1
- lucene索引源码分析1
- Lucene源码分析-- Analyzer
- lucene 源码分析
- lucene源码分析---2
- lucene源码分析---3
- lucene源码分析---4
- lucene源码分析---5
- lucene源码分析---6
- lucene源码分析---7
- lucene源码分析---8
- lucene源码分析---10
- lucene源码分析---9
- lucene源码分析---11
- lucene源码分析---12
- lucene源码分析---13
- lucene源码分析---15
- lucene的索引源码分析
- android udp文件断点续传
- linux快速安装rabbitMQ
- linux Makefile
- 大数据对象
- (C)理解 #define write(b,addr) (void)((*(volatile unsigned int *) (addr)) = (b))
- lucene源码分析---1
- javascript浏览器对象(window对象)——Screen对象
- hdu 1075 What Are You Talking About STL的使用
- vim速查手册(随时更新)
- CSS层叠样式表(Cascading Style Sheets)
- 2016年下半年
- ViewPager+RadioGroup实现微信UI界面
- java list倒序输出及复制list集合
- 【机器学习】常见算法分类汇总