之前的几天发现上不了javaeye,当时很多东西想写一下的,现在不怎么想写了,
看了看lucene ,然后参照网上的一些例子做了稍微的改动:
参考:http://lin23871.iteye.com/blog/347092
package com.chen.lucene.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
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.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
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 LuceneDB {
/**
* @param args
*/
private Connection conn = null;
// 在构造函数中添加
public LuceneDB() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/lucene", "root", "chen"); //
} catch (Exception e) {
// TODO: handle exception
System.out.println("没有连接上数据库");
}
}
// 连接数据库
public Connection getConnection() {
return conn;
}
// 关闭连接
public void close() {
try {
this.conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根
// 调用对象
LuceneDB lucene = new LuceneDB();
// 写入索引
IndexWriter writer = null;
Connection conn = lucene.getConnection();
String sql = "select * from students";
Analyzer analyzer = new StandardAnalyzer();
// writer=new
// IndexWriter("D:\\index3",analyzer,true,IndexWriter.MaxFieldLength.LIMITED);
writer = new IndexWriter("D:\\index3", analyzer, true,
IndexWriter.MaxFieldLength.LIMITED);
PreparedStatement ps = conn.prepareStatement(sql);
// Statement stmt=conn.createStatement();
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
Document doc = new Document();
doc.add(new Field("age", rs.getString("SAGE"), Field.Store.YES,
Field.Index.TOKENIZED));
writer.addDocument(doc);
// System.out.println("rs查询有结果");
}
rs.close();
conn.close();
writer.optimize();
writer.close();
// 寻找索引
Directory dir = FSDirectory.getDirectory("D:\\index3");
IndexSearcher searcher = new IndexSearcher(dir);
QueryParser parser = new QueryParser("age", new StandardAnalyzer());
Query query = parser.parse("19"); // 有待查询的字符串进行Query对象的转换
if (searcher != null) {
Hits hi = searcher.search(query);
if (hi.length() >= 0) {
System.out.println("找到:" + hi.length() + "个关键字");
}
}
TopDocs topDocs = searcher.search(query, 1);
ScoreDoc[] hits = topDocs.scoreDocs;
for (int i = 0; i < hits.length; i++) {
System.out.println("-----Lucene 检索结果为:------");
int DocId = hits[i].doc;
Document doc = searcher.doc(DocId);
System.out.println(doc.get("age"));
}
}
}
数据库:
昨天,一开始我也像参考的例子中查询“张”,但是就是不行,今天我重新打开Eclipse
,之前我用.txt来搜索中文是没有问题的,然后改成了 检索一个数字 Yeah!
结果:
找到:2个关键字
-----Lucene 检索结果为:------
19