`
cleaneyes
  • 浏览: 342411 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

 

分享到:
评论
2 楼 cleaneyes 2008-05-28  
未进行大数据量的测试.
小的数据量的速度还是很快的.
1 楼 jiqiumin 2008-05-28  
不知道效率怎么样??我笨打算用ajax查缓存的但是暂时还没实施呵呵

相关推荐

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-backward-codecs-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-backward-codecs-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-backward-codecs-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...

    lucene-highlighter-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-highlighter-6.6.0.jar; 赠送原API文档:lucene-highlighter-6.6.0-javadoc.jar; 赠送源代码:lucene-highlighter-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-highlighter-6.6.0.pom;...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...

    lucene-sandbox-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-sandbox-7.2.1.jar; 赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API...

    lucene-memory-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-memory-6.6.0.jar; 赠送原API文档:lucene-memory-6.6.0-javadoc.jar; 赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档...

    lucene-spatial-extras-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-spatial-extras-7.7.0.jar; 赠送原API文档:lucene-spatial-extras-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...

    lucene-backward-codecs-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-backward-codecs-7.7.0.jar; 赠送原API文档:lucene-backward-codecs-7.7.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...

    lucene-analyzers-common-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-analyzers-common-7.7.0.jar; 赠送原API文档:lucene-analyzers-common-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...

Global site tag (gtag.js) - Google Analytics