Lucene實戰開發手記(七)--- 關鍵詞提示

	
	/**
	 * 關鍵詞提醒
	 * @param keyword
	 * @return
	 */
	public List remindKeyword(String keyword){
		List keywordList = new ArrayList();
		IndexReader indexReader;
		try {
			File indexDir = new File(INDEX_STORE_PATH);
			if (indexDir.exists()){
				indexReader = IndexReader.open(INDEX_STORE_PATH);
				
				//以輸入的文字開頭,搜索contents的關鍵詞
				Term sterm = new Term("contents",keyword);				
		        TermEnum termEnum = indexReader.terms(sterm);
		       
		        while (true){
		        	Term term = termEnum.term();		        	
		        	if (term == null || (!term.text().startsWith(keyword))){
		        		break;
		        	}
		        	//關鍵詞出現的位置
		        	TermPositions termPositions = indexReader.termPositions(term);	
		        	//關鍵詞出現的頻率
		        	int frq = termPositions.freq();
		        	while (termPositions.next()){
		        		frq = frq + termPositions.freq();
		           	}		
		        	if (frq >0){
		        		Map keyElt = new HashMap();
			        	keyElt.put("KEY", term.text());
			        	keyElt.put("VALUE", String.valueOf(frq));		
			        	//System.out.println(term.text()+":"+frq);
			        	keywordList.add(keyElt);
		        	}        			        	
		        	termEnum.next();
		        }		        
		        
		        //	  以輸入的文字開頭,搜索topic的關鍵詞
		        List keywordList2 = new ArrayList();
				sterm = new Term("topic",keyword);				
		        termEnum = indexReader.terms(sterm);
		       
		        while (true){
		        	Term term = termEnum.term();		        	
		        	if (term == null || (!term.text().startsWith(keyword))){
		        		break;
		        	}
		        	//關鍵詞出現的位置
		        	TermPositions termPositions = indexReader.termPositions(term);	
		        	//關鍵詞出現的頻率
		        	int frq = termPositions.freq();
		        	while (termPositions.next()){
		        		frq = frq + termPositions.freq();
		           	}		
		        	if (frq > 0){
		        		Map keyElt = new HashMap();
			        	keyElt.put("KEY", term.text());
			        	keyElt.put("VALUE", String.valueOf(frq));		
			        	//System.out.println(term.text()+":"+frq);
			        	keywordList2.add(keyElt);
		        	}        			        	
		        	termEnum.next();
		        }		        
		        
		        //合並相同的關鍵詞
		        if (keywordList.size() >0 && keywordList2.size()>0){
		        	int i=0;
			        int j=0;
			        Map elt1 =  (Map)keywordList.get(0);
			        Map elt2 =  (Map)keywordList2.get(0);
			        while (true){		        	
			        	String text1 = (String)elt1.get("KEY");
			        	String text2 = (String)elt2.get("KEY");
			        	int compare = text1.compareTo(text2);
			        	if (compare == 0){
			        		int frq = Integer.valueOf((String)elt1.get("VALUE")) + Integer.valueOf((String)elt2.get("VALUE"));
			        		elt1.put("VALUE", String.valueOf(frq));
			        		keywordList2.remove(j);
			        		if(++j < keywordList2.size() && ++i < keywordList.size()){
			        			elt1 = (Map)keywordList.get(i);
				        		elt2 = (Map)keywordList2.get(j);
			        		}
			        		else{
			        			break;
			        		}			        		
			        	}
			        	else if(compare > 0){
			        		if (++j < keywordList2.size()){
			        			elt2 = (Map)keywordList2.get(j);
			        		}
			        		else{
			        			break;
			        		}
			        	}
			        	else {
			        		if (++i < keywordList.size()){
			        			elt1 = (Map)keywordList.get(i);
			        		}
			        		else{
			        			break;
			        		}
			        	}
			        }			        
		        }
		        keywordList.addAll(keywordList2);
				indexReader.close();					
			}			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new LuceneException();
		}
		
		//依出現次數排序
		Comparator<Map> itemComparator = new Comparator<Map>()
		{ 
			public int compare(Map a, Map b)
			{ 
				return Integer.valueOf((String)a.get("VALUE")).intValue() - Integer.valueOf((String)b.get("VALUE")).intValue();
			}
		};
		Collections.sort(keywordList, Collections.reverseOrder(itemComparator));
		
		int maxSize = keywordList.size();
		if (maxSize > 10) maxSize = 10;
		keywordList = keywordList.subList(0, maxSize);
		
		return keywordList;
	}

 

 function getAlert(tagetpara,e){
	 	alertObj = tagetpara;
	 	alertEvent = e;
	 	//alert(tagetpara.name);
  		var keyword = tagetpara.value;   
  		if (keyword != ''){
  			AjaxService.remindKeyword(keyword,refreshKeywordList);  		
  			return !showPopup(alertObj.name,'USER_ALERT_DIV',event, alertEvent);   			
  		}  				
    }    
    
    function refreshKeywordList(data){
    	reply01(data);    		
    }
    
    function getsearchpara(data,indexnum){ 
		alertObj.value=data[indexnum]['KEY'];
		hideCurrentPopup();
	} 
    
    var reply01 = function(data){
   		dwr.util.removeAllRows(useralert);
 	    dwr.util.addRows(
 	    	useralert, data, cellFunctions, {			
			  rowCreator:function(options) {
			    var row = document.createElement("tr");
			    row.className = "table_row_single";
			    row.onclick=function (){ 
				    getsearchpara(data,this.rowIndex);
			    };
			    row.onmouseover=function (){ 
				    onMouseOver(this);
			    };
			    row.onmouseout=function (){ 
				    onMouseOut(this,'table_row_single');
			    };
			    return row;
			  },
			  cellCreator:function(options) {
				var td = document.createElement("td");				
    			td.className="table_list_column";       			
			    return td;
			  }
		  	}		  	
	  	);
    }
    
    var cellFunctions = [
	  function(keyword) { return keyword.KEY; },
	  function(keyword) { return keyword.VALUE;}
	];

 

 

 <div CLASS="bubble_panel01"  id="USER_ALERT_DIV" STYLE="WIDTH:200px"	onClick="event.cancelBubble = true;">
			<TABLE ID="T1180684588156_TABLE" CLASS="table_bg" STYLE="MARGIN-BOTTOM:16px" STYLE="WIDTH:200px"  BORDER="0" CELLPADDING="0" CELLSPACING="0">
		
			<TBODY ID="useralert">
			</TBODY>
			</TABLE>
	</div>

 

.bubble_panel01 {
	BORDER-RIGHT: black 1px solid; 

	BORDER-TOP: black 1px solid; 

	VISIBILITY: hidden; 

	BORDER-LEFT: black 1px solid; 
	
	BORDER-BOTTOM: black 1px solid; 
	WIDTH: 200px; 
	POSITION: absolute; 
	BACKGROUND-COLOR: white; 
	LAYER-BACKGROUND-COLOR: yellow
}
.table_row_single01{
	background-color: #FFFFFF;
	line-height: 20px;
	onmouseover: expression(onmouseover=function (){this.style.backgroundColor ='#FFFF99'});
	onmouseout: expression(onmouseout=function (){this.style.backgroundColor ='#FFFFFF'});
	
}
.table_list_column01 {
	font-family: Arial;
	font-size: 11px;
	text-decoration: none;
	padding-left: 4px;
	padding-right: 4px;
	line-height: 20px;	
}

 

你可能感兴趣的:(Ajax,DWR,Lucene,J#)