• 完成搜索系统功能(十八)搜索功功能完成


    1.在jingxi-search中写搜索的服务

    搜索页面由总页数totalPages和搜索出来的内容列表itemList组成,而且可能会用到查询总数量。这些我们应该放到一个pojo当中。

    所以我们需要建立一个SearchResult这个pojo类

    package com.jingxi.search.pojo;
    
    import java.util.List;
    
    public class SearchResult {
    
            //商品列表
            private List<Item> itemList;
            //总记录数
            private long recordCount;
            //总页数
            private long pageCount;
            //当前页
            private long curPage;
            public List<Item> getItemList() {
                return itemList;
            }
            public void setItemList(List<Item> itemList) {
                this.itemList = itemList;
            }
            public long getRecordCount() {
                return recordCount;
            }
            public void setRecordCount(long recordCount) {
                this.recordCount = recordCount;
            }
            public long getPageCount() {
                return pageCount;
            }
            public void setPageCount(long pageCount) {
                this.pageCount = pageCount;
            }
            public long getCurPage() {
                return curPage;
            }
            public void setCurPage(long curPage) {
                this.curPage = curPage;
            }
            
            
    
    }
    package com.jingxi.search.pojo;
    
    public class Item {
        
        private String id;
        private String title;
        private String sell_point;
        private long price;
        private String image;
        private String category_name;
        private String item_des;
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getSell_point() {
            return sell_point;
        }
        public void setSell_point(String sell_point) {
            this.sell_point = sell_point;
        }
        public long getPrice() {
            return price;
        }
        public void setPrice(long price) {
            this.price = price;
        }
        public String getImage() {
            return image;
        }
        public void setImage(String image) {
            this.image = image;
        }
        public String getCategory_name() {
            return category_name;
        }
        public void setCategory_name(String category_name) {
            this.category_name = category_name;
        }
        public String getItem_des() {
            return item_des;
        }
        public void setItem_des(String item_des) {
            this.item_des = item_des;
        }
    
        
    }

     

    1.1 dao层

    package com.jingxi.search.dao;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocument;
    import org.apache.solr.common.SolrDocumentList;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import com.jingxi.search.pojo.Item;
    import com.jingxi.search.pojo.SearchResult;
    
    @Repository
    public class SearchDaoImpl implements SearchDao {
    
        @Autowired
        private SolrServer solrServer;
        
        @Override
        public SearchResult search(SolrQuery query) throws Exception {
                    //返回值对象
                    SearchResult result = new SearchResult();
                    //根据查询条件查询索引库
                    QueryResponse queryResponse = solrServer.query(query);
                    //取查询结果
                    SolrDocumentList solrDocumentList = queryResponse.getResults();
                    //取查询结果总数量
                    result.setRecordCount(solrDocumentList.getNumFound());
                    //商品列表
                    List<Item> itemList = new ArrayList<>();
                    //取高亮显示
                    Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
                    //取商品列表
                    for (SolrDocument solrDocument : solrDocumentList) {
                        //创建一商品对象
                        Item item = new Item();
                        item.setId((String) solrDocument.get("id"));
                        //取高亮显示的结果
                        List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
                        String title = "";
                        if (list != null && list.size()>0) {
                            title = list.get(0);
                        } else {
                            title = (String) solrDocument.get("item_title");
                        }
                        item.setTitle(title);
                        item.setImage((String) solrDocument.get("item_image"));
                        item.setPrice((long) solrDocument.get("item_price"));
                        item.setSell_point((String) solrDocument.get("item_sell_point"));
                        item.setCategory_name((String) solrDocument.get("item_category_name"));
                        //添加的商品列表
                        itemList.add(item);
                    }
                    result.setItemList(itemList);
                    return result;
                }
    
    
        }

     1.2 service层

    package com.jingxi.search.service;
    
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServer;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.jingxi.search.dao.SearchDao;
    import com.jingxi.search.pojo.SearchResult;
    
    @Service
    public class SearchServiceImpl implements SearchService {
    
    /*    @Autowired
        private SolrServer solrServer;*/
        
        @Autowired
        private SearchDao searchDao;
        
        @Override
        public SearchResult search(String queryString, int page, int rows) throws Exception {
            //创建查询对象
            SolrQuery query=new SolrQuery();
            //设置查询条件
            query.setQuery(queryString);
            //设置分页
            query.setStart((page - 1)* rows);
            query.setRows(rows);
            //设置默认的搜索域
            query.set("df", "item_keywords");
            //设置高度显示 -->就是设置在搜索后的页面上的东西 商品页面下面的文字显示
            query.setHighlight(true);
            query.addHighlightField("item_title");
            query.setHighlightSimplePre("<em style="color:red">");
            query.setHighlightSimplePost("</em>");
            //执行查询  -->调用dao的方法  后执行查询
            SearchResult searchResult =searchDao.search(query);
            //计算查询结果总页数
            long recordCount = searchResult.getRecordCount();
            long pageCount = recordCount / rows;
            if (recordCount % rows > 0) {
                pageCount++;
            }
            searchResult.setPageCount(pageCount);
            searchResult.setCurPage(page);
            
            return searchResult;
    
        }
    
    }

    1.3 controller层

    package com.jingxi.search.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.jingxi.common.pojo.JingXiResult;
    import com.jingxi.common.util.ExceptionUtil;
    import com.jingxi.search.pojo.SearchResult;
    import com.jingxi.search.service.SearchService;
    
    @Controller
    public class SearchController {
    
        @Autowired
        private SearchService searchService;
        
        
        @RequestMapping(value="/query",method=RequestMethod.GET)
        @ResponseBody
        public JingXiResult search(@RequestParam("q")String queryString,
                        @RequestParam(defaultValue="1")Integer page,
                        @RequestParam(defaultValue="60")Integer rows){
            //假如条件为空的话
            if(queryString == null){
                return JingXiResult.build(400, "查询条件不能为空");
                
            }
            SearchResult searchResult=null;
            try{
                //解决乱码的问题
                queryString =new String(queryString.getBytes("iso8859-1"),"utf-8");
                searchResult=searchService.search(queryString, page, rows);
            }catch(Exception e){
                e.printStackTrace();
                return JingXiResult.build(500, ExceptionUtil.getStackTrace(e));
            }
            
            return JingXiResult.ok(searchResult);
            
        }
        
    }

     

    2.在jingxi-portal中调用jingxi-search的服务

    service层:

    package com.jingxi.portal.service;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    
    import com.jingxi.common.pojo.JingXiResult;
    import com.jingxi.common.util.HttpClientUtil;
    import com.jingxi.portal.pojo.SearchResult;
    @Service
    public class SearchServiceImpl implements SearchService {
    
        @Value("${SEARCH_BASE_URL}")
        private String SEARCH_BASE_URL;  (url的链接为:SEARCH_BASE_URL=http://localhost:8081/search/query)
        @Override
        public SearchResult search(String queryString, int page) {
            // 调用taotao-search的服务
            //查询参数
            Map<String, String> param = new HashMap<>();
            param.put("q", queryString);
            param.put("page", page + "");
                try {
                    //调用服务
                    String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
                    //把字符串转换成java对象
                    JingXiResult jingxiResult = JingXiResult.formatToPojo(json, SearchResult.class);
                    if (jingxiResult.getStatus() == 200) {
                        SearchResult result = (SearchResult) jingxiResult.getData();
                        return result;
                    }
                        
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
    
        }

    controller层:

    package com.jingxi.portal.controller;
    
    import java.io.UnsupportedEncodingException;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.jingxi.portal.pojo.SearchResult;
    import com.jingxi.portal.service.SearchService;
    
    @Controller
    public class SearchController {
    
        @Autowired
        private SearchService searchService;
        
        @RequestMapping("/search")
        public String search(@RequestParam("q")String queryString, @RequestParam(defaultValue="1")Integer page, Model model) {
            if (queryString != null) {
                try {
                    queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            SearchResult searchResult = searchService.search(queryString, page);
            //向页面传递参数
            model.addAttribute("query", queryString);
            model.addAttribute("totalPages", searchResult.getPageCount());
            model.addAttribute("itemList", searchResult.getItemList());
            model.addAttribute("page", page);
            
            return "search";
            
        }
    
    }
  • 相关阅读:
    Delphi 与 C/C++ 数据类型对照表
    JAVA中堆和栈的区别
    关于Column '*' not found 解决方案 Hibernate使用SQL查询返回实体类型,即返回某个类,或实体类
    Oracle笔记
    oracle时间运算
    struts2中iterator标签的相关使用
    url传中文,转码
    表格的css,细线表格
    使用struts 2 获取服务器数据 ongl表达式 标签
    struts 2 Commons FileUpload文件上传
  • 原文地址:https://www.cnblogs.com/mumudechengzhang/p/7715175.html
Copyright © 2020-2023  润新知