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创建索引和检索索引的方法。


                         

  

 

你可能感兴趣的:(lucene3.5)