直接贴代码:
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的facet查询总结
基于solrj开发solr主从搜索引擎服务的dubbo组件
solrj工具类封装,包括条件批量查询,批量增删改,分段修改。
放了SolrJ6.3.0所有web工程下的Jar包。solr-solrj6.3.0.jar等。放了SolrJ6.3.0所有web工程下的Jar包。放了SolrJ6.3.0所有web工程下的Jar包。
solr-solrj 5.0.0 和自己搭建的solr服务交互
主要讲解了 solr客户端如何调用带账号密码的solr服务器调用,实现添加索引和查询索引,以及分组查询
solr-solrj-4.9.0.jar
压缩文件里面有solr-solrj-4.10.3.jar和solr-solrj-5.0.0.jar两个jar
solrj使用教程
solrJ是Java连接solr进行查询检索和索引更新维护的jar包。
solr-solrj-4.10.3.jar。
Solrj 中文教程
solr-solrj-4.4.0.jar
solr-solrj-6.6.0.jar
solr部署在tomcat势必要设置用户权限,所以客户端solrj在创建连接的时候也需要用户名加密码
solr详细配置教程与solrj的使用
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。...Solrj 是访问 Solr 的 Java 客户端,它提供添加、更新和查询Solr 索引的接口。http://wiki.chenlb.com/solr/doku.php?id=solrj
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文
简单的SolrJ使用示例,包括文件:News.java(PO对应的class),SolrService.java(对solrJ的包装),SolrServiceHook.java(在查询前修改SolrQuery的行为)
java大数据开发中solrJ所需要的核心包.................