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

2017-11-07  本文已影响222人  Mr韶先生
前言:根据Lucene7.0版本介绍Lucene相关知识

总体架构

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


Lucene in action 中Lucene架构

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

具体代码(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();
        }

    }

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

上一篇 下一篇

猜你喜欢

热点阅读