SolrJ的使用
1 封装了查询的关键字,查询过滤条件, 查询排序, or查询 2 3 [java] view plain copy 4 5 SolrQuery solrQuery = new SolrQuery(); 6 //solrQuery.setQuery("status:1"); 7 solrQuery.set("q", "storeName:" + "麦兜"); 8 solrQuery.addFilterQuery("status:" + BaseConstant.STATUS_ENABLE); 9 solrQuery.setQuery("name:连衣裙 OR storeName:8"); 10 solrQuery.setSort("onlineTime", ORDER.desc); 11 //高亮 12 solrQuery.setHighlight(true); 13 solrQuery.addHighlightField("storeName"); 14 solrQuery.setHighlightSimplePre("<span style='color:red'>"); 15 solrQuery.setHighlightSimplePost("</span>"); 16 17 Page<Goods> page = SolrUtil.getPage(new Page<Goods>(), solrQuery, Goods.class); 18 19 20 21 在网上的搜的语法 22 23 [java] view plain copy 24 25 //AND 并且 26 SolrQuery params = new SolrQuery("name:apple AND manu:inc"); 27 28 //OR 或者 29 params.setQuery("name:apple OR manu:apache"); 30 //空格 等同于 OR 31 params.setQuery("name:server manu:dell"); 32 33 //params.setQuery("name:solr - manu:inc"); 34 //params.setQuery("name:server + manu:dell"); 35 36 //查询name包含solr apple 37 params.setQuery("name:solr,apple"); 38 //manu不包含inc 39 params.setQuery("name:solr,apple NOT manu:inc"); 40 41 //50 <= price <= 200 42 params.setQuery("price:[50 TO 200]"); 43 params.setQuery("popularity:[5 TO 6]"); 44 //params.setQuery("price:[50 TO 200] - popularity:[5 TO 6]"); 45 //params.setQuery("price:[50 TO 200] + popularity:[5 TO 6]"); 46 47 //50 <= price <= 200 AND 5 <= popularity <= 6 48 params.setQuery("price:[50 TO 200] AND popularity:[5 TO 6]"); 49 params.setQuery("price:[50 TO 200] OR popularity:[5 TO 6]"); 50 51 //过滤器查询,可以提高性能 filter 类似多个条件组合,如and 52 //params.addFilterQuery("id:VA902B"); 53 //params.addFilterQuery("price:[50 TO 200]"); 54 //params.addFilterQuery("popularity:[* TO 5]"); 55 //params.addFilterQuery("weight:*"); 56 //0 < popularity < 6 没有等于 57 //params.addFilterQuery("popularity:{0 TO 6}"); 58 59 //排序 60 params.addSortField("id", ORDER.asc); 61 //分页:start开始页,rows每页显示记录条数 64 //params.setStart(0); 65 //params.setRows(200); 66 67 //设置高亮 68 params.setHighlight(true); // 开启高亮组件 69 params.addHighlightField("name");// 高亮字段 70 params.setHighlightSimplePre("<font color='red'>");//标记,高亮关键字前缀 71 params.setHighlightSimplePost("</font>");//后缀 72 params.setHighlightSnippets(1);//结果分片数,默认为1 73 params.setHighlightFragsize(1000);//每个分片的最大长度,默认为100 74 75
案例使用:搜索框和筛选
1 public class ResultModel { 2 3 //总记录数 4 private Long recordCount; 5 //每页数量 6 private Integer perCount = 20; 7 //当前页数 8 private Integer curPage = 0; 9 //商品列表 10 private List<Product> productList; 11 12 public Long getRecordCount() { 13 return recordCount; 14 } 15 public void setRecordCount(Long recordCount) { 16 this.recordCount = recordCount; 17 } 18 public Integer getPerCount() { 19 return perCount; 20 } 21 public void setPerCount(Integer perCount) { 22 this.perCount = perCount; 23 } 24 public Integer getPageCount() { 25 if (recordCount % perCount == 0) 26 return (int) (recordCount / perCount); 27 else 28 return (int) (recordCount / perCount) + 1; 29 } 30 public Integer getCurPage() { 31 return curPage; 32 } 33 public void setCurPage(Integer curPage) { 34 this.curPage = curPage; 35 } 36 public List<Product> getProductList() { 37 return productList; 38 } 39 public void setProductList(List<Product> productList) { 40 this.productList = productList; 41 } 42 } 43 44 45 @Service 46 public class MyServiceImpl implements MyService { 47 48 @Autowired 49 private HttpSolrServer solrServer; 50 51 @Override 52 public ResultModel queryProducts(String keywords, String catalogName, String priceArea, String sort, 53 Integer pageNum) throws Exception { 54 55 //结果保存域 56 ResultModel resultModel = new ResultModel(); 57 58 SolrQuery query = new SolrQuery(); 59 //设置默认查询域 60 query.set("df", "product_keywords"); 61 //设置查询关键字 62 query.setQuery(StringUtils.isEmpty(keywords) ? "*:*" : "keywords"); 63 //添加过滤条件 64 if (StringUtils.isEmpty(catalogName)) 65 query.addFilterQuery("product_catalog_name:" + catalogName); 66 if (StringUtils.isEmpty(priceArea)) { 67 String[] prs = priceArea.split("-"); 68 if (prs .length == 2) 69 query.addFilterQuery("product_price:[" + prs[0] + " TO " + prs[1] + "]"); 70 else 71 query.addFilterQuery("product_price:[" + prs[0] + " TO *]"); 72 } 73 //排序条件 74 if (StringUtils.isEmpty(sort)) 75 query.addSort("product_price", "1".equals(sort)? ORDER.desc : ORDER.asc); 76 77 //设置查询页数 78 query.setRows(resultModel.getPageCount()); 79 if (pageNum != null) { 80 query.setStart(resultModel.getPageCount() * (pageNum - 1)); 81 } else { 82 query.setStart(0); 83 } 84 85 //设置信息高亮 86 query.setHighlight(true); 87 query.addHighlightField("product_name"); 88 query.setHighlightSimplePre("<font color='red'>"); 89 query.setHighlightSimplePost("</font>"); 90 91 //获取查询结果 92 QueryResponse response = solrServer.query(query); 93 //获取结果集 94 SolrDocumentList res = response.getResults(); 95 //获取记录总数 96 long rocoderNum = res.getNumFound(); 97 //获取高亮信息结果集 98 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); 99 //获取商品列表 100 List<Product> pros = new ArrayList<>(); 101 for (SolrDocument doc : res) { 102 Product p = new Product(); 103 p.setPid(doc.get("id").toString()); 104 p.setCatalog_name(doc.get("product_catalog_name").toString()); 105 p.setPrice(Float.parseFloat(doc.get("product_price").toString())); 106 p.setPicture(doc.get("product_picture").toString()); 107 108 //名称高亮 109 List<String> list = highlighting.get("id").get("product_name"); 110 if (list != null) 111 p.setName(list.get(0)); 112 113 pros.add(p); 114 } 115 116 //保存结果 117 resultModel.setCurPage(pageNum); 118 resultModel.setRecordCount(rocoderNum); 119 resultModel.setProductList(pros); 120 121 return resultModel; 122 } 123 124 125 }
注意:Solr7之后,HttpSolrServer已经没有了,取而代之是HttpSolrClient,而且创建方式也变了,使用Builder模式创建,所以这里用到了工厂方法
1 <!-- 配置SolrServer对象 实例工厂 --> 2 <!-- 单机版 --> 3 <bean id="httpSolrClientBuilder" class="org.apache.solr.client.solrj.impl.HttpSolrClient.Builder"> 4 <constructor-arg name="baseSolrUrl" value="${SOLR.SERVER.URL}"/> 5 </bean> 6 <bean id="httpSolrClient" factory-bean="httpSolrClientBuilder" factory-method="build"/> 7
资源配置文件中:单机版需要指定solrcore
SOLR.SERVER.URL = xxxxx:8983/solr/core1