PrefixQuery前缀搜索符合的所有放入map

/** PrefixQuery前缀搜索符合的所有放入map
	 *	@param File indexDir,索引目录;
	 *	@param String word 索引关键词前缀
	 *  @return Map<String,Integer>
	 *  @author niewj @since 2011-3-10 */
	public static Map<String,Integer> getPairMap(File indexDir,String field,String word) {
		Map<String,Integer> pairMap=new HashMap<String,Integer>();
		Directory dir=null;
		try {
			dir = new SimpleFSDirectory(indexDir);
			IndexSearcher searcher=new IndexSearcher(dir);
			Term term=new Term(field,word);
			PrefixQuery query=new PrefixQuery(term);
			
			TopDocs topDocs=searcher.search(query,5000);
			System.out.println("total hit:"+topDocs.totalHits);
			ScoreDoc[] docs=topDocs.scoreDocs;
			for(int i=0;i<docs.length;i++){
				Document doc=searcher.doc(docs[i].doc);
				String pair=doc.getField("pairContent").stringValue();
				String value=doc.getField("pairLines").stringValue();
				pairMap.put(pair, Integer.parseInt(value));
				System.out.println(pair+"-"+value);
			}
		} catch (IOException e) {
			e.printStackTrace();	//
		}
		return pairMap;
	}
 

你可能感兴趣的:(query)