
课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
昆明IT培训的老师知道Lucene,最新版是Lucene6.2.1,匹配的jdk版本是1.8正式版。
这里用jdk7最后一版,所以用Lucene5.3.3。
新建一个maven项目,如果不会可以参考前面的博文,前面的博文有专门提到如何新建maven项目。
新建的maven项目:<packaging>jar</packaging>,
复制代码
1<dependencies>2<!-- #/artifact/org.apache.lucene/lucene-core -->3<dependency>4<groupId>org.apache.lucene</groupId>5<artifactId>lucene-core</artifactId>6<version>5.5.3</version>7</dependency>8<!-- #/artifact/org.apache.lucene/lucene-queryparser -->9<dependency>10<groupId>org.apache.lucene</groupId>11<artifactId>lucene-queryparser</artifactId>12<version>5.5.3</version>13</dependency>14<!-- #/artifact/org.apache.lucene/lucene-analyzers-common -->15<dependency>16<groupId>org.apache.lucene</groupId>17<artifactId>lucene-analyzers-common</artifactId>18<version>5.5.3</version>19</dependency>20</dependencies>
复制代码
因为我用jdk7,不喜欢每次更新maven仓库都要手动调整项目的jdk版本,所以
复制代码
1<!--源码目录,插件管理等配置-->2<build>3<finalName>Lucene</finalName>4<plugins>5<plugin>6<groupId>org.apache.maven.plugins</groupId>7<artifactId>maven-compiler-plugin</artifactId>8<version>3.3</version>9<configuration>10<!--指定source和target的版本-->11<!-- source指定用哪个版本的编译器对java源码进行编译-->12<source>1.7</source>13<!-- target指定生成的class文件将保证和哪个版本的虚拟机进行兼容-->14<target>1.7</target>15</configuration>16</plugin>17</plugins>18</build>
复制代码
可以这样。
新建两个类:
Indexer
复制代码
importjava.io.File;importjava.io.FileReader;importjava.nio.file.Paths;importorg.apache.lucene.analysis.Analyzer;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.document.Document;importorg.apache.lucene.document.Field;importorg.apache.lucene.document.TextField;importorg.apache.lucene.index.IndexWriter;importorg.apache.lucene.index.IndexWriterConfig;importorg.apache.lucene.store.Directory;importorg.apache.lucene.store.FSDirectory;publicclassIndexer {privateIndexWriter writer;//写索引实例/***构造方法实例化IndexWriter * *@paramindexDir *@throwsException*/publicIndexer(String indexDir)throwsException { Directory dir=FSDirectory.open(Paths.get(indexDir)); Analyzer analyzer=newStandardAnalyzer();//标准分词器IndexWriterConfig iwc =newIndexWriterConfig(analyzer); writer=newIndexWriter(dir, iwc); }/***关闭写索引* *@throwsException*/publicvoidclose()throwsException { writer.close(); }/***索引指定目录的所有文件* *@paramdataDir *@throwsException*/publicintindex(String dataDir)throwsException { File[] files=newFile(dataDir).listFiles();for(File f : files) { indexFile(f); }returnwriter.numDocs(); }/***索引指定文件* *@paramf*/privatevoidindexFile(File f)throwsException {//TODO Auto-generated method stubSystem.out.println("索引文件:" +f.getCanonicalFile()); Document doc=getDocument(f); writer.addDocument(doc); }/***获取文档,文档里在设置每个字段* *@paramf *@return*@throwsException*/privateDocument getDocument(File f)throwsException {//TODO Auto-generated method stubDocument doc =newDocument(); doc.add(newTextField("contents",newFileReader(f))); doc.add(newTextField("fileName", f.getName(), Field.Store.YES)); doc.add(newTextField("fullPath", f.getCanonicalPath(), Field.Store.YES));returndoc; }publicstaticvoidmain(String[] args){ String indexDir="E:\\lucene"; String dataDir="E:\\lucene\\data"; Indexer indexer=null;intnumIndexed=0;longstart=System.currentTimeMillis();try{ indexer=newIndexer(indexDir); numIndexed=indexer.index(dataDir); }catch(Exception e) {//TODO Auto-generated catch blocke.printStackTrace(); }finally{try{ indexer.close(); }catch(Exception e) {//TODO Auto-generated catch blocke.printStackTrace(); } }longend=System.currentTimeMillis(); System.out.println("索引:"+numIndexed+"个文件,花费了"+(end-start)+"毫秒"); } }
复制代码
String indexDir="E:\\lucene"; String dataDir="E:\\lucene\\data";看到这里不要好奇,盘符随意,在任意盘符根目录下新建文件夹,最好英文无空格,中文未测试,然后拷贝几个txt文件到data文件夹下面,一会测试用的到。 然后运行这个类,可以看到 然后可以在lucene文件夹下看到这几个奇怪的文件,是什么后面会提到,稍安勿躁。
新建另一个类:
Searcher
复制代码
1importjava.nio.file.Paths;23importorg.apache.lucene.analysis.Analyzer;4importorg.apache.lucene.analysis.standard.StandardAnalyzer;5importorg.apache.lucene.document.Document;6importorg.apache.lucene.index.DirectoryReader;7importorg.apache.lucene.index.IndexReader;8importorg.apache.lucene.queryparser.classic.QueryParser;9importorg.apache.lucene.search.IndexSearcher;10importorg.apache.lucene.search.Query;11importorg.apache.lucene.search.ScoreDoc;12importorg.apache.lucene.search.TopDocs;13importorg.apache.lucene.store.Directory;14importorg.apache.lucene.store.FSDirectory;1516publicclassSearcher {17publicstaticvoidsearch(String indexDir, String q)throwsException {18Directory dir =FSDirectory.open(Paths.get(indexDir));19IndexReader reader =DirectoryReader.open(dir);20IndexSearcher is =newIndexSearcher(reader);21Analyzer analyzer =newStandardAnalyzer();22QueryParser parse =newQueryParser("contents", analyzer);23Query query =parse.parse(q);24longstart =System.currentTimeMillis();25TopDocs hits = is.search(query, 10);26longend =System.currentTimeMillis();27System.out.println("匹配" + q + ",总共花费" + (end - start) + "毫秒," + "查询到" + hits.totalHits + "个记录");28for(ScoreDoc scoreDoc : hits.scoreDocs) {29Document doc =is.doc(scoreDoc.doc);30System.out.println(doc.get("fullPath"));31}32reader.close();33}3435publicstaticvoidmain(String[] args) {36String indexDir = "E:\\lucene";37//String q = "LICENSE-2.0";38String q = "Zygmunt Saloni";39try{40search(indexDir, q);41}catch(Exception e) {42//TODO Auto-generated catch block43e.printStackTrace();44}45}46}
复制代码
运行这个类,
不要把第一个类生成的几个特殊的文件删除,任性的话,试试看,会报错,如果删除运行第一个类生成的几个特殊的奇怪文件后再运行第二个类的时候会报错。
还是任性的试试看吧。
对比String q = "Zygmunt Saloni";事实证明没什么影响,因为分词了,整体切割。
加上-运行第二个类的话,结果一样,自己试试看。