Lucene4源代码分析之三:Document
Document是Lucene Core中的几个核心Package之一,也是最重要的一个实体对象。网民使用搜索引擎的目的就是为了找到相关的文档即Document。
Lucene中一个Document由多个Field组成,如一个文档可以有多个属性,如路径、标题、内容、修改时间、作者等,每个属性就是一个Field。相关UML图如下:
Field是一个基类,实现了IndexableField接口,主要有三个属性,名字、值和类型,其有多个子类,每个子类只是简单的设定了Field的类型(FieldType)。FieldType最主要的三个属性是:
//是否被索引,设置成False时,不被索引,不能被查询
private boolean indexed;
//是否存储,这个字段有点难理解,其实这个字段与索引(反向索引)无关,只与正向索引相关。所谓反向索引就是通过词找到Document ID,正向索引就是通过Document ID找到相关内容。所以只有stored为True的Field才能被正向索引找到,也就是能在搜索引擎中展示。
private boolean stored;//进行反向索引时是否切词
private boolean tokenized = true;
TextField的代码如下,可以看到TextField一定会被索引和切词,但不一定会被存储。当值为Reader和TokenStream时,不存储,是一个String时可以指定是否存储。
public final class TextField extends Field {
/** Indexed, tokenized, not stored. */
public static final FieldType TYPE_NOT_STORED = new FieldType();/** Indexed, tokenized, stored. */
public static final FieldType TYPE_STORED = new FieldType();static {
TYPE_NOT_STORED.setIndexed(true);
TYPE_NOT_STORED.setTokenized(true);
TYPE_NOT_STORED.freeze();TYPE_STORED.setIndexed(true);
TYPE_STORED.setTokenized(true);
TYPE_STORED.setStored(true);
TYPE_STORED.freeze();
}// TODO: add sugar for term vectors...?
/** Creates a new un-stored TextField with Reader value.
* @param name field name
* @param reader reader value
* @throws IllegalArgumentException if the field name is null
* @throws NullPointerException if the reader is null
*/
public TextField(String name, Reader reader) {
super(name, reader, TYPE_NOT_STORED);
}/** Creates a new TextField with String value.
* @param name field name
* @param value string value
* @param store Store.YES if the content should also be stored
* @throws IllegalArgumentException if the field name or value is null.
*/
public TextField(String name, String value, Store store) {
super(name, value, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED);
}
/** Creates a new un-stored TextField with TokenStream value.
* @param name field name
* @param stream TokenStream value
* @throws IllegalArgumentException if the field name is null.
* @throws NullPointerException if the tokenStream is null
*/
public TextField(String name, TokenStream stream) {
super(name, stream, TYPE_NOT_STORED);
}
}