当前位置: 代码迷 >> Web前端 >> lucene3.0 CRUD范例(四)
  详细解决方案

lucene3.0 CRUD范例(四)

热度:394   发布时间:2012-11-07 09:56:10.0
lucene3.0 CRUD实例(四)
/**
	 * 查询输出所有数据
	 */
	public static void searchAll()
	{
		System.out.println("查询所有开始==============");
		try
		{
			QueryParser queryParser = new QueryParser(Version.LUCENE_30, "userName",   
	                new StandardAnalyzer(Version.LUCENE_30));   
			
			Query query = queryParser.parse("王五 王二");
			queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
			IndexSearcher search = new IndexSearcher(dir); 
			
			TopDocs hits = search.search(query, 20);

			if (hits.scoreDocs.length == 0 || hits == null)
			{
				System.out.println(" 没有记录... 查询结束\n");
				return;
			}
			for (int i = 0; i < hits.scoreDocs.length; i++)
			{
				ScoreDoc sdoc = hits.scoreDocs[i];   
	            Document doc = search.doc(sdoc.doc); 
				
	            System.out.println("own id     = " + doc.get("id"));
				System.out.println("userName   = " + doc.get("userName"));
				System.out.println("come from  = " + doc.get("comefrom"));
			}
		} 
		catch (Exception e)
		{
			LOGGER.info(e.getMessage());
		}
		System.out.println("查询所有结束==============\n");
	}
	
	/**
	 * 根据ID进行删除
	 */
	public static void deleteIndex()
	{
		try
		{
			IndexWriter write = new IndexWriter(dir, new StandardAnalyzer(
					Version.LUCENE_30), true,
					IndexWriter.MaxFieldLength.UNLIMITED);
			Term term = new Term("id", "123456");
			write.deleteDocuments(term);
			write.commit();
			write.close();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
  相关解决方案