Lucene4.3检索,分词,搜索
??? ??? ??? }
??? ??? ??? IndexUtil.renameFile(dstDir, dstBakDir);
??? ??? }
??? ??? IndexUtil.renameFile(srcDir, dstDir);
??? }
??? public static String getIndexpath(String idxPath) {
??? ??? String indexPath =? Config.getProperty(idxPath);
??? ??? return SearchEngineCore.class.getResource("/").toString().replaceAll("WEB-INF/classes", indexPath)
??? ??? ??? .replaceAll("\\\\", "/").replace("file:/", "").trim();
??? }
}
?
public class UserInfo implements Serializable{
??? private static final long serialVersionUID = -1341713350583127283L;
???
??? private Long userId;
??? private String headImg;
??? private String nickName;
??? @JSONField(serialize=false)
??? private String loginName;
??? private int sex=-1;? //性别 0=男 1=女
??? private int checkinCount=0;? //签到数
??? private int favoriteCount=0;? //收藏数
???
??? public UserInfo(){}
??? public Long getUserId() {
??? ??? return userId;
??? }
??? public void setUserId(Long userId) {
??? ??? this.userId = userId;
??? }
??? public String getHeadImg() {
??? ??? return headImg;
??? }
??? public void setHeadImg(String headImg) {
??? ??? this.headImg = headImg;
??? }
??? public String getNickName() {
??? ??? return nickName;
??? }
??? public void setNickName(String nickName) {
??? ??? this.nickName = nickName;
??? }
??? public int getSex() {
??? ??? return sex;
??? }
??? public void setSex(int sex) {
??? ??? this.sex = sex;
??? }
??? public int getCheckinCount() {
??? ??? return checkinCount;
??? }
??? public void setCheckinCount(int checkinCount) {
??? ??? this.checkinCount = checkinCount;
??? }
??? public int getFavoriteCount() {
??? ??? return favoriteCount;
??? }
??? public void setFavoriteCount(int favoriteCount) {
??? ??? this.favoriteCount = favoriteCount;
??? }
??? public String getLoginName() {
??? ??? return loginName;
??? }
??? public void setLoginName(String loginName) {
??? ??? this.loginName = loginName;
??? }
}
?
索引搜索:
public class SoftSearch extends BaseServiceImpl{
???
??? private static String indexpath = SearchEngineCore.getIndexpath("VSOYOU_SOFT_INDEX_PATH"); //索引的路径
??? private static IndexSearcher indexSearcher = null;
???
??? public static void main(String[] args) throws ParseException {
??? ??? long startTime = System.currentTimeMillis();
??? ??? String searchWord ="QQ欢乐斗地主";
??? ??? searchWord = SearchUtil.wmlEncode(searchWord);
??? ??? searchWord = SearchUtil.traditionalToSimple(searchWord).trim();// 繁体转简体
??? ???
??? ??? int page =1;
??? ??? int pageSize = 100;
??? ??? TopDocs topDocs = search(searchWord,page,pageSize);
??? ??? System.out.println("总共命中数:"+topDocs.totalHits);
??? ??? if(topDocs != null && topDocs.totalHits !=0){
??? ??? ??? ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 搜索返回的结果集合
??? ??? ??? //查询起始记录位置
??? ??????? int begin = (page - 1)*pageSize ;
??? ??????? //查询终止记录位置
??? ??????? int end = Math.min(begin + pageSize, scoreDocs.length);
??? ??????? SoftSearch search = new SoftSearch();
??? ??????? List<Soft> softs = search.addHits2List(scoreDocs,begin,end);
??? ??????? for (int i = 0; i < softs.size(); i++) {
??? ??? ??? ??? System.out.println("i: \t "+softs.get(i).getSoftName());
??? ??? ??? }
??? ??? }
??? ??? System.out.println("检索完成用时:"+(System.currentTimeMillis()-startTime)+"毫秒");
??? }
??? private static TopDocs search(String searchWord,int page,int pageSize) throws ParseException {
??? ??? String indexDir = indexpath + File.separator + "softIndex";
??? ??? indexSearcher = SearchEngineCore.getIndexSearcher(indexDir);
??? ??? if(null == indexSearcher) return null;
??? ??? TopDocs topDocs = null;
??? ??? try {
??? ??? ??? BooleanQuery allQuery = new BooleanQuery();
??? ??? ??? //根据搜索关键字进行查询
??? ??? ??? QueryParser parser = new QueryParser(Version.LUCENE_43,"softName",new IKAnalyzer());
??? ??? ??? Query query = parser.parse(searchWord);
??? ??? ??? allQuery.add(query, BooleanClause.Occur.MUST);
??? ??? ???
??? ??? ??? QuerySort keywordQuerySort = getKeywordQuerySort();
??? ??? ??? allQuery.add(keywordQuerySort.query,BooleanClause.Occur.MUST);
??? ??? ???
??? ??? ??? topDocs = indexSearcher.search(allQuery, page*pageSize, keywordQuerySort.sort);
??? ??? } catch (IOException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return topDocs;
??? }
??? private? List<Soft> addHits2List(ScoreDoc[] scoreDocs,int begin,int end) {
??? ??? List<Soft> softs = new ArrayList<Soft>();
??? ??? try {
??? ??? ??? this.commentsInfoDao = ServiceFactory.getBean(CommentsInfoDao.class);
??? ??? ??? for (int i = begin; i < end; i++) {
??? ??? ??? ??? int docID = scoreDocs[i].doc;
??? ??? ??? ??? Soft soft = new Soft();
??? ??? ??? ??? Document doc = indexSearcher.doc(docID);
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("fileSize")))
??? ??? ??? ??? ??? soft.setFileSize(Long.valueOf(doc.get("fileSize")));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("freeDownload")))
??? ??? ??? ??? ??? soft.setFreeDownload(doc.get("freeDownload").equals("1"));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("freeUse")))
??? ??? ??? ??? ??? soft.setFreeUse(doc.get("freeUse").equals("1"));
??? ??? ??? ??? soft.setIconPath(doc.get("iconPath"));
??? ??? ??? ??? soft.setReleaseDate(doc.get("releaseDate"));
??? ??? ??? ??? soft.setReleaseId(Long.valueOf(doc.get("releaseId")));
??? ??? ??? ??? soft.setSoftName(doc.get("softName"));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("stars")))
??? ??? ??? ??? ??? soft.setStars(Integer.valueOf(doc.get("stars")));
??? ??? ??? ???
??? ??? ??? ??? int commentRowCount = NumberUtils.strToInt(commentsInfoDao.getSoftCommentRowCount(Long.valueOf(doc.get("releaseId"))));
??? ??? ??? ??? if(commentRowCount >=0){
??? ??? ??? ??? ??? soft.setTotalComemntCount(commentRowCount);
??? ??? ??? ??? }else if(StringUtils.isNotBlank(doc.get("totalComemntCount"))){
??? ??? ??? ??? ??? soft.setTotalComemntCount(Integer.valueOf(doc.get("totalComemntCount")));
??? ??? ??? ??? }
??? ??? ??? ???
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("totalDownloads")))
??? ??? ??? ??? ??? soft.setTotalDownloads(Integer.valueOf(doc.get("totalDownloads")));
??? ??? ??? ??? soft.setVersionName(doc.get("versionName"));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("softCurrency")))
??? ??? ??? ??? ??? soft.setSoftCurrency(Integer.valueOf(doc.get("softCurrency")));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("versionCode")))
??? ??? ??? ??? ??? soft.setVersionCode(Long.valueOf(doc.get("versionCode")));
??? ??? ??? ??? if(StringUtils.isNotBlank(doc.get("packageName")))
??? ??? ??? ??? ??? soft.setPackageName(doc.get("packageName"));
??? ??? ??? ??? softs.add(soft);
??? ??? ??? }
??? ??? } catch (IOException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return softs;
??? }
??? private static QuerySort getKeywordQuerySort() {
??? ??? QuerySort querySort = new QuerySort();
??? ??? querySort.query = new TermQuery(new Term("runType", "100"));
??? ??? querySort.sort = new Sort(new SortField[] {
??? ??? ??? ??? new SortField("totalDownloads", SortField.Type.INT, true),
??? ??? ??? ??? new SortField("totalComemntCount", SortField.Type.INT, true),
??? ??? ??? ??? ?new SortField("releaseDate", SortField.Type.STRING, true),
??? ??? ??? ??? new SortField("freeDownload", SortField.Type.INT, true)
??? ??? ??? });
??? ??? return querySort;
??? }
??? public Map<String, Object> searchKeyWord(String searchWord,int page,int pageSize) {
??? ??? Map<String, Object> map = new HashMap<String, Object>();
??? ??? map.put(Const.IMG_DOMAIN_KEY,Const.IMG_DOMAIN_VALUE);
??? ??? try {
??? ??? ??? searchWord = SearchUtil.wmlEncode(searchWord);
??? ??? ??? searchWord = SearchUtil.traditionalToSimple(searchWord).trim();// 繁体转简体
??? ??? ??? TopDocs topDocs = search(searchWord,page,pageSize);
??? ??? ??? if(topDocs == null || topDocs.totalHits ==0){
??? ??? ??? ??? map.put("list", null);
??? ??? ??? ??? return map;
??? ??? ??? }
??? ??? ??? map.put("pageCount", getPageCount(topDocs.totalHits,pageSize));
??? ??? ??? ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 搜索返回的结果集合
??? ??? ??? //查询起始记录位置
??? ??????? int begin = (page - 1)*pageSize ;
??? ??????? //查询终止记录位置
??? ??????? int end = Math.min(begin + pageSize, scoreDocs.length);
??? ??? ??? List<Soft> softs = addHits2List(scoreDocs,begin,end);
??? ??? ??? map.put("list", softs);
??? ??? ???
??? ??? ???
??? ??? ??? /*SoftSearchKeyWord searchKeyWord = new SoftSearchKeyWord();
??? ??? ??? searchKeyWord.setKeyword(searchWord);
??? ??? ??? Thread thread = new Thread(new SearchKeyWordThread(searchKeyWord));
??? ??? ??? thread.start();*/
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? return map;
??? }
??? private int getPageCount(int rowCount, int pageSize) {
??? ??? int pageCount = 1;
??? ??? if ((rowCount % pageSize) == 0) {
??? ??? ??? pageCount = rowCount / pageSize;
??? ??? } else {
??? ??? ??? pageCount = rowCount / pageSize + 1;
??? ??? }
??? ??? if (pageCount == 0) {
??? ??? ??? pageCount = 1;
??? ??? }
??? ??? return pageCount;
??? }
???
}
需要jar包:
IKAnalyzer2012FF_u1.jarIKAnalyzer3.2.5Stable.jarlucene-analyzers-common-4.3.0.jar lucene-core-4.3.0.jar lucene-highlighter-4.3.0.jar lucene-memory-4.3.0.jar lucene-queryparser-4.3.0.jar