论坛首页 Java版 Hibernate

Hibernate Lucene 的集成

浏览 1106 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-06-09

Hibernate Lucene Integration

Hibernate Lucene 的集成

Lucene is a high-performance Java search engine library available from
the Apache Software Foundation. Hibernate Annotations includes a package of
annotations that allows you to mark any domain model object as indexable and
have Hibernate maintain a Lucene index of any instances persisted via
Hibernate.

Lucene是一个高性能的java搜索引擎库,可以从 Apache软件基金组织获取。Hibernate Annotations包括一个注解包,它允许把任何域模型对象标记为可索引的,并且对任何经由Hibernate进行持续化的实例,Hibernate 都会为之维护一个对应的Lucene索引。

Using Lucene to index your entities

使用Lucene为实体建立索引

Annotating your domain model

注解域模型

First, we must declare a persistent class as
@Indexed:

首先,必须将一个持久类声明为@Indexed:

programlisting

@Entity
@Indexed(index="indexes/essays")
public class Essay {
...
}

 

The index attribute tells Hibernate where the
Lucene index is located (a directory on your file system). If you wish
to define a base directory for all lucene indexes, you can use the
hibernate.lucene.index_dir property in your
configuration file.

属性index是告诉Hibernate, Lucene索引信息所在的位置(你文件系统的某个目录)。如果你想为所有的Lucene索引定义一个根目录,你可以在配置文件中用属性hibernate.lucene.index_dir进行配置。

Lucene indexes contain four kinds of fields:
keyword fields, text fields,
unstored fields and unindexed
fields. Hibernate Annotations provides annotations to mark a property of
an entity as one of the first three kinds of indexed fields.

Lucene索引包括四种字段:

keyword字段,text字段,unstored字段和unindexed字段。Hibernate注解提供了将实体属性标记为前三种被索引字段的注解。

programlisting

@Entity
@Indexed(index="indexes/essays")
public class Essay {
...@Id
@Keyword(id=true)
public Long getId(){ return id; } @Text(name="Abstract")
public String getSummary(){ return summary; } @Lob
@Unstored
public String getText(){ return text; } }

 

These annotations define an index with three fields:
Id, Abstract and
Text.

这些注解定义了一个带有三个字段的索引:Id, Abstract
Text

Note: you must specify
@Keyword(id=true) on the identifier property of your
entity class.

注意:你必须在你的实体类的标志属性上指定@Keyword(id=true)

The analyzer class used to index the elements is configurable
through the hibernate.lucene.analyzer property. If
none defined,
org.apache.lucene.analysis.standard.StandardAnalyzer
is used as the default.

用于对元素建立索引的分析器类是可以通过hibernate.lucene.analyzer属性进行配置的。如果没有定义,则把org.apache.lucene.analysis.standard.StandardAnalyzer作为缺省。

Enabling automatic indexing

激活自动索引

Finally, we enable the LuceneEventListener for
the three Hibernate events that occur after changes are committed to the
database.

我们激活用于帧听三类Hibernate事件的LuceneEventListener,这些事件会在变更被提交至数据库后产生。

programlisting

...

 

review 完毕:

1. Apache Software Foundation, Apache软件基金组织
2. domain model object,领域模型对象
3. First, we must declare a persistent class as @Indexed
  首先我们必须将持久类声明为@Indexed
4. The index attribute tells Hibernate where the Lucene index is located
  属性index告知Hibernate,Lucene索引信息所在的位置
5. If you wish to define a base directory for all lucene indexes,
  如果你想为......定义一个根目录(base directory)
6. one of the first three kinds of indexed fields.
  ......被索引字段......
7. These annotations define an index with three fields:
  上述注解定义了一个带有三个字段的索引:
8. on the identifier property of
  标识属性(identifier property)
9. The analyzer class used to index the elements is configurable through the hibernate.lucene.analyzer property.
  用于对元素建立索引的分析器类是可以通过......属性进行配置的
10. Enabling automatic indexing
  激活自动索引
11. we enable the LuceneEventListener for
the three Hibernate events that occur after changes are committed to the
database.
  我们激活用于帧听三类Hibernate事件的LuceneEventListener,这些事件会在变更被提交至数据库后产生。


   
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐