第六课 Spring整合solr实现文章的添加与搜索

maven里添加依赖

org.apache.solr
solr-solrj
4.10.2


commons-logging
commons-logging
1.2

spring配置文件里添加



添加文章
Contoller或service里添加

@Autowired
private SolrServer solrServer;

public int addArticle(Article article) {
int result = articleMapper.addArticle(article);

    SolrInputDocument inputDocument = new SolrInputDocument();
    //向文档中添加域以及对应的值,注意:所有的域必须在schema.xml中定义过,前面已经给出过我定义的域。
    UUID uuid = UUID.randomUUID();
    inputDocument.addField("id", uuid);
    inputDocument.addField("article_title", article.getTitle());
    inputDocument.addField("article_content", article.getContent());
    inputDocument.addField("article_id", article.getAid());
    try {
        solrServer.add(inputDocument);
        solrServer.commit();
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

检索文章
public List

queryArticles(String keyword,SolrQueryPageInfo solrQueryPageInfo) {

    List
articleList = new ArrayList<>(); SolrQuery solrQuery = new SolrQuery(); solrQuery.set("q",keyword); solrQuery.set("df","article_keywords");//df=>default field solrQuery.setSort("id", SolrQuery.ORDER.asc); solrQuery.setStart(solrQueryPageInfo.getPageIndex()*solrQueryPageInfo.getPageSize()); solrQuery.setRows(solrQueryPageInfo.getPageSize()); //开启高亮显示 solrQuery.setHighlight(true); solrQuery.addHighlightField("article_title"); solrQuery.addHighlightField("article_content"); solrQuery.setHighlightSimplePre(""); solrQuery.setHighlightSimplePost(""); QueryResponse queryResponse = null; try { queryResponse = solrServer.query(solrQuery); } catch (SolrServerException e) { e.printStackTrace(); } SolrDocumentList solrDocumentList = queryResponse.getResults(); int total = (int) solrDocumentList.getNumFound(); solrQueryPageInfo.setTotal(total); Map>> mapMapHighlighting = queryResponse.getHighlighting(); for(SolrDocument solrDocument : solrDocumentList) { Article article = new Article(); String id = (String)solrDocument.get("id"); String article_title = (String)solrDocument.get("article_title"); String article_content = (String)solrDocument.get("article_content"); Long article_id = (Long)solrDocument.get("article_id"); article.setTitle(article_title); article.setContent(article_content); article.setAid(article_id.intValue()); Map> map = mapMapHighlighting.get(id);//一个商品,一个json对象 for(Map.Entry> entry : map.entrySet()) { if(entry.getKey().equals("article_title")) { article.setTitle(entry.getValue().get(0)); } if(entry.getKey().equals("article_content")) { article.setContent(entry.getValue().get(0)); } } articleList.add(article); } return articleList; }

Controller里调用

@RequestMapping("query")
public void queryArticles(String keyword, Model model, SolrQueryPageInfo solrQueryPageInfo, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");

    ArticlePageInfo articlePageInfo = new ArticlePageInfo();
    List
articleList = articleService.queryArticles(keyword,solrQueryPageInfo); articlePageInfo.setTotal(solrQueryPageInfo.getTotal()); articlePageInfo.setArticles(articleList); String jsonString = JSON.toJSONString(articlePageInfo); response.getWriter().println(jsonString); }

ArticlePageInfo
public class ArticlePageInfo {
int total;
List

articles;

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

public List
getArticles() { return articles; } public void setArticles(List
articles) { this.articles = articles; }

}
SolrQueryPageInfo
public class SolrQueryPageInfo {
int pageIndex;
int pageSize;
int total;

public int getPageIndex() {
    return pageIndex;
}

public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
}

public int getPageSize() {
    return pageSize;
}

public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

}
index_query.jsp
<%--
Created by IntelliJ IDEA.
User: ttc
Date: 2018/7/6
Time: 14:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>





可能的问题
如果想向solr添加带html标签的文本,可以使用jsoup选中元素,调用text()方法脱去全部的html标签,再保存到索引中。

谢谢

你可能感兴趣的:(第六课 Spring整合solr实现文章的添加与搜索)