lucene3.x和之前版本有很大的变动,而lucene4.x版本和lucene3.x也有不小的区别,网上有好多文章,但是差不多都是3.x版本的,在这贴一段代码,
从数据库读取数据后创建索引,然后根据前台的关键字进行查询。
//用于存放建立的索引
String index_Store_Path = "F:\\index";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
Directory directory = null;
//用于将数据库的学生信息创建索引
public void createIndex() throws Exception {
directory = FSDirectory.open(new File(index_Store_Path));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_41,analyzer);
IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
List<Studentinfo> studentInfos = studentService.searchStus();
for(Studentinfo studentInfo : studentInfos) {
Document doc = new Document();
doc.add(new Field("sex",studentInfo.getSex()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("name",studentInfo.getName()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("birthday",studentInfo.getBirthday()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("nativePlace",studentInfo.getNativePlace()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("politicStatus",studentInfo.getPoliticsStatus()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("graduatedDep",studentInfo.getGraduatedDep()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("major",studentInfo.getMajor()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("degreeLevel",studentInfo.getDegreeLevel()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("placeOfWork",studentInfo.getPlaceOfWork()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("administrativeLevel",studentInfo.getAddress()+"",Store.YES,Index.ANALYZED));
doc.add(new Field("address",studentInfo.getAddress()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("postalcode",studentInfo.getPostalcode()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("beOnTheJob",studentInfo.getBeOnTheJob()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("officePhoneNumber",studentInfo.getOfficePhoneNum()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("mobiePhoneNumber",studentInfo.getMobilePhoneNum()+"",Store.YES,Index.NOT_ANALYZED));
doc.add(new Field("email",studentInfo.getEmail()+"",Store.YES,Index.NOT_ANALYZED));
indexWriter.addDocument(doc);
}
indexWriter.close();
}
进行查询:
public String getStudentInfos() throws Exception {
String keyWord = "";
String name = new String(stuMiddel.getName().getBytes("iso-8859-1"),"utf8");
if(!"".equals(name)) {
keyWord += name;
}
String sex = new String(stuMiddel.getSex().getBytes("iso-8859-1"),"utf8");
if(!"".equals(sex)) {
keyWord +=" "+sex;
}
String graduatedDep = new String(stuMiddel.getGraduatedDep().getBytes("iso-8859-1"),"utf8");
if(!"".equals(graduatedDep)) {
keyWord +=" "+graduatedDep;
}
String degreeLevel = new String(stuMiddel.getDegreeLevel().getBytes("iso-8859-1"),"utf8");
if(!"".equals(degreeLevel)) {
keyWord +=" "+degreeLevel;
}
String administrativeLevel = new String(stuMiddel.getAdministrativeLevel().getBytes("iso-8859-1"),"utf8");
if(!"".equals(administrativeLevel)) {
keyWord +=" "+administrativeLevel;
}
String major = new String(stuMiddel.getMajor().getBytes("iso-8859-1"),"utf8");
if(!"".equals(major)) {
keyWord +=" "+major;
}
String placeOfWork = new String(stuMiddel.getPlaceOfWork().getBytes("iso-8859-1"),"utf8");
if(!"".equals(placeOfWork)) {
keyWord +=" "+placeOfWork;
}
String[] fields = new String[]{"name","sex","graduatedDep","administrativeLevel","major","placeOfWork","degreeLevel"};
QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_41,fields,analyzer);
//不加的话默认为or(好像是)
queryParser.setDefaultOperator(Operator.AND);
Query query = queryParser.parse(keyWord);
directory = FSDirectory.open(new File(index_Store_Path));
//获取访问索引的接口,进行搜索
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//TopDocs 搜索返回的结果
TopDocs topDocs = indexSearcher.search(query, 100);//只返回前100条记录
int totalCount = topDocs.totalHits; // 搜索结果总数量
System.out.println("搜索到的结果总数量为:" + totalCount);
ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 搜索的结果列表
stus = new ArrayList<Studentinfo>();
//把搜索结果取出放入到集合中
for(ScoreDoc scoreDoc : scoreDocs) {
int docID = scoreDoc.doc;//当前结果的文档编号
float score = scoreDoc.score;//当前结果的相关度得分
System.out.println("score is : "+score);
Document document = indexSearcher.doc(docID);
Studentinfo studentInfo = new Studentinfo();
studentInfo.setName(document.get("name"));
studentInfo.setAddress(document.get("address"));
studentInfo.setAdministrativeLevel(document.get("administrativeLevel"));
studentInfo.setBirthday(document.get("birthday"));
studentInfo.setDegreeLevel(document.get("degreeLevel"));
studentInfo.setEmail(document.get("email"));
studentInfo.setGraduatedDep(document.get("graduatedDep"));
studentInfo.setJob(document.get("job"));
studentInfo.setMajor(document.get("major"));
studentInfo.setPlaceOfWork(document.get("placeOfWork"));
studentInfo.setMobilePhoneNum(document.get("mobilePhoneNum"));
studentInfo.setNativePlace(document.get("nativePlace"));
studentInfo.setOfficePhoneNum(document.get("officePhoneNum"));
studentInfo.setPostalcode(document.get("postalcode"));
studentInfo.setSex(document.get("sex"));
studentInfo.setBeOnTheJob(document.get("beOnTheJob"));
stus.add(studentInfo);
}
indexReader.close();
return "listStudentInfo";
}
因为资料少,而且刚开始学习lucene,如果有牛人的话,求指点啊.......