Lucene入门(一)

Lucene是一款非常棒的搜索引擎,而Lucene.NET是Lucene的.NET版本,本入门文档将以Lucene.NET 3.03为例。

1、介绍一下创建索引过程的核心类

技术分享


Directory类描述了Lucene索引存放的位置,它是一个抽象类,它的子类负责具体指定索引的存储路径。例如FSDirectory类是负责真实文件系统的存储。

IndexWriter是写索引的核心类,该类负责创建新索引或者打开已有的索引,以及向索引中添加、更新、删除被索引文档。该类不负责读取和搜索索引。

Analyzer是分词器的抽象类,分词器用来对文本信息进行分析处理,例如:输入的索引信息是Let me see.经过分词器分析后,得到三个单词Let、me、see。当然有很多分词规则,视实际需要而定。待索引文本需要经过分词器分析后,才能提交给IndexWriter类创建索引。

Document代表待索引的文档信息,可以通俗的理解为一条记录或一篇文章,而Field就是组成Document的最小单位。例如,需要保存10条记录的数据,每个记录包含title和content两个信息项,那么一条记录就是一个Document对象,而title和content就是两个Field。

2、介绍一下搜索索引过程的核心类

技术分享

Searcher是一个用于搜索由 IndexWriter 类创建的索引,它是个抽象类,其中,IndexSearcher是最常用的派生类,该类最简单的实现是接收单个Query和int topN计数作为参数,返回一个TopDocs对象。

Query是查询子类,它是一个抽象类。搜索指定单词或词组涉及到在项中包装它们,将项添加到查询对象,将查询对象传递到Searcher的搜索方法。

Term是搜索功能的基本单元。与Field对象类似,Term对象包含一对字符串的键值对,即域名和单词。

TopDocs是一个简单的指针容器,其包含前Searcher对象返回的搜索结果,搜索结果包含在其ScoreDoc数组属性中,还包含这些文档的DocId和浮点型分数。

3、下面给出添加索引和搜索的简单示例

添加索引

// 索引文件所在目录
private string INDEX_STORE_PATH = "D:\\SearchIndex";

private void CreateIndex()
{
    // 创建Directory对象,指向索引文件保存的目录
    using (Directory indexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(INDEX_STORE_PATH)))
    {
        // 定义使用标准分词器
        using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
        {
            // 检查目录是否已经存在
            bool iscreate = !Lucene.Net.Index.IndexReader.IndexExists(indexDirectory);

            // 使用之前创建的分析器、目录来创建索引写对象
            using (IndexWriter writer = new IndexWriter(indexDirectory, analyzer, iscreate, IndexWriter.MaxFieldLength.UNLIMITED))
            {
                // 向索引中添加文档
                AddDocument(writer, "Visual Studio", "Visual Studio 2013");
                AddDocument(writer, "Microsoft Co.", "Microsoft will publish Visual Studio 2013");

                // 优化索引
                writer.Optimize();
            }
        }
    }
}

static void AddDocument(IndexWriter writer, string title, string content)
{
    // 创建文档对象
    Document document = new Document();

    // 给文档添加Field对象
    document.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
    document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));

    // 将文档添加到writer对象中,由其后代码创建索引
    writer.AddDocument(document);
}

搜索索引

public void Search()
{
    // 创建Directory对象,指向索引文件保存的目录
    using (Directory indexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(INDEX_STORE_PATH)))
    {
        // 创建标准分词器
        using (Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
        {
            // 创建索引搜索对象
            using (IndexSearcher searcher = new IndexSearcher(indexDirectory, true))
            {
                // 查找title中包含visual单词的文档
                Term t = new Term("title", "visual");
                Query query = new TermQuery(t);
                TopDocs docs = searcher.Search(query, 10);
                Console.WriteLine(docs.TotalHits);

                // 查找content中包含studio单词的文档
                t = new Term("content", "studio");
                docs = searcher.Search(new TermQuery(t), 10);
                Console.WriteLine(docs.TotalHits);

            }
        }
    }
}




参考:《Lucene in Action》





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。