⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bbssearchim.java

📁 一个不错的bbs论坛系统.对初学者很有帮助
💻 JAVA
字号:
package com.yhbbs.article.search.biz;

import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.BooleanClause.Occur;

import com.yhbbs.article.search.bean.SearchResultIm;
import com.yhbbs.utils.Constants;
import com.yhbbs.utils.DateUtils;
/**
 * <p>Title:论坛搜索相关操作</p>
 * <li>	论坛搜索的相关操作
 * <p>Company: www.yyhweb.com</p>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YHBBS-2.0
 */
public class BbsSearchIm {
	
	static{ BooleanQuery.setMaxClauseCount(5120); }
	
	private static Analyzer ikAnalyzer = BbsWriterIm.ikAnalyzer;
	// 以时间降序排序
	private static Sort ssort = new Sort(new SortField("posttime", 3, true));
	
	public static IndexSearcher getSearch(){
		try {
			return new IndexSearcher(BbsWriterIm.getIndexPath());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/** 论坛搜索的主要入口
	 * @param keyword 关键字
	 * @param authorpost 作者还是帖子搜索
	 * @param author 作者搜索类型
	 * @param content 帖子搜索类型
	 * @param postdate 搜索范围
	 * @param schfmId 搜索论坛
	 * @param from 搜索起点
	 * @param to 搜索结束位置
	 * @return 搜索结果列表
	 */
	public static SearchResultIm search(String keyword,int authorpost,int author,int content,int postdate,int schfmId,int from,int to){
		if(authorpost==0)
			return searchAuthor(keyword,author,postdate,schfmId,from,to);
		else if(authorpost==1)
			return searchPost(keyword,content,postdate,schfmId,from,to);
		
		return null;
	}
	/** 作者搜索
	 * @param author 作者搜索类型
	 * @param postdate 搜索范围
	 * @param schfmId 搜索论坛
	 * @return 搜索结果列表
	 */
	private synchronized static SearchResultIm searchAuthor(String keyword,int author,int postdate,int schfmId,int from,int to){
		SearchResultIm schRS = new SearchResultIm(from,to);
		IndexSearcher searcher = getSearch();
		if(searcher!=null){
			BooleanQuery booleanQuery = new BooleanQuery();
			try {
				// 0:主题作者 1:回复作者
				Query aQuery = null;
				if(author==0){
					aQuery = new QueryParser("parentId",ikAnalyzer).parse("0");
					booleanQuery.add(aQuery,Occur.MUST);
				}else if(author==1){
					aQuery = new QueryParser("parentId",ikAnalyzer).parse("0");
					booleanQuery.add(aQuery,Occur.MUST_NOT);
				}
				// 搜索日期需要转化
				if(postdate>0){
					Term beginTime = new Term("posttime",getSchStartDate(postdate));
					Term endTime = new Term("posttime",DateUtils.getCurFormatDate(Constants.dateFM));
					RangeQuery timeQuery = new RangeQuery(beginTime, endTime, true);
					booleanQuery.add(timeQuery,Occur.MUST);
				}
				// 论坛Id>0才具体搜索某论坛
				if(schfmId>0){
					Query forumQuery = new QueryParser("forumId",ikAnalyzer).parse(String.valueOf(schfmId));
					booleanQuery.add(forumQuery,Occur.MUST);
				}
				// 作者必须搜索
				Query authorQuery = new QueryParser("author",ikAnalyzer).parse(keyword);
				booleanQuery.add(authorQuery,Occur.MUST);
				
				schRS.setHits(searcher.search(booleanQuery,ssort));
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				closeSearch(searcher);	
			}
		}
		return schRS;
	}
	
	/** 帖子相关搜索
	 * @param content 帖子搜索类型
	 * @param postdate 搜索范围
	 * @param schfmId 搜索论坛
	 * @return 搜索结果列表
	 */
	private synchronized static SearchResultIm searchPost(String keyword,int content,int postdate,int schfmId,int from,int to){
		SearchResultIm schRS = new SearchResultIm(from,to);
		IndexSearcher searcher = getSearch();
		if(searcher!=null){
			BooleanQuery booleanQuery = new BooleanQuery();
			try {
				// 0:搜索标题 1:搜索内容 2:两者都搜索
				Query aQuery = null;
				if(content==0){
					aQuery = new QueryParser("title",ikAnalyzer).parse(keyword);
					booleanQuery.add(aQuery,Occur.MUST);
				}else if(content==1){
					aQuery = new QueryParser("content",ikAnalyzer).parse(keyword);
					booleanQuery.add(aQuery,Occur.MUST);
				}else{
					aQuery = MultiFieldQueryParser.parse(keyword,new String[]{"title","content"},new Occur[]{Occur.SHOULD,Occur.SHOULD},ikAnalyzer);
					booleanQuery.add(aQuery,Occur.MUST);
				}
				// 搜索日期需要转化
				if(postdate>0){
					Term beginTime = new Term("posttime",getSchStartDate(postdate));
					Term endTime = new Term("posttime",DateUtils.getCurFormatDate(Constants.dateFM));
					RangeQuery timeQuery = new RangeQuery(beginTime, endTime, true);
					booleanQuery.add(timeQuery,Occur.MUST);
				}
				// 论坛Id>0才具体搜索某论坛
				if(schfmId>0){
					Query forumQuery = new QueryParser("forumId",ikAnalyzer).parse(String.valueOf(schfmId));
					booleanQuery.add(forumQuery,Occur.MUST);
				}
				
				schRS.setHits(searcher.search(booleanQuery,ssort));
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				closeSearch(searcher);	
			}
		}
		return schRS;
	}
	/** 取得过去days天的日期字符串
	 * @param days 过去天数
	 * @return 过去days天的日期字符串
	 */
	private static String getSchStartDate(int days){
		Date startDate = DateUtils.getPastdayDate(days);
		return DateUtils.getDate2Str(Constants.dateFM, startDate);
	}
	
	private static void closeSearch(Searcher searcher){
		if(searcher!=null){
			try {
				searcher.close();
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -