Lucene全文检索中用到方法

Services中调用接口方法:

   1.重新生成索引

      

 /**
     * 重新生成索引
     *
     * @return
     */
    public String newIndex(String page) {
        HelpSearchDomain helpSearchDomain = (HelpSearchDomain) getContextBean("helpSearchDomain");
        BaseInfoAttributeDomain baseInfoAttributeDomain = (BaseInfoAttributeDomain) getContextBean("baseInfoAttributeDomain");
        //设置分页对象信息
        SplitPage sp = new SplitPage();
        sp.setCurrentPage(Integer.parseInt(page));
        sp.setPageSize(20);
        List knowledgeList = baseInfoAttributeDomain.getAllBaseInfoAttribute("1", sp);
        String path = "D:\\upload";
        path = PropertiesUtil.get("PATH", path);
        if (knowledgeList != null && knowledgeList.size() > 0) {
            for (Iterator iterator = knowledgeList.iterator(); iterator.hasNext();) {
                BaseInfoAttribute bi = (BaseInfoAttribute) iterator.next();
                BaseInfoAttribute bia = null;
                System.out.println(bi.getInfoType().getType());
                if (bi.getInfoType().getType().equals(InfoType.ZSK)) {
                    bia = baseInfoAttributeDomain.findBaseInfoAttribute(bi.getId());
                }
                try {
                    helpSearchDomain.delKnowledgeIndex(path + "\\" + bia.getContent(), bia.getInfoType().getName(), path);//删除索引
                } catch (Exception e) {
                    e.getMessage();
                }
            }
            for (Iterator iterator = knowledgeList.iterator(); iterator.hasNext();) {
                BaseInfoAttribute bi = (BaseInfoAttribute) iterator.next();
                if (bi.getInfoType().getType().equals(InfoType.ZSK)) {
                    BaseInfoAttribute bia = baseInfoAttributeDomain.findBaseInfoAttribute(bi.getId());
                    helpSearchDomain.createKnowledgeIndex(false, path + "\\" + bia.getContent(), path, bia.getTitle(), bia.getCreatetime(), bia.getInfoType().getName(), String.valueOf(bi.getId()));
                }
            }
        }
        return "索引生成成功";
    }

 

2.根据条件检索内容

  

/**
     * 根据条件检索内容
     *
     * @param keyword
     * @param type
     * @param page
     * @return
     */
    public Object searchKnowledgeQuery(String keyword, String type, String page) {
        HelpSearchDomain helpSearchDomain = (HelpSearchDomain) getContextBean("helpSearchDomain");
        String path = "D:\\upload";
        SplitPage sp = new SplitPage();
        sp.setPageSize(18);
        sp.setCurrentPage(page == null ? 1 : Integer.parseInt(page));
        String isExist = helpSearchDomain.isExistsKnowlegeIndex(path);
        List returnlist = new ArrayList();
        List _list = new ArrayList();
        List list = new ArrayList();
        if (!isExist.equals("")) {
            if (type.equals("3")) {//标题
                list = helpSearchDomain.searchKnowlegeByKey("title", keyword, path, sp);
                if (list != null && list.size() > 0) {
                    int len = list.size();
                    for (int i = 0; i < len; i++) {
                        Map m = (Map) list.get(i);
                        Map map = new HashMap();
                        map.put("id", m.get("knowlegeid"));
                        map.put("title", "<a href=\"javascript:void(0)\" οnclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
                        map.put("infoType", m.get("infoType"));
                        map.put("createtime", m.get("createtime"));
                        _list.add(map);
                    }
                }
            } else if (type.equals("2")) {// 内容
                list = helpSearchDomain.searchKnowlegeByKey("contents", keyword, path, sp);
                if (list != null && list.size() > 0) {
                    int len = list.size();
                    for (int i = 0; i < len; i++) {
                        Map m = (Map) list.get(i);
                        Map map = new HashMap();
                        map.put("id", m.get("knowlegeid"));
                        map.put("title", "<a href=\"javascript:void(0)\" οnclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
                        map.put("infoType", m.get("infoType"));
                        map.put("createtime", m.get("createtime"));
                        _list.add(map);
                    }
                }
            } else { //全文
                list = helpSearchDomain.searchKnowlegeByKey("all", keyword, path, sp);
                if (list != null && list.size() > 0) {
                    int len = list.size();
                    for (int i = 0; i < len; i++) {
                        Map m = (Map) list.get(i);
                        Map map = new HashMap();
                        map.put("id", m.get("knowlegeid"));
                        map.put("title", "<a href=\"javascript:void(0)\" οnclick=\'getKnowledgeInfo(" + m.get("knowlegeid") + ")\'>" + m.get("title") + "</a>");
                        map.put("infoType", m.get("infoType"));
                        map.put("createtime", m.get("createtime"));
                        _list.add(map);
                    }
                }
            }
            returnlist.add(_list);
            returnlist.add(sp);
            return returnlist;
        } else {
            return "";
        }
    }

 

有不懂得地方加:QQ413540158

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值