欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lucene全文检索中用到方法

程序员文章站 2022-03-04 23:42:05
...

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)\" onclick=\'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)\" onclick=\'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)\" onclick=\'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