http://archive.apache.org/dis...
解压之后进入bin目录,当前位置打开cmd,这里以jetty的方式启动:
solr start -p 8983
停止单个指定端口
solr stop -p 8983
停止所有的
solr stop -all
http://localhost :8983/solr
使用Core Admin菜单添加一个数据表,之后再对新建的core进行字段配置( 如不配置直接使用代码设置,相应的字段类型默认是数组类型
)
q : 查询条件,可以key=value进行查询,value可以试模糊值
fq: 过滤条件
sort : 排序,规则:field必须是索引的field;field不能是multivalued 多个值的
start,rows : 分页查询
fl : 返回的结果集只返回指定字段
df : 默认查询字段
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>2.1.6.RELEASE</version> </dependency>
spring: data: solr: host: http://127.0.0.1:8983/solr
/** * * 2019/11/4 14:20 * @description */ @Slf4j @RestController @RequestMapping("solr") public class SolrController { @Autowired private SolrClient solrClient; private final static String solrDataName = "disShops"; /** * 添加店铺信息至solr索引 * @return */ @RequestMapping("addShopToSolr") public ResultData addShopToSolr(){ List<DisCitys> disCitys = disCitysService.queryAllDisCitysList(); disCitys = disCitys.stream().filter(c->c.getAreaType()==2).collect(Collectors.toList()); disCitys.forEach(c->{ try { List<SolrInputDocument> docs = new ArrayList<>(); List<DisShopsResp> disShops = disShopsService.queryShopsByCityNo(c.getAreaNo(), null); disShops.forEach(d->{ SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", d.getShopId()); doc.setField("shopNo", d.getShopNo()); doc.setField("shopName", d.getShopName()); doc.setField("address", d.getAddress()); doc.setField("cityNo", d.getCityNo()); doc.setField("orderSeq", d.getOrderSeq()); docs.add(doc); }); if(docs.size()>0){ solrClient.add(solrDataName, docs); solrClient.commit(solrDataName); } } catch (Exception e) { log.error("添加店铺至solr出错,cityId:{},/n错误信息:{}", c.getAreaNo(), e.getMessage()); } }); return new ResultData<>(); } /** * solr查询 * @param queryParam * @param cityNo * @param page * @return */ @RequestMapping("searchShop") public ResultData searchShop(@RequestParam("queryParam") String queryParam, @RequestParam("cityNo") String cityNo, PageHelperUtil page){ try { SolrQuery query = new SolrQuery(); //查询参数q query.set("q", queryParam); //过滤条件 query.set("fq", "cityNo:"+cityNo); query.addSort("orderSeq", SolrQuery.ORDER.asc); query.setStart((page.getPage()-1)*page.getRows()); query.setRows(page.getRows()); //默认查找域 query.set("df", "shopName"); //高亮及相关域 设置前缀及后缀 query.setHighlight(true); query.addHighlightField("shopName"); query.setHighlightSimplePre("<span style='color:red'>"); query.setHighlightSimplePost("</span>"); QueryResponse response = solrClient.query(solrDataName, query); SolrDocumentList results = response.getResults(); long sumNum = results.getNumFound(); if(sumNum > 0){ //获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的 Map<String, Map<String, List<String>>> highlight = response.getHighlighting(); for (SolrDocument result : results) { Map<String, List<String>> map = highlight.get(result.get("id")); result.setField("shopName", map.get("shopName").get(0)); } } return new ResultData(results); } catch (Exception e) { log.error("查询失败:"+e.getMessage()); return null; } } /** * 删除所有索引 * @return */ @RequestMapping("delAllShopSolr") public ResultData delAllShopSolr(){ try { solrClient.deleteByQuery(solrDataName, "*:*"); solrClient.commit(solrDataName); } catch (Exception e) { log.error("删除失败:"+e.getMessage()); } return new ResultData(); } }
下载 IKAnalyzer
<lib dir="./lib" regex=".*/.jar"/>
<fieldType name="text_ik" class="solr.TextField"> <analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer"/> <analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType>
本文由博客一文多发平台 OpenWrite 发布!