您的位置:首页 > 其它

lucene学习笔记1--索引创建

2012-10-13 16:11 375 查看
创建索引代码:

public void createIndex(String indexPath, String dataDir) throws IOException

{

//获取数据源文件列表

File[] files = new File(dataDir).listFiles();

//创建索引目錄

Directory directory = FSDirectory.open(new File(indexPath));

//創建分詞器

// Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

Analyzer analyzer = new PaodingAnalyzer();

//创建索引写入类

IndexWriter indexWriter = new IndexWriter(directory, analyzer, true, MaxFieldLength.LIMITED);

for (File file : files)

{

String content = FileOperation.readContents(file, "UTF-8");

String[] records = content.split("\r\n");

for (String record : records)

{

//创建Document对象

Document document = new Document();

//创建域

Field contentField = new Field("content", record, Store.YES, Index.ANALYZED);

Field nameField = new Field("filename", file.getName(), Store.YES, Index.ANALYZED);

document.add(nameField);

document.add(contentField);

indexWriter.addDocument(document);

}

}

indexWriter.optimize();

//查看有多少个索引

System.out.println("numDocs:" + indexWriter.numDocs());

indexWriter.close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: