基于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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solr基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎 课程特点毕业后接触的第一个中间件就是Solr,在工作中用处广泛,为了便于大家快速掌握该技能,开始录制相关课程,该专栏特点如下:1.采用Solr最新版本视频录制,全网最新课程(Solr8.1于2019年5月16日发布)2.技能点全网最全,会结合工作经验,项目中用到的技能点都会有所涉及,更新章节比较全面3.适用范围广,从零基础到高级架构以及分布式集群都涵盖,适用初级、高级、项目实战等多个层次开发者4.多种维度辅助学习,采用独立solr粉丝群辅助教学,学员问题会及时得到解决,程序员突破圈 打卡制度,督促学员学习关注后再购买、 关注后再购买、 关注后再购买课程能得到什么1.快速学习到最新版本的全文检索技术,从视频、文章、圈子、粉丝交流等快速促进学习2.通过该技术,获得面试进阶指导3.结交人脉(庞大的粉丝群)..End初期学员100人,价格不会太高,也是为了帮助更多的开发者但是个人精力有限,所以限制条件如下1.求知欲强,有想向技术更深一层了解的2.乐于交流,喜欢探讨技术者3.学习惰性者慎入,购买后会督促大家学习,购买不是目的,学习到该技能才是该专栏的主要目的正式进入学习状态了吗,专栏群见。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值