1. 程式人生 > >Lucene筆記09-Lucene的搜尋-TermRange等基本搜尋

Lucene筆記09-Lucene的搜尋-TermRange等基本搜尋

一、精確查詢

// 精確查詢,對field域查詢name
public void searchByTerm(String field, String name, int number) {
    IndexSearcher indexSearcher = getIndexSearcher();
    try {
        Query query = new TermQuery(new Term(field, name));
        TopDocs topDocs = indexSearcher.search(query, number);
        System.out.println("一共查詢到:" + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println(document.get("id") + " " + document.get("name") + " " + document.get("email"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (indexSearcher != null) {
            try {
                indexSearcher.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

二、對範圍進行查詢(字串範圍)

// 對範圍進行查詢(字串)
public void searchByTermRange(String field, String start, String end, int number) {
    IndexSearcher indexSearcher = getIndexSearcher();
    try {
        // 對field域查詢,查詢範圍是start到end,包含兩端點
        // 這裡的start和end都是字串型別的,所以查詢數字型別的資料是查不出來的
        // 如果需要根據範圍查詢數字,需要使用NumericRangeQuery類來建立query
        Query query = new TermRangeQuery(field, start, end, true, true);
        TopDocs topDocs = indexSearcher.search(query, number);
        System.out.println("一共查詢到:" + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println(document.get("id") + " " + document.get("name") + " " + document.get("email"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (indexSearcher != null) {
            try {
                indexSearcher.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、對範圍進行查詢(數字)

// 對範圍進行查詢(數字)
public void searchByNumericRange(String field, int start, int end, int number) {
    IndexSearcher indexSearcher = getIndexSearcher();
    try {
        // 對field域查詢,查詢範圍是start到end,型別是數字,包含兩端點
        Query query = NumericRangeQuery.newIntRange(field, start, end, true, true);
        TopDocs topDocs = indexSearcher.search(query, number);
        System.out.println("一共查詢到:" + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            System.out.println(document.get("id") + " " + document.get("name") + " " + document.get("email"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (indexSearcher != null) {
            try {
                indexSearcher.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四、主方法體進行測試

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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import java.io.IOException;

public class SearchUtil {
    private String[] ids = {"1", "2", "3", "4", "5", "6"};
    private String[] emails = {"[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"};
    private String[] contents = {"content 1", "content 2", "content 3", "content 4", "content 5", "content 6"};
    private int[] attachFiles = {1, 2, 3, 4, 5, 6};
    private String[] names = {"qianyi", "zhaoer", "zhangsan", "lisi", "wangwu", "liuliu"};
    private Directory directory;
    private IndexReader indexReader;

    public SearchUtil() {
        directory = new RAMDirectory();
        index();
    }

    public IndexSearcher getIndexSearcher() {
        try {
            indexReader = IndexReader.open(directory);
            if (indexReader == null) {
            } else {
                IndexReader temp = IndexReader.openIfChanged(indexReader);
                if (temp != null) {
                    indexReader.close();
                    indexReader = temp;
                }
            }
            return new IndexSearcher(indexReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void index() {
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
            Document document;
            for (int i = 0; i < ids.length; i++) {
                document = new Document();
                document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.add(new Field("content", contents[i], Field.Store.NO, Field.Index.ANALYZED));
                document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.add(new NumericField("attachFiles", Field.Store.YES, true).setIntValue(attachFiles[i]));
                indexWriter.addDocument(document);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (indexWriter != null) {
                try {
                    indexWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        SearchUtil searchUtil = new SearchUtil();
        searchUtil.searchByTerm("content", "content", 3);
        searchUtil.searchByTermRange("name", "a", "s", 10);
        searchUtil.searchByNumericRange("attachFiles", 1, 10, 5);
    }
}