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();
}
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();
}
相关文章推荐
- lucene学习笔记之索引创建、内容检索、分类器实现
- lucene3.5学习笔记02--创建索引和建立搜索
- Lucene学习笔记(1)-索引创建和简单的查询
- 学习Lucene笔记一:创建索引
- lucene学习笔记1-创建索引
- lucene学习笔记3-索引-创建域选项
- mysql 索引的创建与优化 学习笔记
- Lucene学习-创建索引、关键词查询
- 一步一步跟我学习lucene(6)---lucene索引优化之多线程创建索引
- 一步一步跟我学习lucene(1)---lucene的IndexWriter对象创建和索引策略的选择
- 01_java Lucene学习——创建索引
- lucene学习-创建索引
- lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)
- lucene学习笔记之构建索引
- Lucene6.0学习笔记——建立索引
- Elasticsearch 学习笔记 索引创建、插入、查询、修改、删除
- lucene学习--创建索引与搜索
- lucene学习之创建索引的方式
- lucene学习笔记:三,Lucene的索引文件格式
- MySQL 5.6学习笔记(索引的创建与删除)