Solr数据初始化及查询高亮显示

Solr数据初始化:

    Controller

  @ResponseBody
    @RequestMapping("/initSolr")
    public String init() throws Exception {
    	try {
			videoService.Initialization();
			return JSONResult.create("初始化成功...").toJSON();
		} catch (Exception e) {
			return JSONResult.failCreate("初始化失败...", "").toJSON();
		}
    }

service

@Override
	public void Initialization() throws SolrServerException, IOException {
		List
public void saveVideoSolr(List
/**
	 * 根据任务的大小,获取每个任务的执行数量
	 * @param size   数据集合的大小  
	 * @return
	 */
	private Integer getEachTaskCount(Integer size) {
		// 一个线程处理多少条数据,默认333
		Integer count =333; 
		// 根据数据量确定一个线程处理多少条数据
		if( size >= 1 && size < 100) {
			count = 33;
		}else if(size >= 100 && size < 500) {
			count = 53;
		}else if(size >= 500 && size < 1000) {
			count = 133;
		}else if(size >= 1000 && size < 10000) {
			count = 333;
		}else if(size >= 10000) {
			count = 1333;
		}
		return count;
	}
/**
	 * 获取每个任务的执行数据(开始和结束)
	 * @param runThreadSize  执行线程数
	 * @param count          每个任务的执行数量
	 * @param list           源数据集
	 * @param i              i
	 * @return
	 */
	private List getEachTaskStartAndEnd(Integer runThreadSize,Integer count,List list,Integer i ) {
		// 存放每个线程的执行数据
		List

initTask

/**
 * 初始化Solr任务
 * @author reyco
 *
 */
public class InitVideoSolrTask implements Runnable {
	
	private List
/**
	 * 初始化Solr
	 * 
	 * @param list
	 */
	@Override
	public void initSolr(List
@Override
	public void BatchInsertSolr(List
@Override
	public SolrInputDocument getSolrInputDocument(Video video) {
		SolrInputDocument doc = new SolrInputDocument();
		doc.setField("id", video.getId().toString());
		doc.setField("video_name", video.getName());
		doc.setField("video_categoryId", video.getCategoryId().toString());
		doc.setField("video_vipId", video.getVipId().toString());
		doc.setField("video_accountId", video.getAccountId().toString());
		doc.setField("video_recordPath", video.getRecordPath());
		doc.setField("video_hitQuantity", video.getHitQuantity().toString());
		doc.setField("video_status", video.getStatus().toString());
		doc.setField("video_desc", video.getDesc());
		doc.setField("video_gmtCreate", DateUtil.formatDateTime(video.getGmtCreate()));
		doc.setField("video_gmtModified", DateUtil.formatDateTime(video.getGmtModified()));
		return doc;
	}

Solr查询及高亮显示:

 @ResponseBody
    @RequestMapping("/solrPage")
    public String searchPage(Integer pageNo,String keywords) throws Exception {
    	if(pageNo < 1) {
			return JSONResult.failCreate(JSONResult.ERROR_SELECT,JSONResult.ERROR_SELECT).toJSON();
		}
    	Map map = videoService.searchPage(pageNo,Contants.SEAR_TYPE_SIZE, keywords);
		if(map.size() < 1) {
			return JSONResult.noDataCreate().toJSON();
		}
		Long totalCount = (Long)map.get("totalCount");
		Page page = PageUtil.getPage(pageNo, Contants.SEAR_TYPE_SIZE,totalCount.intValue(), (List)map.get("videos"));
		return JSONResult.create(page).toJSON();
    }
@Override
	public Map searchPage(Integer pageNo,Integer pageSize,String keywords) throws SolrServerException, IOException{
		VideoSolrImpl videoSolrImpl = new VideoSolrImpl();
		return videoSolrImpl.searchSolr(pageNo, pageSize, keywords);
	}
@Override
	public Map searchSolr(Integer pageNo, Integer pageSize, String keywords) {
		Map map = new HashMap();
		SolrQuery params = new SolrQuery();
		if(StringUtils.isBlank(keywords)) {
			params.setQuery("video_keywords:*");
		}else {
			params.setQuery("video_keywords:"+keywords);
		}
		params.setStart(pageSize*(pageNo-1));
		params.setRows(pageSize);
		//开启高亮显示
		params.setHighlight(true);
		params.addHighlightField("video_name");
		params.setHighlightSimplePre("");
		params.setHighlightSimplePost("");
		try {
			QueryResponse response = solrClient.query(params);
			return getSolrVideoList(response);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
private Map getSolrVideoList(QueryResponse response){
		// 未高亮内容
		SolrDocumentList videoList = response.getResults();
		// 高亮内容
		Map>> hh = response.getHighlighting();
		Map map = new HashMap();
		List

至于新增数据、更新数据、删除数据,可以异步开启一个线程新增数据、更新数据、删除数据...

新增数据:

/**
	 * video单独新增
	 */
	private void insertVideo(Integer categoryId,Integer vipId,Integer accountId, String name, String recordPath, Integer hitQuantity,Integer status, String desc) {
		Video video = new Video(categoryId,vipId,accountId,name,recordPath,hitQuantity,status,desc);
		videoDao.insertVideo(video);
		// 异步开启一个线程新增一条Solr数据
		new Thread(new InsertVideoSolrTask(video)).start();
	}

insertTask

/**
 * 新增Solr数据任务
 * @author reyco
 *
 */
public class InsertVideoSolrTask implements Runnable{
	
	private Video video;
	
	public InsertVideoSolrTask(Video video) {
		super();
		this.video = video;
	}

	@Override
	public void run() {
		try {
			VideoSolrImpl videoSolrImpl = new VideoSolrImpl();
			videoSolrImpl.insertSolr(video);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
/**
	 * 单条数据添加到Solr
	 * @param video
	 */
	public void insertSolr(Video video) {
		try {
			SolrInputDocument doc = getSolrInputDocument(video);
			solrClient.add(doc);
			solrClient.commit();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			// 关闭流
			if (null != solrClient) {
				try {
					solrClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
@Override
	public SolrInputDocument getSolrInputDocument(Video video) {
		SolrInputDocument doc = new SolrInputDocument();
		doc.setField("id", video.getId().toString());
		doc.setField("video_name", video.getName());
		doc.setField("video_categoryId", video.getCategoryId().toString());
		doc.setField("video_vipId", video.getVipId().toString());
		doc.setField("video_accountId", video.getAccountId().toString());
		doc.setField("video_recordPath", video.getRecordPath());
		doc.setField("video_hitQuantity", video.getHitQuantity().toString());
		doc.setField("video_status", video.getStatus().toString());
		doc.setField("video_desc", video.getDesc());
		doc.setField("video_gmtCreate", DateUtil.formatDateTime(video.getGmtCreate()));
		doc.setField("video_gmtModified", DateUtil.formatDateTime(video.getGmtModified()));
		return doc;
	}

 

你可能感兴趣的:(solr)