Lucene的基本概念

2016-07-22  本文已影响0人  尚亦汐

分析器,一段有意义的文字需要通过Analyzer来分割成一个个词语后才能按关键词搜索。StandartdAnalyzer是Lucene中常用的分析器,对于中文分词有CJKAnalyzer、SmartChinieseAnalyzer等。

Documents are the unit of indexing and search. A Document is a set of fields. Each field has a name and a textual value. A field may be stored with the document, in which case it is returned with search hits on the document. Thus each document should typically contain one or more stored fields which uniquely identify it.
Note that fields which are not stored are not available in documents retrieved from the index.

简单的翻译如下:

文档是建立索引和搜索的基本单位,一个文档由一系列的filed组成,每个field有一个名称和一个值(键值对),一个field可以随文档一起存储,在搜索时候随着Document一起被返回。因此,每个Document都应该由一个或者多个能够唯一标识它的field组成。值得注意的是,没有被存储的field在检索的时候是不会被返回的。

怪不得每次创建Field的时候,都会有个Field.Store.YES,如下:

Field pathField = new StringField("path", file.toString(), Field.Store.YES);

一个Document可以包含多个列,叫做Field。例如一篇文章可以包含“标题”、“正文”、“修改时间”等Field。创建这些列对象后,可以通过Document的add方法增加这些列。如:

Document doc = new Document();   
Field pathField = new StringField("path", file.toString(), Field.Store.YES);
doc.add(pathField);

和一般的数据库不一样,一个文档的一个列可以有多个值。例如一篇文档即可以属于互联网类,有可以属于科技类。

Note: Lucene中的API相对数据库来说比较灵活,没有类似数据库先定义表结构后使用的过程。如果前后两次写索引时定义的列名称不一样,Lucene会自动创建新的列,所以Field的一致性需要我们自己掌握。

上一篇 下一篇

猜你喜欢

热点阅读