lucene学习-创建索引

   本文的lucene是基于lucene3.5版本.

   使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索。本节主要是记录创建索引部分的内容。

   创建的索引结构如图所示。

   

 

创建索引的步骤分为以下几个步骤:

1、建立索引器IndexWriter

2、创建文档对象Document

3、建立信息对象字段Field

4、将Field对象添加到Document

5、将Document对象添加到IndexWriter对象中

下面简要介绍几个核心对象。

(1)、创建IndexWriter对象。

IndexWriter writer=new IndexWriter(directory, iwc)。

directory是创建的索引要保存的路径,如果要保存在硬盘中则使用Directory directory = FSDirectory.open(new File(path))创建一个directory对象。

如果要保存在内存中则使用:RAMDirectory directory=new RAMDirectory()创建一个directory对象。

(2)、创建Document对象。

Document doc =new Document();创建了一个不含有任何Field的空Document,如果要要Field添加到Document中,则使用add(Field)方法即可实现。

doc.add(field)。

(3)、创建Field对象。

Field field=new Field(Field名称,Field内容,存储方式,索引方式);

存储方式分为3种:1、完全存储(Field.Store.YES);2、不存储(Field.Store.NO);3、压缩存储(Field.Store.COMPRESS)。

索引方式分为4种:1、不索引(Field.Index.NO);2、 Field.Index.ANALYZED ;3、 Field.Index.NOT_ANALYZED;4、Field.Index.NOT_ANALYZED_NO_NORMS

创建一个简单的索引程序代码如下所示:

public void Index() {
		String[] ids = { "1", "2", "3", "4" };
		String[] names = { "aa", "bb", "cc", "dd" };
		String[] contents = {
				"Using AbstractJExcelView to export data to Excel file via JExcelAPI library",
				"Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library. ",
				"Example to integrate Log4j into the Spring MVC application. ",
				"Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC. " };
		IndexWriter writer = null;
		try {
			Directory directory = FSDirectory.open(new File(path));
			// RAMDirectory directory=new RAMDirectory();
			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
					new StandardAnalyzer(Version.LUCENE_35));
			writer = new IndexWriter(directory, iwc);
			Document doc = null;
			for (int i = 0; i < ids.length; i++) {
				doc = new Document();
				doc.add(new Field("id", ids[i], Field.Store.YES,
						Field.Index.NOT_ANALYZED_NO_NORMS));
				doc.add(new Field("name", names[i], Field.Store.YES,
						Field.Index.NOT_ANALYZED_NO_NORMS));
				doc.add(new Field("contents", contents[i], Field.Store.YES,
						Field.Index.ANALYZED));
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				doc.add(new Field("date", sdf.format(new Date()),
						Field.Store.YES, Field.Index.NOT_ANALYZED));
				// Field.Index.ANALYZED;

				writer.addDocument(doc);
				writer.commit();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 

 

 

转载于:https://my.oschina.net/u/1158740/blog/306622

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值