Lucene笔记09-Lucene的搜索-TermRange等基本搜索

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36059561/article/details/83216582

一、精确查询

// 精确查询,对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);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36059561/article/details/83216582