Dissect Lucene – Lucene中的文档

在包org.apache.lucene.document中,三个类,其中DateField是用来处理日期与字符串转换的工具类,来方便的处理日期类型的Field。

Field就像我们学过的数据库中的字段,简单的说,就是一个名值对。这个域有三种属性,分别是

isStored – 是否被存储

isIndexed – 是否被索引

isTokenized – 是否分词

这些属性的组合又构成了四种不同类型的Field,而且各有用途,用Xerdoc DSearch中举个例子:

Keyword(String name, String value)

存储、索引、不分词,用于URI(比如MSN聊天记录的日期域、比如MP3文件的文件全路径等等)

UnIndexed(String name, String value)

存储、不索引、不分词,比如文件的全路径

UnStored(String name, String value)

不存储、索引、分词,比如HTML的正文、Word的内容等等,这部分内容是要被索引的,但是由于具体内容通常很大,没有必要再进行存储,可以到时候根据URI再来挖取。所以,这部分只分词、索引,而不存储。

Text(String name, String value)

存储、索引、分词,比如文件的各种属性,比如MP3文件的歌手、专辑等等。

Text(String name, Reader value)

不存储、索引、分词。

Field经常需要进行Name的比较,比如:

  1. /** Returns a field with the given name if any exist in this document, or
  2.    * null.  If multiple fields exists with this name, this method returns the
  3.    * first value added.
  4.    */
  5.   public final Field getField(String name) {
  6.     for (int i = 0; i < fields.size(); i++) {
  7.       Field field = (Field)fields.get(i);
  8.       if (field.name().equals(name))
  9.     return field;
  10.     }
  11.     return null;
  12.   }

因此,基于效率上的考虑,Field的fieldName(字段名)是要被intern的,。如果你不明白Intern,可以看看这篇文章"Can I use ‘==’ to compare two String?"。

Lucene中的Document(文档)就如同数据库中的一个数据列一样,是一个Field的集合。类Document用一个Vector来存储这个Field集合,然后提供一些基本的add, remove等接口来进行管理。

Document还能设置这个文档的boost(翻译成权重?评分?),这些会在后面讲到。

Field和Document都是final Class,不能被继承。

Popularity: 13% [?]

Related entries:

2 Responses to “Dissect Lucene – Lucene中的文档”

  1. Winters Mi Says:

    Yo, 开始写Lucene了,呵呵

  2. Meng Yan Says:

    有时间就写写,呵呵

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI