java教程

java lucene分页并高亮显示 带多字段搜索的例子

位置:首页 > java教程 > java技巧,2018-01-04 12:59
这是java lucene分页并高亮显示 带多字段搜索的例子,根据搜索的句子进行分词再搜索。简单实用
public class Lucenepage {
	/**
lucenepath,索引存放的路径,color为true时高亮显示, curtotal为显示的页码个数,如首页 1 2 3 4 尾页,leitype为其它字段限制搜索
* 实现分页的功能  
	 * @param stringQuery查询的关键字
	 * @param currentPage 当前的页数
	 * @return 记录数
	 */
	public Luceneben queryByPage(String lucenepath,String stringQuery,boolean color,int currentPage,int pageSize,int curtotal,Integer leitype){
		File lf=new File(lucenepath);
		
		
		if(lf.exists()){
			IKAnalyzer analyzer = new IKAnalyzer();
			//对搜索词分词
			String [] stringQueryarry=getWords(stringQuery,analyzer);
			int len=stringQueryarry.length;
			if(len>0){
			Occur[] occurs = new Occur[stringQueryarry.length];
			String[] fields = new String[stringQueryarry.length];
			int at = 0;
			for (at = 0; at < stringQueryarry.length; at++) {
				occurs[at] = Occur.SHOULD;
				fields[at] = "title";
			}
			
			//搜索词分词
			
			
		Luceneben lu=new Luceneben();
		List<Xinwen> goodsList=new ArrayList<Xinwen>();
		IndexSearcher indexSearcher=null;
		IndexReader reader =null;
		Integer totalcount=0;
		Integer totalPage=0;
		try {
			 reader = IndexReader.open(FSDirectory.open(lf));
			indexSearcher= new IndexSearcher(reader);
			BooleanQuery booleanq=new BooleanQuery();;
			//QueryParser paser1=new QueryParser(Version.LUCENE_CURRENT,"title",analyzer);
			//Query titlequery=paser1.parse(stringQuery);
			Query titlequery=MultiFieldQueryParser.parse(Version.LUCENE_CURRENT,stringQueryarry,fields, occurs, analyzer);
			booleanq.add(titlequery,BooleanClause.Occur.MUST);
			if(leitype>0){
			QueryParser paser2=new QueryParser(Version.LUCENE_CURRENT,"leitype",analyzer);
			Query leitypequery=paser2.parse(leitype.toString());
			booleanq.add(leitypequery,BooleanClause.Occur.MUST);
			}
			TopDocs topDocs = indexSearcher.search(booleanq,(currentPage-1)*pageSize+pageSize);
			
			totalcount=topDocs.totalHits;
//System.out.println("真正命中的结果数:" + topDocs.totalHits);
			
			if(topDocs.totalHits%pageSize!=0){
				totalPage=topDocs.totalHits/pageSize+1;
			}else{
				totalPage=topDocs.totalHits/pageSize;
			}
		//	System.out.println("通过系统的总结果数/每页显示的数量=总页数" + totalPage);
			
			// 返回的是符合条件的文档编号,并不是文档本事
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			
			  //此处加入的是搜索结果的高亮部分
			Highlighter highlighter = null;
			if(color){
			SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color=red>","</font>"); //如果不指定参数的话,默认是加粗,即<b><b/>
            QueryScorer scorer = new QueryScorer(titlequery);//计算得分,会初始化一个查询结果最高的得分
            Fragmenter fragmenter = new SimpleSpanFragmenter(scorer); //根据这个得分计算出一个片段
            highlighter = new Highlighter(simpleHTMLFormatter, scorer);
            highlighter.setTextFragmenter(fragmenter); //设置一下要显示的片段
			}
			// 去期望值  和实际值的 最小值
			//System.out.println("真正查询出来的数组的长度:" + scoreDocs.length);
			
			for(int i=(currentPage-1)*pageSize;i<scoreDocs.length;i++){
				ScoreDoc scoreDoc=scoreDocs[i];
				//System.out.println("存储了命中率积分:" + scoreDoc.score);
				//System.out.println("存储的是文档编号:" + scoreDoc.doc);
				// 第二次查询: 通过文档的编号,查询真正的文档信息
				Document document=indexSearcher.doc(scoreDoc.doc);
				
				Xinwen book = new Xinwen();
				
		if(color){
				//显示高亮部分
                if(document.get("title") != null) {
                	// 高亮关键字    
                    try {  
                    	TokenStream tokenStream = analyzer.tokenStream("title", new StringReader( document.get("title")));  
                     //String highlightStr = highlighter.getBestFragment(tokenStream, document.get("title"));  
                     book.setTitle(highlighter.getBestFragment(tokenStream, document.get("title")));
                     // System.out.println("高亮关键字  "+highlightStr);  
                      tokenStream.close();
                  } catch (InvalidTokenOffsetsException e) {  
                      // TODO Auto-generated catch block  
                      e.printStackTrace();  
                  }  
                }
		}
		else{
			 book.setTitle(document.get("title"));
		}
				book.setId(document.get("id"));
				book.setLeitype(Integer.parseInt(document.get("leitype")));
				book.setUrl(document.get("url"));
				goodsList.add(book);
				
				//goodsList.add(DocumentUtil.documentToGoods(document));
			}
			
			
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new RuntimeException(e);
		}finally{
			try {
				
				indexSearcher.close();
				analyzer.close();
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		
		int beginindex = 0,endindex=0;
		if (curtotal % 2 == 1) {

			beginindex = currentPage - (curtotal - 1) / 2;
			endindex = currentPage + (curtotal - 1) / 2;

			if (beginindex < 1 && totalPage <= curtotal) {
				beginindex = 1;
				endindex = totalPage;
			}

			if (beginindex < 1 && totalPage > curtotal) {
				beginindex = 1;
				endindex = curtotal;
			}

			if (endindex > totalPage) {
				beginindex = currentPage - (curtotal - (totalPage - currentPage)) + 1;
				if (beginindex < 1) {
					beginindex = 1;
				}
				endindex = totalPage;
			}
		}

		if (curtotal % 2 == 0) {

			beginindex = currentPage - (curtotal / 2 - 1);
			endindex = currentPage + curtotal / 2;

			if (beginindex < 1 && totalPage <= curtotal) {
				beginindex = 1;
				endindex = totalPage;
			}

			if (beginindex < 1 && totalPage > curtotal) {
				beginindex = 1;
				endindex = curtotal;
			}

			if (endindex > totalPage) {
				beginindex = currentPage - (curtotal - (totalPage - currentPage)) + 1;
				if (beginindex < 1) {
					beginindex = 1;
				}
				endindex = totalPage;
			}
		}
		
		
		
		
		lu.setList(goodsList);
		lu.setBeginindex(beginindex);
		lu.setEndindex(endindex);
		lu.setTotalpage(totalPage);
		lu.setTotalcount(totalcount);
		return lu;
			}
			else{
				return null;
			}
		}
		else{
			return null;
		}
	}
	
	
	
	public static String[] getWords(String str,IKAnalyzer analyzer){  
	    TokenStream stream = null;
	    StringBuffer fenstr=new StringBuffer();
	    try {  
	        stream = analyzer.tokenStream("content", new StringReader(str));  
	        CharTermAttribute attr = stream.addAttribute(CharTermAttribute.class);  
	        stream.reset();  
	        while(stream.incrementToken()){  
	            fenstr.append(attr).append(",");
	        }  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }finally{  
	        if(stream != null){  
	            try {  
	                stream.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	    String res=fenstr.toString();
	    if(res!=null&&res.endsWith(","));
	    res=res.substring(0,res.length()-1);
	    //System.out.println(res);
	    return res.split(",");
	} 

	
} 


TAGS:java 字符串中提取数字

猜你喜欢


NewHot手机版