lucene5.5 使用了java的NIO2.0,以前创建directory使用file换成了使用path,获取path的简单方法file.toPath(),上代码
package com.liu.lucene.pro;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
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.LongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class LuceneIndex {
/**
* 默认使用StandardAnalyzer分析器
* @param clear 是否清空所有的索引
*/
public void index(boolean clear){
this.index(clear, new StandardAnalyzer());
}
/**
* 根据analyzer创建索引
* @param clear
* @param analyzer
*/
public void index(boolean clear,Analyzer analyzer){
try {
//1 创建directory
//Path path = FileSystems.getDefault().getPath("E:\\luceneDir");
Path path = new File("E:\\luceneDir").toPath();
Directory directory = FSDirectory.open(path);
//2 创建indexWriter
IndexWriter writer = new IndexWriter(directory,new IndexWriterConfig(analyzer));
if(clear){
writer.deleteAll();
}
//3创建document并且写入filed
Document doc = null;
File file = new File("D:\\lhl\\developSoft\\apache-tomcat-7.0.62-windows-x64\\apache-tomcat-7.0.62\\logs");
for(File f:file.listFiles()){
doc = new Document();
Reader reader = new FileReader(f);
doc.add(new TextField("content", reader));
doc.add(new TextField("name",f.getName(),Field.Store.YES));
doc.add(new TextField("path",f.getPath(),Field.Store.YES));
doc.add(new LongField("length",f.length(),Field.Store.YES));
doc.add(new LongField("date",f.lastModified(),Field.Store.YES));
//4向index写入document
writer.addDocument(doc);
}
writer.close();
directory.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用默认的标准分词器检索
* @param queStr
*/
public void search(String queStr){
this.search(queStr, new StandardAnalyzer());
}
/**
* 使用指定的分词器
* @param queStr
* @param analyzer
*/
public void search(String queStr,Analyzer analyzer){
try {
//1 创建directory
Path path = new File("E:\\luceneDir").toPath();
Directory directory = FSDirectory.open(path);
//2 创建indexReader
DirectoryReader ireader = DirectoryReader.open(directory);
//3创建searcher
IndexSearcher isearcher = new IndexSearcher(ireader);
//4创建query
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse(queStr);
ScoreDoc[] hits = isearcher.search(query, 100).scoreDocs;
for(ScoreDoc doc:hits){
Document document = isearcher.doc(doc.doc);
System.out.println(document.get("path")+"-->"+document.get("name") +"======>"+document.get("date"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
测试使用junit代码:
package com.liu.lucene.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import org.junit.Before;
import org.junit.Test;
import com.liu.lucene.pro.AnalyzerUtils;
import com.liu.lucene.pro.LuceneIndex;
import com.liu.lucene.pro.SameWordAnalyzer;
import com.liu.lucene.pro.SameWordEngineImpl;
public class TestUnit {
LuceneIndex index = null;
@Before
public void setUp(){
index = new LuceneIndex();
}
@Test
public void testIndex(){
index.index(true);
}
@Test
public void testIndexAnalyzer(){
index.index(true,new SameWordAnalyzer(new SameWordEngineImpl()));
}
@Test
public void testSearch(){
index.search("20一5",new SameWordAnalyzer(new SameWordEngineImpl()));
}
@Test
public void testDisplayTokens(){
try {
Reader reader = new FileReader("D:\\lhl\\developSoft\\apache-tomcat-7.0.62-windows-x64\\apache-tomcat-7.0.62\\logs\\loginfo.log.2015-11-27.log");
AnalyzerUtils.displayTokens(new SameWordAnalyzer(new SameWordEngineImpl()), reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}