Lucene入门实例

来源:互联网 发布:科比职业生涯数据 编辑:程序博客网 时间:2024/06/17 15:51

在应用程序中使用Lucene的方法

1、创建一个Document对象,并添加Field。
2、创建一个IndexWrite,并通过其addDocument()方法把上一步创建的Document对象添加到里面。
3、调用QueryParser.parse(String keywords)方法来创建一个查询对象Query。
4、创建一个IndexSearcher,并把上一步的Query对象传入IndexSearcher的search()方法进行查询。
查看官方说明:http://lucene.apache.org/core/4_0_0/core/overview-summary.html

简单的实例

这个简单的实例包括3个类:常量类constants.java、索引建立类LuceneIndex.java、搜索类SearchIndex.java。

Constants.java

package com.lixing;
public class Constants {
    public final static String INDEX_FILE_PATH = "g:\\lucene\\test";    //索引的文件的存放路径
    public final static String INDEX_STORE_PATH = "g:\\lucene\\index";    //索引的存放位置
}

LuceneIndex.java

package com.lixing;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
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.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneIndex {
    // 索引器
    private IndexWriter writer = null;
    public LuceneIndex() {
        try {
            // 索引文件的保存位置
            Directory dir = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
            // 分析器
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
            // 配置类
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
            // 创建模式,(添加模式:OpenMode.CREATE_OR_APPEND)
            iwc.setOpenMode(OpenMode.CREATE);
            writer = new IndexWriter(dir, iwc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 将要建立索引的文件构造成一个Document对象,并添加一个域"content"
    private Document getDocument(File f) throws Exception {
        Document doc = new Document();
        FileInputStream is = new FileInputStream(f);
        Reader reader = new BufferedReader(new InputStreamReader(is));
        // 字符串StringField LongField TextField
        Field pathField = new StringField("path", f.getAbsolutePath(), Field.Store.YES);
        Field contenField = new TextField("contents", reader);
        // 添加字段
        doc.add(contenField);
        doc.add(pathField);
        return doc;
    }
    public void writeToIndex() throws Exception {
        File folder = new File(Constants.INDEX_FILE_PATH);
        if (folder.isDirectory()) {
            String[] files = folder.list();
            for (int i = 0; i < files.length; i++) {
                File file = new File(folder, files[i]);
                Document doc = getDocument(file);
                System.out.println("正在建立索引 : " + file + "");
                writer.addDocument(doc);
            }
        }
    }
    public void close() throws Exception {
        writer.close();
    }
    public static void main(String[] args) throws Exception {
        // 声明一个对象
        LuceneIndex indexer = new LuceneIndex();
        // 建立索引
        Date start = new Date();
        indexer.writeToIndex();
        Date end = new Date();
        System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");
        indexer.close();
    }
}

LuceneSearch.java

package com.lixing;
import java.io.File;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneSearch {
    // 声明一个IndexSearcher对象
    private IndexSearcher searcher = null;
    // 声明一个Query对象
    private Query query = null;
    private String field = "contents";
    public LuceneSearch() {
        try {
            IndexReader reader = DirectoryReader.open(FSDirectory
                    .open(new File(Constants.INDEX_STORE_PATH)));
            searcher = new IndexSearcher(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 返回查询结果
    public final TopDocs search(String keyword) {
        System.out.println("正在检索关键字 : " + keyword);
        try {
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
            QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
            // 将关键字包装成Query对象
            query = parser.parse(keyword);
            Date start = new Date();
            TopDocs results = searcher.search(query, 5 * 2);
            Date end = new Date();
            System.out.println("检索完成,用时" + (end.getTime() - start.getTime()) + "毫秒");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 打印结果
    public void printResult(TopDocs results) {
        ScoreDoc[] h = results.scoreDocs;
        if (h.length == 0) {
            System.out.println("对不起,没有找到您要的结果。");
        } else {
            for (int i = 0; i < h.length; i++) {
                try {
                    Document doc = searcher.doc(h[i].doc);
                    System.out.print("这是第" + i + "个检索到的结果,文件名为:");
                    System.out.println(doc.get("path"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("--------------------------");
    }
    public static void main(String[] args) throws Exception {
        LuceneSearch test = new LuceneSearch();
        TopDocs h = null;
        h = test.search("hello");
        test.printResult(h);
        h = test.search("lucene");
        test.printResult(h);
        h = test.search("hi");
        test.printResult(h);
    }
}

运行结果

待建立索引目录


运行LuceneIndex.java


生成索引目录


运行LuceneSearch.java


原创粉丝点击