1. 程式人生 > >Lucene全文檢索引擎

Lucene全文檢索引擎

getname 通過 nal dem 檢索 數據庫 project cep 關閉

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
  <groupId>demo.lucene</groupId>
  <artifactId>Lucene01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build/>

  <dependencies>
    <!-- lucene核心包 -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- lucene查詢解析包 -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- lucene解析器包 -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>5.3.1</version>
    </dependency>
  </dependencies>
</project>

  

import java.io.File;
import java.io.FileReader;
import java.nio.file.Paths;

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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * 建立索引的類
 * @author Ni Shengwu
 *
 */
public class Indexer {

    private IndexWriter writer; //寫索引實例

    //構造方法,實例化IndexWriter
    public Indexer(String indexDir) throws Exception {
        Directory dir = FSDirectory.open(Paths.get(indexDir));
        Analyzer analyzer = new StandardAnalyzer(); //標準分詞器,會自動去掉空格啊,is a the等單詞
        IndexWriterConfig config = new IndexWriterConfig(analyzer); //將標準分詞器配到寫索引的配置中
        writer = new IndexWriter(dir, config); //實例化寫索引對象
    }
    //關閉寫索引
    public void close() throws Exception {
        writer.close();
    }
    //索引指定目錄下的所有文件
    public int indexAll(String dataDir) throws Exception {
        File[] files = new File(dataDir).listFiles(); //獲取該路徑下的所有文件
        for(File file : files) {
            indexFile(file); //調用下面的indexFile方法,對每個文件進行索引
        }
        return writer.numDocs(); //返回索引的文件數
    }
    //索引指定的文件
    private void indexFile(File file) throws Exception {
        System.out.println("索引文件的路徑:" + file.getCanonicalPath());
        Document doc = getDocument(file); //獲取該文件的document
        writer.addDocument(doc); //調用下面的getDocument方法,將doc添加到索引中
    }
    //獲取文檔,文檔裏再設置每個字段,就類似於數據庫中的一行記錄
    private Document getDocument(File file) throws Exception{
        Document doc = new Document();
        //添加字段
        doc.add(new TextField("contents", new FileReader(file))); //添加內容
        doc.add(new TextField("fileName", file.getName(), Field.Store.YES)); //添加文件名,並把這個字段存到索引文件裏
        doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES)); //添加文件路徑
        return doc;
    }
    public static void main(String[] args) {
        String indexDir = "D:\\lucene"; //將索引保存到的路徑
        String dataDir = "D:\\lucene\\data"; //需要索引的文件數據存放的目錄
        Indexer indexer = null;
        int indexedNum = 0;
        long startTime = System.currentTimeMillis(); //記錄索引開始時間
        try {
            indexer = new Indexer(indexDir);
            indexedNum = indexer.indexAll(dataDir);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                indexer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis(); //記錄索引結束時間
        System.out.println("索引耗時" + (endTime-startTime) + "毫秒");
        System.out.println("共索引了" + indexedNum + "個文件");
    }
}

  

import java.nio.file.Paths;
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.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {
	
	public static void search(String indexDir, String q) throws Exception {

        Directory dir = FSDirectory.open(Paths.get(indexDir)); //獲取要查詢的路徑,也就是索引所在的位置
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(); //標準分詞器,會自動去掉空格啊,is a the等單詞
        QueryParser parser = new QueryParser("contents", analyzer); //查詢解析器
        Query query = parser.parse(q); //通過解析要查詢的String,獲取查詢對象

        long startTime = System.currentTimeMillis(); //記錄索引開始時間
        TopDocs docs = searcher.search(query, 10);//開始查詢,查詢前10條數據,將記錄保存在docs中
        long endTime = System.currentTimeMillis(); //記錄索引結束時間
        System.out.println("匹配" + q + "共耗時" + (endTime-startTime) + "毫秒");
        System.out.println("查詢到" + docs.totalHits + "條記錄");

        for(ScoreDoc scoreDoc : docs.scoreDocs) { //取出每條查詢結果
            Document doc = searcher.doc(scoreDoc.doc); //scoreDoc.doc相當於docID,根據這個docID來獲取文檔
            System.out.println(doc.get("fullPath")); //fullPath是剛剛建立索引的時候我們定義的一個字段
        }
        reader.close();
    }
    public static void main(String[] args) {
        String indexDir = "D:\\lucene";
        String q = "generate-maven-artifacts"; //查詢這個字符串
        try {
            search(indexDir, q);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  

pom.xml

Lucene全文檢索引擎