package index;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class BuildIndex {
private Analyzer analyzer = new IKAnalyzer();
private FSDirectory fsd = null;
public void createIndex(String path, String pathWrite) throws IOException {
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_36, analyzer);
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
fsd = FSDirectory.open(new File(pathWrite));
IndexWriter indexWriter = new IndexWriter(fsd, indexWriterConfig);
File f = new File(path);
File[] fs = f.listFiles();
for (int i = 0; i < fs.length; i++) {
Document doc = File2DocumentUtils.fileToDocument(fs[i]);
indexWriter.addDocument(doc);
}
indexWriter.close();
}
// /////////測試索引
public List<String[]> search(String text, String pathWrite)
throws ParseException, CorruptIndexException, IOException {
List<String[]> list = new ArrayList<String[]>();
String[] fields = { "title", "content" };
Map<String, Float> map = new HashMap<String, Float>();
map.put("title", 1.0f);
map.put("content", 1.0f);
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36,
fields, analyzer, map);
Query query = queryParser.parse(text);
fsd = FSDirectory.open(new File(pathWrite));
IndexReader indexReader = IndexReader.open(fsd);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TopDocs topDocs = indexSearcher.search(query, 100000);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docSn = scoreDoc.doc;
Document document = indexSearcher.doc(docSn);
String title = document.get("title");
String content = document.get("content");
list.add(new String[] { title, content });
}
return list;
}
// ////////////// 測試
public static void main(String[] args) {
try {
List<String[]> list = new BuildIndex().search("李良",
"E:\\apache-tomcat-6.0.29\\webapps\\Test\\index");
for (String[] strings : list) {
for (String string : strings) {
System.out.println(string);
}
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}