Lucene学习-创建索引(一)
2010-05-09 20:26
337 查看
Lucene创建索引的过程如下:
1) 建立索引器IndexWriter
2)建立文档对象Document
3)建立信息字段对象Field
4)将Field添加到Document中
5)将Document添加到IndexWriter中
6)关闭索引器IndexWriter
创建Field:Field field=new Field(Field名称,Field内容,存储方式,索引方式);
创建Document:Document doc=new Document();
将Field添加到Document中:doc.add(field)
创建IndexWriter:IndexWriter writer=new IndexWriter(存储索引的路径,分析器的实例);
将Docment添加到IndesWriter中:writer.addDocument(doc);
关闭索引器:writer.close();
创建索引实例:
1) 建立索引器IndexWriter
2)建立文档对象Document
3)建立信息字段对象Field
4)将Field添加到Document中
5)将Document添加到IndexWriter中
6)关闭索引器IndexWriter
创建Field:Field field=new Field(Field名称,Field内容,存储方式,索引方式);
创建Document:Document doc=new Document();
将Field添加到Document中:doc.add(field)
创建IndexWriter:IndexWriter writer=new IndexWriter(存储索引的路径,分析器的实例);
将Docment添加到IndesWriter中:writer.addDocument(doc);
关闭索引器:writer.close();
创建索引实例:
//为文件创建索引 package tool; import java.io.*; import tool.FileText; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.analysis.standard.StandardAnalyzer; public class FileIndex { /** * @param args */ public static void main(String[] args) throws java.io.IOException { // 设定索引文件的存放路径,为程序的同级目录下的file文件 String indexPath = "F:/DocumentIndex"; // 创建IndexWriter IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer()); // 创建Document Document doc = new Document(); File f = new File("F:/doc/a.htm"); // 创建Field-name String name = f.getName(); Field field = new Field("name", name, Field.Store.YES,Field.Index.TOKENIZED); // 添加field doc.add(field); // 创建Field-content String content = FileText.getText(f); field =new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED); //添加field doc.add(field); //创建Field-path String path=f.getPath(); field=new Field("path",path,Field.Store.YES,Field.Index.TOKENIZED); //添加field doc.add(field); //添加 document writer.addDocument(doc); //*****************************************/ doc=new Document(); f=new File("F:/doc/b.htm"); //创建Field-name name=f.getName(); field=new Field("name",name,Field.Store.YES,Field.Index.TOKENIZED); //添加field doc.add(field); //创建Field-content content=FileText.getText(f); field=new Field("content",content,Field.Store.YES,Field.Index.TOKENIZED); //添加field doc.add(field); //创建Field-path; path=f.getPath(); field=new Field("path",path,Field.Store.YES,Field.Index.TOKENIZED); //添加field doc.add(field); //添加Document writer.addDocument(doc); //关闭IndexWriter writer.close(); //提示 System.out.println("File Index Created!"); } }
相关文章推荐
- 3、学习Lucene3.5之索引创建--域选项
- 新一篇: lucene学习----创建索引 |
- lucene3.5学习笔记02--创建索引和建立搜索
- 一步一步跟我学习lucene(1)---lucene的IndexWriter对象创建和索引策略的选择
- lucene学习-创建索引
- Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询
- lucene学习之创建索引的方式
- Lucene5学习之创建索引入门
- Lucene 4.10 + Mysql 5.5 创建数据库表索引(Lucene 学习序列1)
- Lucene.net(4.8.0) 学习问题记录三: 索引的创建 IndexWriter 和索引速度的优化
- 01_java Lucene学习——创建索引
- lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)
- lucene学习笔记之索引创建、内容检索、分类器实现
- Lucene学习-创建索引、关键词查询
- Lucene学习笔记(1)-索引创建和简单的查询
- Lucene学习-创建索引(二)
- 1、学习Lucene3.5之创建索引
- lucene学习----创建索引
- lucene学习-创建索引
- lucene学习笔记1-创建索引