首页 > 代码库 > lucene学习-创建索引

lucene学习-创建索引

   本文的lucene是基于lucene3.5版本.

   使用lucene实现搜索引擎开发,核心的部分是建立索引和搜索。本节主要是记录创建索引部分的内容。

   创建的索引结构如图所示。

   

 

创建索引的步骤分为以下几个步骤:

1、建立索引器IndexWriter

2、创建文档对象Document

3、建立信息对象字段Field

4、将Field对象添加到Document

5、将Document对象添加到IndexWriter对象中

下面简要介绍几个核心对象。

(1)、创建IndexWriter对象。

IndexWriter writer=new IndexWriter(directory, iwc)。

directory是创建的索引要保存的路径,如果要保存在硬盘中则使用Directory directory = FSDirectory.open(new File(path))创建一个directory对象。

如果要保存在内存中则使用:RAMDirectory directory=new RAMDirectory()创建一个directory对象。

(2)、创建Document对象。

Document doc =new Document();创建了一个不含有任何Field的空Document,如果要要Field添加到Document中,则使用add(Field)方法即可实现。

doc.add(field)。

(3)、创建Field对象。

Field field=new Field(Field名称,Field内容,存储方式,索引方式);

存储方式分为3种:1、完全存储(Field.Store.YES);2、不存储(Field.Store.NO);3、压缩存储(Field.Store.COMPRESS)。

索引方式分为4种:1、不索引(Field.Index.NO);2、 Field.Index.ANALYZED ;3、 Field.Index.NOT_ANALYZED;4、Field.Index.NOT_ANALYZED_NO_NORMS

创建一个简单的索引程序代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public void Index() {
        String[] ids = { "1", "2", "3", "4" };
        String[] names = { "aa", "bb", "cc", "dd" };
        String[] contents = {
                "Using AbstractJExcelView to export data to Excel file via JExcelAPI library",
                "Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library. ",
                "Example to integrate Log4j into the Spring MVC application. ",
                "Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC. " };
        IndexWriter writer = null;
        try {
            Directory directory = FSDirectory.open(new File(path));
            // RAMDirectory directory=new RAMDirectory();
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,
                    new StandardAnalyzer(Version.LUCENE_35));
            writer = new IndexWriter(directory, iwc);
            Document doc = null;
            for (int i = 0; i < ids.length; i++) {
                doc = new Document();
                doc.add(new Field("id", ids[i], Field.Store.YES,
                        Field.Index.NOT_ANALYZED_NO_NORMS));
                doc.add(new Field("name", names[i], Field.Store.YES,
                        Field.Index.NOT_ANALYZED_NO_NORMS));
                doc.add(new Field("contents", contents[i], Field.Store.YES,
                        Field.Index.ANALYZED));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                doc.add(new Field("date", sdf.format(new Date()),
                        Field.Store.YES, Field.Index.NOT_ANALYZED));
                // Field.Index.ANALYZED;
 
                writer.addDocument(doc);
                writer.commit();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (CorruptIndexException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }