1. 程式人生 > >Lucene筆記12-Lucene的搜尋-複習和再查詢分頁搜尋

Lucene筆記12-Lucene的搜尋-複習和再查詢分頁搜尋

一、Lucene的分頁搜尋

Lucene的分頁不像資料庫中的limit的方式,而是提供了一種“再查詢”的方式。什麼是“再查詢”呢?就是第一次把所有的資料都取出來,第二次查詢再根據需求,從第幾條取到第幾條,分兩步進行查詢,所以叫“再查詢”。

二、測試程式碼

package com.wsy;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.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;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileIndexUtils {
    private static Directory directory;
    private static IndexReader indexReader;

    static {
        try {
            directory = FSDirectory.open(new File("E:\\Lucene\\IndexLibrary"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public FileIndexUtils() {
        try {
            indexReader = IndexReader.open(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void index(boolean update) {
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            if (update) {
                indexWriter.deleteAll();
            }
            File[] files = new File("E:\\Lucene\\SearchSource").listFiles();
            for (File file : files) {
                Document document = new Document();
                document.add(new Field("content", new FileReader(file)));
                document.add(new Field("fileName", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new NumericField("date", Field.Store.YES, true).setLongValue(file.lastModified()));
                document.add(new NumericField("size", Field.Store.YES, true).setIntValue((int) (file.length() / 1024)));
                indexWriter.addDocument(document);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (indexWriter != null) {
                try {
                    indexWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void searchPage(String queryString, int pageIndex, int pageSize) {
        try {
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            QueryParser queryParser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
            Query query = queryParser.parse(queryString);
            TopDocs topDocs = indexSearcher.search(query, pageIndex * pageSize);
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            // 分頁查詢
            for (int i = (pageIndex - 1) * pageSize; i < pageIndex * pageSize; i++) {
                Document document = indexSearcher.doc(scoreDocs[i].doc);
                System.out.println(scoreDocs[i].doc + ":" + document.get("path") + " " + document.get("fileName"));
            }
            System.out.println("-------------------------------------------------");
            // 不分頁查詢
            for (int i = 0; i < scoreDocs.length; i++) {
                Document document = indexSearcher.doc(scoreDocs[i].doc);
                System.out.println(scoreDocs[i].doc + ":" + document.get("path") + " " + document.get("fileName"));
            }
            indexSearcher.close();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FileIndexUtils.index(true);
        FileIndexUtils fileIndexUtils = new FileIndexUtils();
        fileIndexUtils.searchPage("java", 2, 3);
    }
}

在searchPage()裡面,我們對分頁和不分頁都做了查詢,用於對比,可以知道分頁結果正確。