读书人

Lucene3.5 兑现创建索引和搜索实例

发布时间: 2012-09-06 10:37:01 作者: rapoo

Lucene3.5 实现创建索引和搜索实例

????????????????

???????最近在做关于大数据量查询,发现Lucene是个不错的选择。关于Luncene3.5的代码在网上不是很多,参考网上一些代码并看API,做出如下代码,有什么问题,给留言。

?

??

?????? 1.数据库作为数据源,创建索引:

??

???????????

 //创建索引public void createIndex(OpenMode  openMode,List<Authors> list){             try {IndexWriter indexWriter=null;try {indexWriter=new LTes ().createIndexWriter(openMode);} catch (Exception e) {e.printStackTrace();}//List<Authors> list=new  PersistenceFacade().query("from Authors where id=300");for(Authors au:list){  Document doc=new Document();          doc.add(new Field("id",au.getId()+"",Field.Store.YES,Field.Index.ANALYZED));          doc.add(new Field("authorName",au.getAuthorName(),Field.Store.YES,Field.Index.ANALYZED));          doc.add(new Field("authorID",au.getAuthorID(),Field.Store.YES,Field.Index.NO));          doc.add(new Field("phone",au.getPhone(),Field.Store.YES,Field.Index.NO));          doc.add(new Field("Address",au.getAddress() ,Field.Store.YES,Field.Index.NO));          doc.add(new Field("City",au.getAuthorID(),Field.Store.YES,Field.Index.NO));          doc.add(new Field("status",au.getPhone(),Field.Store.YES,Field.Index.NO));          doc.add(new Field("zip",au.getPhone(),Field.Store.YES,Field.Index.NO));          //indexWriter.addDocument(doc);  if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)      {  logger.info("OpenModel:CREATE");  indexWriter.addDocument(doc);      }      else      {      logger.info("OpenModel:APPEND");          indexWriter.updateDocument(new Term("id",String.valueOf(au.getId())), doc);      }}indexWriter.close();} catch (CorruptIndexException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}   }   

?

?

???????? 提示:? indexWriter.updateDocument(new Term("id","123"), doc);

?? 表示更新ID为123的索引,如果没有则新增。

??

/** * 创建索引 * @return */public IndexWriter createIndexWriter(OpenMode openMode)throws Exception{//索引存放位置设置Directory dir = FSDirectory.open(new File("E:\\index"));// 索引配置类设置IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35));iwc.setOpenMode(openMode);IndexWriter writer = new IndexWriter(dir, iwc);return writer;}

?

?

?

?? ??

?? 2.检索数据:

?

????

public  List<Authors> search(String queryString) throws Exception{String[] queryFields={"authorName"};List<Authors> list=new ArrayList<Authors>();IndexReader reader=IndexReader.open(FSDirectory.open(new File("E:\\index")));IndexSearcher searcher=new IndexSearcher(reader);QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, queryFields,new StandardAnalyzer(Version.LUCENE_35));Query query = parser.parse(queryString);TopDocs results = searcher.search(query,null,1000);ScoreDoc[] hits=results.scoreDocs;for(ScoreDoc sd:hits){       int docID=sd.doc;       Document doc=searcher.doc(docID);       Authors authors=new Authors();       authors.setId(Integer.valueOf(doc.get("id")));       authors.setAddress(doc.get("Address"));              authors.setAuthorID(doc.get("authorID"));       authors.setAuthorName(doc.get("authorName"));       authors.setCity(doc.get("city"));       authors.setPhone(doc.get("phone"));       authors.setStatus(doc.get("status"));       authors.setZip(doc.get("zip"));       list.add(authors);}return list;//System.out.println("总符合: " + results.totalHits + "条数!");}

?

?

?

???????? 以上是Lucene3.5创建索引和检索索引的方法。


?????????????????????????

??

?

1 楼 649199285 2012-02-03 我想问一下 你这个OpenMode是怎么个用法。 2 楼 SteveLee 2012-02-10 创建索引调用createIndex(OpenMode.APPEND);OpenMode 有两个属性APPEND和CREATE。APPEND表示在原来的基础上增加索引,CREATE表示重新创建索引 。 3 楼 walong2012 2012-06-18 请问可以给完整的实例吗可以部署的,我是刚刚学lucene,想通过成功运行后读源码

读书人网 >编程

热点推荐