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<Video> videos = videoDao.searchAll();
		saveVideoSolr(videos);
	}
public void saveVideoSolr(List<Video> list) {
		// 数据集合大小
		int listSize = list.size();
		// 一个线程处理多少条数据,默认333
		int count = getEachTaskCount(listSize);
		// 开启的线程数
		int runThreadSize  = (listSize / count) + 1;   
		// 存放每个线程的执行数据
		//List<Video> newList = null;
		// 创建一个线程池,数量和开启线程的数量一样
	    ExecutorService executor = Executors.newFixedThreadPool(runThreadSize);
	    // 门闩
	    CountDownLatch latch = new CountDownLatch(runThreadSize);
	    long start = System.currentTimeMillis();
        // 循环创建线程
        for (int i=0;i<runThreadSize;i++) {
        	List<Video> newList = getEachTaskStartAndEnd(runThreadSize,count,list,i);
        	try {
				executor.execute(new InitVideoSolrTask(newList));
				latch.countDown();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
        try {
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        long end = System.currentTimeMillis();
        System.out.println("###############################end-start......."+(end-start));
	}
/**
	 * 根据任务的大小,获取每个任务的执行数量
	 * @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<Video> newList = null;
		if ((i + 1) == runThreadSize) {
            int startIndex = (i * count);
            int endIndex = list.size();
            newList = list.subList(startIndex, endIndex);
        } else {
            int startIndex = (i * count);
            int endIndex = ((i + 1) * count);
            newList = list.subList(startIndex, endIndex);
        }
		return newList;
	}

initTask

/**
 * 初始化Solr任务
 * @author reyco
 *
 */
public class InitVideoSolrTask implements Runnable {
	
	private List<Video> list;

	public InitVideoSolrTask(List<Video> list) {
		super();
		this.list = list;
	}

	@Override
	public void run() {
		try {
			VideoSolrImpl videoSolrImpl = new VideoSolrImpl();
			videoSolrImpl.initSolr(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
/**
	 * 初始化Solr
	 * 
	 * @param list
	 */
	@Override
	public void initSolr(List<Video> list) {
		try {
			// 删除Solr所有数据
			solrClient.deleteByQuery("*:*");
			solrClient.commit();
			// 数据初始化
			BatchInsertSolr(list);
		} catch (SolrServerException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
@Override
	public void BatchInsertSolr(List<Video> list) {
		try {
			for (Video video : list) {
				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查询及高亮显示:

 @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<String, Object> 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<String,Object> 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<String,Object> map = new HashMap<String,Object>();
		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("<span style='color:red'>");
		params.setHighlightSimplePost("</span>");
		try {
			QueryResponse response = solrClient.query(params);
			return getSolrVideoList(response);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
private Map<String,Object> getSolrVideoList(QueryResponse response){
		// 未高亮内容
		SolrDocumentList videoList = response.getResults();
		// 高亮内容
		Map<String, Map<String, List<String>>> hh = response.getHighlighting();
		Map<String,Object> map = new HashMap<String,Object>();
		List<Video> list = new ArrayList<Video>();
		String path =PropertiesUtil.getValue("video.path");
		for (SolrDocument solrDocument : videoList) {
			Video video = new Video();
			String fieldValue = (String)solrDocument.getFieldValue("id");
			Integer id = Integer.parseInt(fieldValue);
			video.setId(id);
			List<String> hhList = hh.get(solrDocument.getFieldValue("id")).get("video_name");
			if(null != hhList && hhList.size()>0) {
				video.setName(hhList.get(0));
			}else {
				video.setName(solrDocument.getFieldValue("video_name").toString());
			}
			video.setCategoryId((Integer)solrDocument.getFieldValue("video_categoryId"));
			video.setVipId((Integer)solrDocument.getFieldValue("video_vipId"));
			video.setAccountId((Integer)solrDocument.getFieldValue("video_accountId"));
			video.setRecordPath(path+solrDocument.getFieldValue("video_recordPath").toString());
			video.setHitQuantity((Integer)solrDocument.getFieldValue("video_hitQuantity"));
			video.setStatus((Integer)solrDocument.getFieldValue("video_status"));
			video.setDesc(solrDocument.getFieldValue("video_desc").toString());
			video.setGmtCreate(DateUtil.parseDateTime(solrDocument.getFieldValue("video_gmtCreate").toString()));
			video.setGmtModified(DateUtil.parseDateTime(solrDocument.getFieldValue("video_gmtModified").toString()));
			list.add(video);
		}
		map.put("totalCount", videoList.getNumFound());
		map.put("videos", list);
		return map;
	}

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

新增数据:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java的艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值