Solr的基本语法
//高亮查询 @Override public Map<String, Object> searchList(Map<String, Object> map) { System.out.println(map); //不写泛型返回的话,会出现IO异常 Map<String,Object> map1 = new HashMap(); //获取前端传递的当前页码和展示数量 Integer pageNo = (Integer)map.get("pageNo"); Integer pageSize = (Integer)map.get("pageSize"); if (pageNo==null || pageNo<=0){ pageNo=1; } pageNo = (pageNo-1)*pageSize; //高亮查询 HighlightQuery query = new SimpleHighlightQuery(); //起始索引,是当前页码-1*当前展示数量 query.setOffset(pageNo); query.setRows(pageSize); //高亮属性的设置 HighlightOptions options = new HighlightOptions(); //设定属性名,属性名必须与solr字段名一致, options.addField("item_title"); //高亮属性的设置前缀 options.setSimplePrefix("<em style='color:red'>"); //高亮属性的设置后缀 options.setSimplePostfix("</em>"); //设置高亮选项 query.setHighlightOptions(options); //--------------获取前台传的关键字,进行空格消除 String keywords = (String)map.get("keywords"); String s = keywords.replaceAll(" ", ""); //查询条件的设置 Criteria criteria = new Criteria("item_keywords").is(s); String spec =(String) map.get("category"); List<Map> list = JSON.parseArray(spec, Map.class); //添加复杂查询 query.addCriteria(criteria); //----------------过滤查询----------------- //根据分类进行查询 String category = (String) map.get("category"); if (!"".equals(category) && category!=null){ Criteria criteria1 = new Criteria("item_category").is(category); FilterQuery query1 = new SimpleQuery(criteria); //放入查询对象中 query.addFilterQuery(query1); } //根据品牌进行复杂查询 String brand =(String) map.get("brand"); if (brand!=null && !"".equals(brand)){ Criteria criteria1 = new Criteria("item_brand").is(brand); FilterQuery query1 = new SimpleQuery(criteria1); //放入查询对象中 query.addFilterQuery(query1); } //价格区间查询 String price = (String) map.get("price"); if (!"".equals(price) && price!=null){ String[] split = price.split("-"); if (!"0".equals(split[0])) { //如果第一个不是0,就按照大于第一个数字价格去查询 Criteria criteria1 = new Criteria("item_price").greaterThan(split[0]); FilterQuery query1 = new SimpleQuery(criteria1); query.addFilterQuery(query1); } if (!"*".equals(split[1])){ //如果第二个数字不是* ,就按照小于第二个数字价格去查询 Criteria criteria1 = new Criteria("item_price").lessThan(split[1]); FilterQuery query1 = new SimpleQuery(criteria1); query.addFilterQuery(query1); } } //排序查询 String sortValue = (String)map.get("sort"); //price,updatetime String sortField = (String) map.get("sortField");//ASC,DESC if (sortValue!="" && sortValue.length()>0){ if ("ASC".equals(sortValue)){ //排序查询语句 Sort sort = new Sort(Sort.Direction.ASC, "item_"+sortField); query.addSort(sort); } if ("DESC".equals(sortValue)){ Sort sort = new Sort(Sort.Direction.DESC, "item_"+sortField); query.addSort(sort); } } //根据规格查询 Map<String,String> spec1 = (Map<String,String>)map.get("spec"); System.out.println(spec1); Set<Map.Entry<String, String>> entries = spec1.entrySet(); for (Map.Entry<String, String> entry : entries) { Criteria criteria1 = new Criteria("item_spec_"+entry.getKey()).is(entry.getValue()); FilterQuery query1 = new SimpleQuery(criteria1); query.addFilterQuery(query1); } HighlightPage<Item> page = solrTemplate.queryForHighlightPage(query, Item.class); //5、遍历实体集合,将符合要求的当西设置高亮 //华为 mate <em style='color:green'>华为mate</em> for (HighlightEntry<Item> h : page.getHighlighted()) {//循环高亮入口 Item item = h.getEntity();//获取原实体 //h.getHighlights()高亮的内容, if (h.getHighlights().size() >0 && h.getHighlights().get(0).getSnipplets().size()>0){ //因为上面的复杂查询不能够直接去查询,我们要将符合要求的结果拿出来,在设置格式,再赋值给对象 item.setTitle(h.getHighlights().get(0).getSnipplets().get(0)); } } List<Item> content = page.getContent(); //将list集合 map1.put("total",page.getTotalElements()); map1.put("totalPages",page.getTotalPages()); //所有数据 map1.put("rows",content); //------------------1高亮数据 //------------------2分类数据 List categiryList = searchCategoryList(map); //将分类数据放入map,传入前台 map1.put("categoryList",categiryList); //-------------------3品牌数据 if (categiryList.size()>0) { map1.putAll(searchBrandAndSpecList((String)categiryList.get(0))); } return map1; }