Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询

最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo。

使用的lucene jar包是3.6版本.

一:建立数据库表,并加上测试数据。数据库表:UserInfo


二:新建java project,并引入lucene jar包。http://lucene.apache.org/


三:为数据库表建立索引及利用索引查数据:


import java.io.File;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


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.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
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.util.Version;
import org.apache.lucene.store.SimpleFSDirectory;
import com.test.dbc.DBConnection;


public class MakeTableIndex {
public static void main(String[] args) throws IOException, SQLException {
String indexDir = "d:\\lucene\\index";
Connection conn;
DBConnection conn1 = new DBConnection();
conn = conn1.getConnection();
PreparedStatement pstmt = conn
.prepareStatement("SELECT * FROM UserInfo");


ResultSet rs = pstmt.executeQuery();
// 为表字段建立索引
Directory dir = new SimpleFSDirectory(new File(indexDir));
// 分词
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,
luceneAnalyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir, iwc);


while (rs.next()) {
System.out.println("username***" + rs.getString(2));
Document doc = new Document();
doc.add(new Field("ID", rs.getString(1), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("UserName", rs.getString(2), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("Hobby", rs.getString(5), Field.Store.YES,
Field.Index.ANALYZED));
indexWriter.addDocument(doc);
}
System.out.println("numDocs" + indexWriter.numDocs());
indexWriter.close();
try {
search();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}

}


// ------------------Search
public static void search() throws Exception {
String  dirPathString="d:\\lucene\\index";
System.out.println(dirPathString);
Directory dir = new SimpleFSDirectory(new File(dirPathString));//查询分析器  路径
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_35, "UserName", new StandardAnalyzer(Version.LUCENE_36));
Query q = parser.parse("张丽");
TopDocs tds = searcher.search(q, 5);
ScoreDoc[] sds = tds.scoreDocs;
for (ScoreDoc sd : sds) {
System.out.println(sd.score);
int docName = sd.doc;
Document doc = searcher.doc(docName);
String UserName = doc.get("UserName");
String Hobby = doc.get("Hobby");
System.out.println("UserName:"+UserName+"---Hobby:"+Hobby);
}
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值