Lucene QuickStart

Posted 分布式服务

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucene QuickStart相关的知识,希望对你有一定的参考价值。

Lucene QuickStart

版本6.6.1

package com.ghost.lucene.index;

 

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.document.TextField;

import org.apache.lucene.index.DirectoryReader;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.queryparser.classic.ParseException;

import org.apache.lucene.queryparser.classic.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.Sort;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.RAMDirectory;

importstatic org.junit.Assert.assertEquals;

 

import java.io.IOException;

 

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

 

publicclass QuickStart {

 

    Analyzer   analyzer;

    Directory directory;

 

    @Before

    publicvoid before() {

        analyzer = new  StandardAnalyzer();

 

        // 将索引存在内存中

        directory = new RAMDirectory();

    }

 

    /**

     * 建立索引:

     * 1)Directory描述索引存放位置

     * 2)IndexWriter索引过程的核心组件,负责新建索引,向索引中增删改文档

     * 3)Document索引文档

     * 4)Field域,一个文档由多个域组成

     *

     * 检索:

     * 1)IndexSearcher索引入口

     * 2)Query封装查询

     * 3)QueryParser将用户输入的查询表达式处理成具体的Query对象

     * 4)TopDocs返回的较高评分的顶部文档

     * 5)ScoreDoc提供TopDocs每条搜索结果的接口

     */

    @Test

    publicvoid test() {

        //1.建立索引

        IndexWriterConfig config = new  IndexWriterConfig(analyzer);

        IndexWriter iwriter = null;

 

        try {

            iwriter = new IndexWriter(directory, config);

            Document doc = new Document();

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

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

            iwriter.addDocument(doc);

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            if (iwriter != null)

                try {

                    iwriter.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

        }

       

        //2.查询索引

        DirectoryReader ireader = null;

        IndexSearcher isearcher = null;

        try {

            ireader = DirectoryReader.open(directory);

            isearcher = new IndexSearcher(ireader);

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

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

            ScoreDoc[] hits = isearcher.search(query, 10, Sort.INDEXORDER).scoreDocs;

            assertEquals(1, hits.length);

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

            assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ParseException e) {

            e.printStackTrace();

        } finally {

            if (ireader != null)

                try {

                    ireader.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

        }

    }

 

    @After

    publicvoid destroy() {

        if (analyzer != null){

            analyzer.close();

        }

        if (directory != null) {

            try {

                directory.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

}

 

 


以上是关于Lucene QuickStart的主要内容,如果未能解决你的问题,请参考以下文章

Lucene底层原理和优化经验分享-Lucene简介和索引原理

Lucene 实战(第2版)

Lucene学习总结

实战 Lucene,第 1 部分: 初识 Lucene (zhuan)

Lucene建立索引库

Lucene--搜索