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

📄 searchjob.java

📁 java阿里巴巴代码
💻 JAVA
字号:
package com.saas.biz.searchMgr;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import com.saas.biz.commen.config;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

import com.saas.biz.attachMgr.Attachinfo;
import com.saas.biz.commen.commMethodMgr;
import com.saas.sys.buffer.Buffers;
import com.saas.sys.dbm.Dbtable;
import com.saas.sys.exp.SaasApplicationException;
import com.saas.sys.log.Logger;

public class SearchJob {
	
	
	Dbtable tradeQuery;
	
	
	Logger log;
	
	
	Buffers inBuffer;
	
	
	Buffers outBuffer;
	
	
	commMethodMgr commen;
	
	
	ArrayList queryResult = new ArrayList();
	
	
	
	public static void main(String[] args) {

		SearchJob searchjob = new SearchJob();
		try {
			searchjob.queryResult = searchjob.doSearchJob("0139", 0, "通信");
			System.out.println(searchjob.queryResult);
		}
		catch (SaasApplicationException e) {
			searchjob.log.LOG_INFO(e.getMessage());
		}
		
	}
	
	
	public SearchJob() {

		log = new Logger(this);
		tradeQuery = new Dbtable();
		outBuffer = new Buffers();
		commen = new commMethodMgr();
	}
	
	
	public void setTradeQuery(Dbtable tradeQuery) {

		this.tradeQuery = tradeQuery;
	}
	
	
	public Dbtable getTradeQuery() {

		return this.tradeQuery;
	}
	
	
	public void setOutBuffer(Buffers outBuffer) {

		this.outBuffer = outBuffer;
	}
	
	
	public Buffers getOutBuffer() {

		return this.outBuffer;
	}
	
	
	public ArrayList getQueryResult() {

		return this.queryResult;
	}
	
	
	public void setQueryResult(ArrayList queryResult) {

		this.queryResult = queryResult;
	}
	
	
	public void doSearchJob(Buffers inbuffer) {

		this.outBuffer = inbuffer;
		log.LOG_INFO("进入doSearchJob方法...");
		String strTradeTypeCode = inbuffer.getStringWeb("RSRV_STR1");// 归属业务
		String srcType = inbuffer.getStringWeb("RSRV_STR4");// 来源
		int iStart = inbuffer.getInt("START");
		String queries = "";
		if (srcType.equalsIgnoreCase("1")) {
			queries = inbuffer.getStringWeb("RSRV_STR2");// 关键字
		}
		else {
			queries = inbuffer.getString("RSRV_STR2");// 关键字
		}
		try {
			this.queryResult = doSearchJob(strTradeTypeCode, iStart, queries);
		}
		catch (SaasApplicationException e) {
			log.LOG_INFO(e.getMessage());
		}
		this.outBuffer.setString("DISPALY_STYLE", "0");
		log.LOG_INFO("退出doSearchJob方法...");
	}
	
	
	public ArrayList doSearchJob(String strTradeTypeCode, int showNo, String queries) throws SaasApplicationException {

		log.LOG_INFO("进入doSearchJob方法...");
		ArrayList resultList = new ArrayList();
		config configFile= new config();
    configFile.init();
    String rootpath = configFile.getString("mysqlbase.rootpath");
		String index = rootpath+"index/";
		IndexReader reader = null;
		int xfrom = showNo;
		String link = "";
		String keyword = queries;
		
		
		// 0139:客户;0141: 销售;0154:采购;0156:人才;0140:商品;0161:信息;0190:知识库
		try {
			reader = IndexReader.open(index);
			Searcher searcher = new IndexSearcher(reader);
			Analyzer analyzer = new StandardAnalyzer();
			
			BufferedReader in = null;
			Query query1 = null;
			Query query2 = null;
			// Query query = null;
			QueryParser parser = null;
			
			if (strTradeTypeCode.equalsIgnoreCase("0139")) {
				queries = "rage:customerRage AND contents:" + queries + " OR title:" + queries;
				// link = "../fristlink.do?trade_type_code=0134&flag=0&cust_id=";
				link = "../zone_b2b/enterprise/customer/";
				parser = new QueryParser("rage", analyzer);
				query1 = parser.parse("customerRage");
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0141")) {
				queries = "rage:saleRage AND contents:" + queries + " OR title:" + queries;
				link = "../zone_b2b/supply/saleInquiry.jsp?sale_id=";
				parser = new QueryParser("rage", analyzer);
				query1 = parser.parse("saleRage");
				
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0154")) {
				queries = "rage:stockRage AND contents:" + queries + " OR title:" + queries;
				// link = "../stockArticle.do?trade_type_code=0155&stock_id=";
				link = "zone_b2b/stock/stockInquiry.jsp?stock_id=";
				parser = new QueryParser("rage", analyzer);
				query1 = parser.parse("stockRage");
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0156")) {
				queries = "rage:jobRage AND contents:" + queries + " OR title:" + queries;
				// link = "../jobIndex.do?trade_type_code=0160&job_unit=";
				link = "/hr/r/d/hr_r_";
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0140")) {
				queries = "rage:commodityRage AND contents:" + queries + " OR title:" + queries;
				// link = "../Commoditylist.do?trade_type_code=0157&commodity_id=";
				link = "/product/detail/product";
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0411")) {
				queries = "rage:infoRage AND contents:" + queries + " OR title:" + queries;
				link = "../newsdetail.do?trade_type_code=0163&news_id=";
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0161")) {
				queries = "rage:infoRage AND contents:" + queries + " OR title:" + queries;
				link = "../zone_b2b/news/zixun_list_content.jsp?news_id=";
				parser = new QueryParser("rage", analyzer);
				query1 = parser.parse("infoRage");
			}
			else if (strTradeTypeCode.equalsIgnoreCase("0190")) {
				queries = "rage:repositoryRage AND contents:" + queries + " OR title:" + queries;
				link = "../repositoryList.do?trade_type_code=0179&repository_id=";
			}
			// parser = new QueryParser("contents", analyzer);
			// query = parser.parse(queries);
			BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
			query2 = MultiFieldQueryParser.parse(keyword, new String[] { "title", "contents" }, flags, analyzer);
			BooleanQuery bqf = new BooleanQuery();
			log.LOG_INFO("执行BooleanQuery");
			bqf.add(query1, BooleanClause.Occur.MUST);
			bqf.add(query2, BooleanClause.Occur.MUST);
			Hits hits = searcher.search(bqf);
			String hitcount = String.valueOf(hits.length());
			log.LOG_INFO(hitcount);
			/**
			 * if(outBuffer!=null) { this.outBuffer.setString("RSRV_STR3",hitcount); }
			 */
			final int HITS_PER_PAGE = 10;
			
			int end = Math.min(hits.length(), xfrom + HITS_PER_PAGE);
			for (int i = xfrom; i < end; i++) {
				Document doc = hits.doc(i);
				String id = doc.get("id");
				String title = doc.get("title");
				String contents = doc.get("contents");
				contents = contents.replaceAll("&nbsp;", "");
				contents = contents.replaceAll("<[^<>]+>", "");
				if (contents.length() > 50) {
					contents = "    " + commen.splitStr(contents, 50) + "...";
				}
				contents = contents.replaceAll("<A", "");
				contents = contents.replaceAll("<a", "");
				HashMap listMap = new HashMap();
				String links = link + id;
				if (strTradeTypeCode.equalsIgnoreCase("0139")) {
					links = link + id + "/";
				}
				String strImagePath = "";
				listMap.put("title", title);
				listMap.put("ref", contents);
				listMap.put("linkstr", links);
				listMap.put("pic_path", strImagePath);
				listMap.put("count", hitcount);
				resultList.add(listMap);
			}
			reader.close();
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return resultList;
	}
	
	
	
	/**
	 * @param root_id
	 * @return 取出图片
	 * @throws SaasApplicationException
	 */
	public String getImge(String root_id) throws SaasApplicationException {

		Attachinfo attach = new Attachinfo();
		ArrayList list = attach.getAttachInfoByList(root_id);
		String file_path = "";
		if (list != null && list.size() > 0) {
			HashMap map = (HashMap) list.get(0);
			if (map.get("file_path") != null) {
				file_path = map.get("file_path").toString();
				file_path = file_path.substring(23);
			}
		}
		return file_path;
	}
}

⌨️ 快捷键说明

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