lucene数据库查询

首先我们先把库下载了,我这里是lucene-6.3.0版本,所需jar包:
http://download.csdn.net/detail/yabaj/9757979
在数据库中查询指定字段内容,将该条记录输出

public class TestLucene {
    private static ResultSet recrd;
    private static IndexSearcher iSearcher;
    private static Directory ramDirectory = new RAMDirectory();

    /**
     * @param args
     */
    public static void main(String[] args) {
        // MMAnalyzer analyzer=new MMAnalyzer();
        try {

            recrd = getConn(); // 获取数据库的记录集
            // while (recrd.next()) {
            // System.out.println(recrd.getString(1));
            //
            // }

            Date date1 = new Date();
            IndexBuilder(); // 建立索引
            Date date2 = new Date();
            System.out.println("创建索引-----耗时:"
                    + (date2.getTime() - date1.getTime()) + "ms\n");
            BufferedReader bReader = new BufferedReader(new InputStreamReader(
                    System.in));
            String query = bReader.readLine();
            // System.out.println(query);
            TopDocs hits = search(query); // 输入查询内容后,查询
            ScoreDoc[] scoreDocs = hits.scoreDocs;
            for (int i = 0; i < scoreDocs.length; i++) { // 返回查询后结果
                int docId = scoreDocs[i].doc;
                Document document = iSearcher.doc(docId);
                System.out.println(document.get("ID"));
                System.out.println(document.get("Name"));
                System.out.println(document.get("Pass"));
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    public static TopDocs search(String quString) { // 搜索用户输入的字符
        TopDocs hits = null;
        try {
            iSearcher = new IndexSearcher(DirectoryReader.open(ramDirectory));
            QueryParser parser = new QueryParser("Name", getAnalyzer());
            Query query = parser.parse(quString);
            // System.out.println(query.toString());

            hits = iSearcher.search(query, 10);
            return hits;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

    public static void IndexBuilder() throws Exception { // 建立索引
        while (recrd.next()) {

            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
                    new StandardAnalyzer());
            IndexWriter ramWriter = new IndexWriter(ramDirectory,
                    indexWriterConfig);
            Document document = new Document();
            String _id = recrd.getInt("id") + "";
            System.out.println("id=" + _id);
            Field id = new TextField("ID", _id, Field.Store.YES);
            Field name = new TextField("Name", recrd.getString("Name"),
                    Field.Store.YES);
            Field pass = new TextField("Pass", recrd.getString("pass"),
                    Field.Store.YES);

            document.add(id);
            document.add(name);
            document.add(pass);

            ramWriter.addDocument(document);
            ramWriter.close();
            // fWriter.addIndexes(new Directory[] { ramDirectory });

        }
    }

    public static Analyzer getAnalyzer() {
        return new StandardAnalyzer();
    }

    private IndexSearcher getIndexSearcher() throws IOException {
        return new IndexSearcher(DirectoryReader.open(new RAMDirectory()));
    }

    public static ResultSet getConn() { // 建立数据库连接,并返回结果
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // &autoReconnect=true&failOverReadOnly=false&maxReconnects=10
            String connString = "jdbc:mysql://localhost:3306/hibernate?user=root&password=123456";
            Connection conn = DriverManager.getConnection(connString);
            Statement stmt = conn.createStatement();
            String sql = "select * from user";
            ResultSet rs = stmt.executeQuery(sql);
            return rs;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值