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();
		}
	}

}

你可能感兴趣的:(Lucene)