Lucene的总体架构——基于Lucene7.0

前言:根据Lucene7.0版本介绍Lucene相关知识

总体架构

在Lucene in action中,Lucene 的构架和过程如下图,


Lucene in action 中Lucene架构

根据Lucene架构图,lucene主要包含两个过程,建立索引以及搜索索引。

  • 建立索引
    使用Lucene时,将被搜索的内容包括文件、数据库、web数据等数据源转换成lucene的Document对象,存入系统文件或内存中,建立索引。
  • 搜索索引
    当用户搜索时,将用户搜索内容封装成Query对象,并通过search函数搜索Lucene索引,如果命中关键词,则返回命中的TopDocs对象

具体代码(Lucene7.0)

 public void buildIndex() {
        try {
            //索引建立过程
            //创建索引目录,RAMDirectory基于内存
            RAMDirectory directory = new RAMDirectory();
            //保存用于创建IndexWriter对象的所有配置;WhitespaceAnalyzer空白分词器,用于建立索引时的分词
            IndexWriterConfig config = new IndexWriterConfig(new WhitespaceAnalyzer());
            //创建IndexWriter用于建立索引
            IndexWriter indexWriter = new IndexWriter(directory, config);
            //Document索引的载体,每条索引信息对应一个document对象
            Document document = new Document();
            //Field对象相当于索引信息的某个属性,通过add()添加到document中,StringField的value作为一个整体,不能再被分词;第三个参数代表是否被存储
            document.add(new StringField("name", "seansherwood", Field.Store.YES));
            //TextField中的值需要被分词器分词
            document.add(new TextField("description", "like Android and Lucene", Field.Store.YES));
            //建立索引
            indexWriter.addDocument(document);
            //建立索引之后要将indexWriter对象commit方法,对于索引的增删改操作都需要进行commit;如果建立索引完成可以直接调用close,其默认调用了commit
            indexWriter.close();
            //搜索过程
            //IndexSearcher用于索引搜索,参数需要创建索引时的索引目录
            IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
            //封装搜索查询语句
            Query query = new TermQuery(new Term("description", "Lucene"));
            //搜索返回命中的document的ID对象
            TopDocs hit = searcher.search(query, 10);
            System.out.println(hit.scoreDocs.length);
            //遍历拿到索引中的document
            for (int i = 0; i < hit.scoreDocs.length; i++) {
                Document doc = searcher.doc(hit.scoreDocs[i].doc);
                System.out.println("name:" + doc.get("name"));
                System.out.println("description:" + doc.get("description"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

注:能力一般,水平有限,如有不当之处,请批评指正,定当虚心接受!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容