zoukankan      html  css  js  c++  java
  • Lucene基础(一)--入门

    Lucene介绍

    lucene的介绍,这里引用百度百科的介绍Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供。Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。在Java开发环境里Lucene是一个成熟的免费开源工具。就其本身而言,Lucene是当前以及最近几年最受欢迎的免费Java信息检索程序库。人们经常提到信息检索程序库,虽然与搜索引擎有关,但不应该将信息检索程序库与搜索引擎相混淆。

    了解lucene的相关知识直接进入lucene官网 http://lucene.apache.org/

    在lucene的官网我们会发现还会有一个solr的开源的软件,它是基于的一个上层写的应用,lucene给我提供的是类似sdk的功能,开发人员需要更多的自己写代码,而solr相对少些代码。可想而知,如果使用lucene的话,我们可以更多的自定义自己的搜索

    Lucene下载安装

    进入上面的lucene的官方网站,找到下载后解压就行

    根据lucene的版本的不同,需要的jdk的版本也会有差异,这个需要在官方网站上看看说明文档,本次测试使用的lucene4.9,编译的jdk为1.7

    Unsupported major.minor version 51.0 xxx 
    如果出现类似的异常,就是下载的lucene和编译的jdk的版本不对,更新个jdk1.7 就可以

    Lucene入门实例

    下面是在lucene的官方文档里的一段入门代码

    public class LuceneDemo

    {

      private static final Version version = Version.LUCENE_4_9;

      public static void main(String[] args) throws Exception

      {

        Analyzer analyzer = new StandardAnalyzer(version);

        // Store the index in memory:

        Directory directory = new RAMDirectory();

        // To store an index on disk, use this instead:

        //Directory directory = FSDirectory.open("/tmp/testindex");

        IndexWriterConfig config = new IndexWriterConfig(version, analyzer);

        IndexWriter iwriter = new IndexWriter(directory, config);

        Document doc = new Document();

        Document doc2 = new Document();

        String text = "This is the text to be indexed.";

        String text2 = "This is the text to be indexed22222.";

        doc.add(new Field("fieldname", text, TextField.TYPE_STORED));

        doc2.add(new Field("fieldname", text2, TextField.TYPE_STORED));

        iwriter.addDocument(doc); iwriter.addDocument(doc2); iwriter.close();

        // Now search the index:

        DirectoryReader ireader = DirectoryReader.open(directory);

        IndexSearcher isearcher = new IndexSearcher(ireader);

        // Parse a simple query that searches for "text":

        QueryParser parser = new QueryParser(version, "fieldname", analyzer);

        Query query = parser.parse("text");

        ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;

        // Iterate through the results:

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

        {

          Document hitDoc = isearcher.doc(hits[i].doc);

          System.out.println("This is the text to be indexed="+hitDoc.get("fieldname"));

        }

        ireader.close();

        directory.close();

        }

      }

    代码解析

    写索引

    建立索引开始建立Analyzer,创建索引保存位置创建写(IndexWriter)索引对象建立文档添加文档域添加文档关闭结束

    读索引

    从代码可以看到基本操作步骤:

    1. 先知名索引的位子,建立读索引的对象
    2. 创建查询器 Query(query有很多实现类,例子中用的是QueryParse的方法)
    3. 执行search方法,得到ScoreDoc ,解析文档
    4. 关闭读写器

    Lucene主要元素

    Lucene中有几个重要的元素

    1. IndexWriter – 用来创建写索引的对象,与之配合使用的有一个IndexWriterConfig,用来对indexwriter做配置
    2. IndexSearch – 与indexwriter对应,用来读所以
    3. Document – 被索引的内容形成一个document文档
    4. Field – 文档的域
    5. Query – 用来查询的对象
    6. Term – 构建查询
    7. ScoreDoc – 命中的结果集

    比如:一篇文章,我们有title,author,createTime,category,content那么这每一个可以看做一个域,整个文章就是一个document,如果搜索内容content包含了lucene的文件,就会更具域content的所有找到有Lucene的文章。

  • 相关阅读:
    struct 结构体解析(原)
    C++标准编程:虚函数与内联
    基于 IOCP 的通用异步 Windows Socket TCP 高性能服务端组件的设计与实现
    直接用编译器按ctrl+F5运行和双击运行结果不一样
    驱动编译的时候注意编译工程选项
    驱动编译的时候注意编译工程选项
    'ddkbuild.cmd' 不是内部或外部命令,也不是可运行的程序
    'ddkbuild.cmd' 不是内部或外部命令,也不是可运行的程序
    NtOpenProcess被HOOK,跳回原函数地址后仍然无法看到进程
    NtOpenProcess被HOOK,跳回原函数地址后仍然无法看到进程
  • 原文地址:https://www.cnblogs.com/downey/p/4890790.html
Copyright © 2011-2022 走看看