基于solrJ 查询 显示 分页,高亮等处理

直接贴代码:

 Map<String, Object> map = new HashMap<String, Object>();
        AjaxResultModel arm = new AjaxResultModel();
        try {
            List<String> searchTypeList = new ArrayList<String>();

            SolrServer server = new HttpSolrServer(SearchUtils.SOLR_HTTP_SERVER_URL);
            SolrQuery query = new SolrQuery(SearchConstants.SEARCH_KEY_ALL + keyWord);

            HightLightUtils.getFilterQuery(prece, news, book, boat, knowledge, operate, selectType, query);

            PagingInfo page = HightLightUtils.setPageAndHightLight(curPage, pageSize, keyWord, query);

            List<SolrInforModel> list = new ArrayList<SolrInforModel>();

            QueryResponse qResponse = server.query(query);

            String lastWord = SolrDocUtils.checkSpellKey(query, qResponse);

            SolrDocumentList docs = qResponse.getResults();

            FacetField facetField = qResponse.getFacetField("root_info_type_id");
            List<Count> counts = null;

            if (facetField != null) {
                counts = facetField.getValues();
            }
            for (SolrDocument doc : docs) {
                SolrDocUtils.getSolrDocument(list, qResponse, doc);
            }

            SpringModelExtraUtils.ajaxMapModel(pageSize, map, searchTypeList, page, list, lastWord, docs, counts);
            arm.setData(map);
            arm.setStatus(AjaxResultModel.SUCCESS);
        } catch (Exception e) {
            arm.setStatus(AjaxResultModel.FAIL);

            logger.error(Thread.currentThread().getStackTrace()[1].getClass(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(), e.getMessage());
        }
        JsonUtils.objToJson(arm, response);
    }

 public static String getFilterQuery( String news, String book, String boat, String knowledge,
            String selectType, SolrQuery query) {
        String fq = "";

  
        if (StringUtils.isNotBlank(news)) {
            fq += news + ",";
        }
        if (StringUtils.isNotBlank(book)) {
            fq += book + ",";
        }
        if (StringUtils.isNotBlank(boat)) {
            fq += boat + ",";
        }
        if (StringUtils.isNotBlank(knowledge)) {
            fq += knowledge + ",";
        }
        if (StringUtils.isNotBlank(selectType) && !"all".equals(selectType)) {
            fq += selectType + ",";
        }
        if (StringUtils.isNotBlank(fq)) {
            fq = fq.substring(0, fq.lastIndexOf(","));
            query.addFilterQuery(SearchConstants.INFO_TYPE_KEY + fq);
        }
        return fq;
    }


HightLightUtils
   /**
     * 设置分页高亮
     * 
     * @param curPage
     * @param pageSize
     * @param keyWord
     * @param query
     * @return
     */
    public static PagingInfo setPageAndHightLight(Integer curPage, Integer pageSize, String keyWord, SolrQuery query) {
        PagingInfo page = new PagingInfo(pageSize, curPage);
        query.setStart((page.getCurrentPage() - 1) * page.getPageSize());
        query.setRows(pageSize);
        query.setHighlight(true);
        query.setParam("hl.fl", "info_title,info_content");
        query.setHighlightSimplePre("<font color=\'red\'>");

        query.setHighlightSimplePost("</font>");
        query.setHighlightFragsize(200);
        query.set("qt", "/spell");
        query.set("spellcheck", "on");
        query.set("spellcheck.q", keyWord);
        query.set("spellcheck.collate", "true");
        query.set("spellcheck.dictionary", "file");
        query.set("spellcheck.build", "true");
        query.set("facet", "on");
        query.set("facet.field", "root_info_type_id");
        return page;
    }




//纠错拼写
    public static String checkSpellKey(SolrQuery query, QueryResponse spellRespose) throws SolrServerException {
        String lastWord = "";
        SpellCheckResponse spellCheckResponse = spellRespose.getSpellCheckResponse();
        List<Suggestion> suggestionList = spellCheckResponse.getSuggestions();

        if (suggestionList.size() > 0) {
            if (!spellCheckResponse.isCorrectlySpelled()) {
                lastWord = suggestionList.get(0).getAlternatives().toString().replace("[", "").replace("]", "");
                if (lastWord.indexOf(",") > 0) {
                    lastWord = lastWord.split(",")[0].toString();
                }
            }

            query.set("q", lastWord);
        }
        return lastWord;
    }
//文档高亮等
    public static String getSolrDocument(List<SolrInforModel> list, QueryResponse qResponse, SolrDocument doc) {
        SolrInforModel pj = new SolrInforModel();

        String id = doc.getFieldValue("id").toString();
        String solrTitle = SolrStringUtils.htmlReplace(doc.getFieldValue("info_title").toString());

        String solrType = SolrStringUtils.htmlReplace(doc.getFieldValue("info_type_name").toString());

        pj.setId(id);
        String hightTitle = qResponse.getHighlighting().get(id).get("info_title") == null ? solrTitle : SolrStringUtils
                .htmlReplace(qResponse.getHighlighting().get(id).get("info_title").toString());

        String solrContent = SolrStringUtils.htmlReplace(doc.getFieldValue("info_content").toString());
        pj.setInfoContent(qResponse.getHighlighting().get(id).get("info_content") == null ? solrContent
                : SolrStringUtils.htmlReplace(qResponse.getHighlighting().get(id).get("info_content").toString()));

        pj.setInfoTitle(hightTitle);

        pj.setInfoTypeName(solrType);
        list.add(pj);

        return solrType;
    }

你可能感兴趣的:(solrj)