lucene2.4.1 搜索文件

雖然lucene3.0都出來了,但是相比較lucene2.4.1的文檔更全面一些,所有現在還是用lucene2.4.1

創建lucene的類

/**
 *類說明
 *@author kaowww153
 *@version 1.0 創建時間: Apr 27, 2010 11:17:49 AM
 **/
@Service
public class LuceneTestService {
	@Autowired
	private UserDao userDao;
	
	/**
	 * 方法描述:測試數據
	 * @return
	 */
	public List<User> getUsers(){
		List<User> users = userDao.findAll();
		return users;
	}
	
	/**
	 * 方法描述:
	 */
	public void getLuceneIndex(){
		List<User> users = getUsers();
		IndexWriter indexWriter;
		try {
			//構造函數參數:索引目錄,分詞類型,是否創建目錄,limited 字段默認最大長度10000 unlimited 沒有長度限制 
			indexWriter = new IndexWriter("d:/index/ii",new StandardAnalyzer(),true,MaxFieldLength.LIMITED);
			//遍歷user放到document裏面
			Document document = null;
			for(User user : users){
				document =new Document();
				
				//字段名字,字段值,字段是否保存到index,是否對字段進行分詞
				document.add(new Field("uid", user.getUid().toString(), Field.Store.YES, Field.Index.ANALYZED));
				document.add(new Field("username", user.getUsername(), Field.Store.YES, Field.Index.ANALYZED));
				indexWriter.addDocument(document);
			}
						
			indexWriter.optimize();
			indexWriter.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 方法描述:得到搜索lucene的值
	 * @param queryName
	 * @return
	 */
	public ScoreDoc[] getResultByLucene(String queryName) {
		getLuceneIndex();
		IndexSearcher indexSearcher;
		try {
				File file = new File("d:/index/ii");
				FSDirectory fsdir = FSDirectory.getDirectory(file);
				indexSearcher = new IndexSearcher(fsdir);
				//字段是否必須出現
				Occur[] flags = {BooleanClause.Occur.SHOULD};
				//可以選擇多個搜索的字段
				Query query = MultiFieldQueryParser.parse(queryName,new  String[]{"username"}, flags, new StandardAnalyzer());
			//	QueryParser queryParser = new QueryParser("username",new PaodingAnalyzer());
			//	Query query = queryParser.parse(queryName);
				TopDocCollector collector = new TopDocCollector(10);  
				indexSearcher.search(query,collector); 
				//符合關鍵字的document
				ScoreDoc[] hits = collector.topDocs().scoreDocs; 
				return hits;
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
}

 採用junit測試

@ContextConfiguration(locations = {"/applicationContext*.xml"})
public class UserServiceTest extends AbstractTransactionalJUnit4SpringContextTests {

	
	@Autowired
	private LuceneTestService luceneService;
	@Test
	public void test() {
		ScoreDoc[] hits = luceneService.getResultByLucene("qeqw 111");
		System.out.println(hits.length);	
	}
	
}
房改房

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章