Lucene使用文件扩展名标识不同的索引文件。如.fnm文件存储域Fields名称及其属性,.fdt存储文档各项域数据,.fdx存储文档在fdt中的偏移位置即其索引文件,.frq存储文档中term位置数 据,.tii文件存储term字典,.tis文件存储term频率数据,.prx存储term接近度数据,.nrm存储调节因子数据,另外 segments_X文件存储当前最新索引片段的信息,其中X为其最新修改版本,segments.gen存储当前版本即X值。

 

本系列文章将详细介绍的这些文件的数据存储细节。下面的图描述了一个典型的lucene索引文件列表:

它们的关系图则如下所示:

 

 

其中, Segments是一个比较特殊的结构:

  • 一个索引可以包含多个段,段与段之间是独立的,添加新文档可以生成新的段,不同的段可以合并。
  • 如上图,具有相同前缀文件的属同一个段,图中共两个段 "_0" 和 "_1"。
  • segments.gen和segments_5是段的元数据文件,也即它们保存了段的属性信息

 

 

《索引文件格式》专题用例

 

在后面详细介绍每个索引文件的时候,都会使用一个例子中的数据进行分析。而这个例子就是《索引创建(5):索引数据池及内存数据细节 》中在内存中所建立好的那个倒排索引的例子。这里再次详细说明一下,后面将不再对这个例子做出说明:


 

(1)待索引文件集合一共四篇文档,分别是

文档名

文档路径

文档内容

lucene 1

E:\实验\content\lucene 1.txt

lucene lucene lucene lucene lucene term .

lucene 2

E:\实验\content\lucene 2.txt

lucene lucene lucene lucene lucene term term.

lucene 3

E:\实验\content\lucene 3.txt

term term term lucene lucene lucene lucene lucene.

lucene 4

E:\实验\content\lucene 4.txt

term

 

(2)内存源数据组织形式(Document/Fields) 参见《索引创建(1):IndexWriter索引器 》中的索引创建代码:


Java代码

for (每个文本文件) {     
         //Lucene的文档结构  
         Document doc = new Document();                       
         //文件名称,可查询,不分词  
         String fileName=file.getName().substring(0,file.getName().indexOf("."));  
         doc.add(new Field("name",fileName, Field.Store.YES, Field.Index.NOT_ANALYZED));    
          //文件路径,可查询,不分词  
         String filePath=file.getPath();  
         doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED));  
        //文件内容,需要检索  
         doc.add(new Field("content", new FileReader(file)));              
         //使用索引器对Document文档建索引  
        standardWriter.addDocument(doc);    
}

这样,其数据源在Lucene的内存结构Document, Field表示如下:

 

真实文档名

Document对象

lucene 1

doc1

lucene 2

doc2

lucene 3

doc3

lucene 4

doc4

 

每个document包含的Field信息如下(以doc1举例):


域名

域数据值

是否被索引

(Indexed)

是否被存储

(Stored)

Field1

"name"

"lucene 1"

     Y

        N

Field2

"path"

"E:\实验\content\lucene 1.txt"

     Y

        N

Field3

"content"

lucene lucene lucene lucene lucene term .

     Y

        N

 

(3)内存索引表的结构如:《索引创建 (5):索引数据池及内存数据细节 》中所述。