博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SolrJ
阅读量:2244 次
发布时间:2019-05-09

本文共 2563 字,大约阅读时间需要 8 分钟。

SolrJ

(1) 引入相关依赖

org.apache.solr
solr-solrj

(2) 编写配置文件:applicationContext-solr.xml

(3) 导入数据到solr索引库

@Servicepublic class SearchItemServiceImpl implements SearchItemService {    @Autowired    private SearchItemMapper searchItemMapper;        @Autowired    private SolrServer solrServer;        public Result importItemsToIndex() {        try {            List
itemList = searchItemMapper.getItemList(); for (SearchPojo searchPojo : itemList) { SolrInputDocument document = new SolrInputDocument(); document.addField("id", searchPojo.getId()); document.addField("item_title", searchPojo.getTitle()); document.addField("item_sell_point", searchPojo.getSell_point()); document.addField("item_price", searchPojo.getPrice()); document.addField("item_image", searchPojo.getImage()); document.addField("item_desc", searchPojo.getItem_desc()); document.addField("item_category_name", searchPojo.getCategory_name()); solrServer.add(document); } solrServer.commit(); } catch (Exception e) { e.printStackTrace(); return Result.build(500, "索引库导入失败!"); } return Result.ok(); }}

(4) 搜索solr索引库

@Servicepublic class SearchServiceImpl implements SearchService {    @Autowired    private SearchDao searchDao;        public SearchResult search(String queryString, int page, int rows) throws Exception {        SolrQuery query = new SolrQuery(queryString);        if (page < 1) {            page = 1;        }        if (rows < 1) {            rows = 10;        }        query.setStart((page - 1) * rows);        query.setRows(rows);        // 设置默认搜索域        query.set("df", "item_title");        // 设置高亮        query.setHighlight(true);    // 开启高亮        query.addHighlightField("item_title");    // 显示高亮的域        query.setHighlightSimplePre("");    //设置高亮颜色        query.setHighlightSimplePost("");                SearchResult result = searchDao.search(query);        // 计算总页数        long recordCount = result.getRecordCount();        long pages = recordCount / rows;        if (recordCount % rows > 0) {            pages++;        }        result.setTotalPages(pages);                return result;    }}

 

转载于:https://www.cnblogs.com/lin-nest/p/10322494.html

你可能感兴趣的文章
AJAX 设计制作 在公司弄的 非得要做出这个养的 真晕!
查看>>
Linux 查看文件大小
查看>>
Java并发编程:线程池的使用
查看>>
redis单机及其集群的搭建
查看>>
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>