`

lucene学习笔记4

阅读更多
下面讲一下索引的建立

其实从上面的例子就可以看出建立索引就用到Document,IndexWriter,Field。

最简单的步骤就是:

首先分别new 一个Document,IndexWriter,Field

然后用Doument.add()方法加入Field,

其次用IndexWrtier.addDocument()方法加入Document。

最后调用一下IndexWriter.close()方法关闭输入索引,这一步非常的重要只有调用这个方法索引才会被写入索引的目录里,而这是被很多

初学的人所忽略的。

Document没有什么好介绍的,把它的作用看成数据库中的一行记录就行。

Field是一个比较重要的也是比较复杂的:

看一下它的构造函数有5个:

Field(String name, byte[] value, Field.Store store)

Field(String name, Reader reader)

Field(String name, Reader reader, Field.TermVector termVector)

Field(String name, String value, Field.Store store, Field.Index index)

Field(String name, String value, Field.Store store, Field.Index index, Field.TermVector termVector)

在Field中有三个内部类:Field.Index,Field.Store,Field.termVector,而构造函数也用到了它们。

注意:termVector是Lucene 1.4新增的它提供一种向量机制来进行模糊查询的这个不常用,默认是false不过是什么对于一般查询无影响。


它们的不同的组合,在全文检索中有着不同的作用。看看下面的表吧:

Field.Index

Field.Store

看本文上面的介绍

说明

TOKENIZED(分词)

YES

文章的标题或内容(如果是内容的话不能太长)是可以被搜索的

TOKENIZED

NO

文章的标题或内容(内容可以很长)也是可以被看过的

NO

YES

这是不能被搜索的,它只是被搜索内容的附属物。如URL等

UN_TOKENIZED

YES/NO

不被分词,它作为一个整体被搜索,搜一部分是搜不出来的

NO

NO

没有这种用法

而对于Field(String name, Reader reader)

Field(String name, Reader reader, Field.TermVector termVector)

**注意**:他们是Field.Index.TOKENIZED和Field.Store.NO的。这就是为什么我们在上面的例子中会出现文章的内容为 null了。因为它只是被索引了,
而并没有被存储下来。如果一定要看到文章的内容的话可以通过文章的路径得到毕竟文章的路径是作为搜索的附属物被搜索出来了。而我们在
Web开发的时候一般是将大数据放在数据库中,不会放在文件系统中,更不会放在索引目录里,因为它太大了操作会加大服务器的负担。

下面介绍一下IndexWriter:

它就是一个写入索引的写入器,它的任务比较简单:

1.用addDocument()将已经准备好写入索引的document们加入

2.调用close()将索引写入索引目录

先看一下它的构造函数:

IndexWriter(Directory d, Analyzer a, boolean create)

IndexWriter(File path, Analyzer a, boolean create)

IndexWriter(String path, Analyzer a, boolean create)

可见构造它需要一个索引文件目录,一个分析器(一般用标准的这个),最后一个参数是标识是否清空索引目录

它有一些设置参数的功能如:设置Field的最大长度

看个例子:
public void IndexMaxField() throws IOException {

        IndexWriter indexWriter= new IndexWriter("c:\\\\index",new StandardAnalyzer(),true);

        Document doc1 = new Document();

        doc1.add(new Field("name1","程序员之家",Field.Store.YES,Field.Index.TOKENIZED));

        Document doc2 = new Document();

        doc2.add(new Field("name2","Welcome to the Home of programers",Field.Store.YES,Field.Index.TOKENIZED));

        indexWriter.setMaxFieldLength(5);

        indexWriter.addDocument(doc1);

        indexWriter.setMaxFieldLength(3);

        indexWriter.addDocument(doc1);

        indexWriter.setMaxFieldLength(0);

        indexWriter.addDocument(doc2);

        indexWriter.setMaxFieldLength(3);

        indexWriter.addDocument(doc2);

        indexWriter.close();
}




public void SearcherMaxField() throws ParseException, IOException {

        Query query = null;

        Hits hits = null;

        IndexSearcher indexSearcher= null;

        QueryParser queryParser= null;

        queryParser = new QueryParser("name1",new StandardAnalyzer());

        query = queryParser.parse("程序员");

        indexSearcher= new IndexSearcher("c:\\\\index");

        hits = indexSearcher.search(query);

        System.out.println("您搜的是:程序员");

        System.out.println("找到了"+hits.length()+"个结果");

        System.out.println("它们分别是:");

        for (int i = 0; i < hits.length(); i++) {

            Document doc = hits.doc(i);

            System.out.println(doc.get("name1"));

      }

        query = queryParser.parse("程序员之家");

        indexSearcher= new IndexSearcher("c:\\\\index");

        hits = indexSearcher.search(query);

        System.out.println("您搜的是:程序员之家");

        System.out.println("找到了"+hits.length()+"个结果");

        System.out.println("它们分别是:");

        for (int i = 0; i < hits.length(); i++) {

            Document doc = hits.doc(i);

            System.out.println(doc.get("name1"));

        }

        queryParser = new QueryParser("name2",new StandardAnalyzer());

        query = queryParser.parse("Welcome");

        indexSearcher= new IndexSearcher("c:\\\\index");

        hits = indexSearcher.search(query);

        System.out.println("您搜的是:Welcome");

        System.out.println("找到了"+hits.length()+"个结果");

        System.out.println("它们分别是:");

        for (int i = 0; i < hits.length(); i++){

            Document doc = hits.doc(i);

            System.out.println(doc.get("name2"));

        }

        query = queryParser.parse("the");

        indexSearcher= new IndexSearcher("c:\\\\index");

        hits = indexSearcher.search(query);

        System.out.println("您搜的是:the");

        System.out.println("找到了"+hits.length()+"个结果");

        System.out.println("它们分别是:");

        for (int i = 0; i < hits.length(); i++){

            Document doc = hits.doc(i);
            System.out.println(doc.get("name2"));

        }

        query = queryParser.parse("home");

        indexSearcher= new IndexSearcher("c:\\\\index");

        hits = indexSearcher.search(query);

        System.out.println("您搜的是:home");

        System.out.println("找到了"+hits.length()+"个结果");

        System.out.println("它们分别是:");
        for (int i = 0; i < hits.length(); i++){

            Document doc = hits.doc(i);

            System.out.println(doc.get("name2"));

        }

}

它的运行结果为:







总结一下:




1.设置Field的长度限制只是限制了搜索。如果用了Field.Store.YES的话还是会全部被保存进索引目录里的。

2.为什么搜the没有搜出来呢是因为lucene分析英文的时候不会搜索the to of 等无用的词(搜这些词是无意义的)。

3.New StandardAnlayzer()对于英文的分词是按空格和一些无用的词,而中文呢是全部的单个的字。

4.设置Field的最大长度是以0开头和数组一样。

程序员之家----------3--------程序员之

                                    0 1 2  3

Welcome to the home of programmers------3------Welcome to the home of programmers


                                 0  1  2

大家还可以试一下别的,以便加深一下印象,到现在我们已经可以用lucene建立索引了
分享到:
评论

相关推荐

    Lucene 学习笔记 1

    **Lucene 学习笔记 1** Lucene 是一个全文搜索引擎库,由 Apache 软件基金会开发。它提供了一个可扩展的、高性能的搜索框架,使得开发者能够在其应用程序中集成高级的搜索功能。本篇学习笔记将深入探讨 Lucene 的...

    lucene学习笔记

    标题:Lucene学习笔记 描述:Lucene学习笔记,Lucene入门必备材料 知识点: 一、Lucene概述与文档管理策略 Lucene是一款高性能、全功能的文本搜索引擎库,广泛应用于文档检索、全文搜索等场景。为了提升搜索效率...

    【大搜集:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt Lucene-2.0学习文档 .txt Lucene入门与使用 .txt lucene性能.txt 大富翁全文索引和查询的例子...

    Lucene学习笔记(一)Lucene入门实例

    NULL 博文链接:https://kylinsoong.iteye.com/blog/719415

    Lucene学习笔记

    【Lucene学习笔记】 Lucene 是一款开源的全文检索框架,由Apache软件基金会维护,它提供了高效的、可扩展的搜索引擎功能。不同于一个完整的应用程序,Lucene 提供的是一个基础组件,开发者可以将其集成到自己的应用...

    lucene3.5学习笔记

    ### Lucene 3.5 学习笔记 #### 一、Lucene 3.5 基本概念 ##### 1.1 Lucene 概述 **1.1.1 IndexWriter** `IndexWriter` 是 Lucene 中的核心类之一,用于创建或更新索引。它提供了添加文档、删除文档、优化索引等...

    lucene 3.5学习笔记

    《Lucene 3.5 学习笔记》 在信息技术高速发展的今天,搜索引擎技术成为了信息检索的核心工具。Apache Lucene,作为一个开源全文检索库,为开发者提供了强大的文本搜索功能。本文将深入探讨Lucene 3.5版本的相关知识...

    lucene3.0学习笔记(三)与paoding整合

    《Lucene 3.0 学习笔记(三)与Paoding整合》 在深入了解Lucene 3.0的过程中,我们经常会遇到如何将其与第三方工具进行整合的问题,以提升搜索性能和用户体验。这篇学习笔记主要关注的是将Lucene 3.0与Paoding搜索...

    lucene基础学习笔记&源码

    **Lucene 基础学习笔记与源码分析** **一、Lucene 概述** Lucene 是一个高性能、全文本搜索库,由 Apache 软件基金会开发并维护。它是一个 Java 开发的开源项目,被广泛应用于各种搜索引擎的构建,支持多种编程...

    本人的Lucene2.9学习笔记

    《深入理解Lucene 2.9.1:构建与搜索的全方位解析》 Lucene,作为一款开源的全文搜索引擎库,被广泛应用于各种信息检索场景。本文将详细讲解Lucene 2.9.1版本的核心概念、架构以及索引创建与搜索的流程。 一、...

    Lucene 课堂笔记

    ### Lucene 课堂笔记知识点详解 #### 一、信息检索概览 **1.1 信息检索的概念** 信息检索指的是从海量信息集中筛选出与用户需求相关联的信息。本课程主要探讨文本信息的检索,虽然实际应用中还可能涉及图像、音频...

Global site tag (gtag.js) - Google Analytics