太仓住房城乡建设网站,长沙3合1网站建设电话,wordpress安装包文件夹,石狮市网站建设学习了一下https://github.com/TyCoding/ssm-redis-solr这个github上的solr搜索功能#xff0c;现在来记录一下。 我的理解就是solr有点类似于数据库#xff0c;但它是有索引的数据库#xff0c;按很多字段建立索引#xff0c;可能是b树或者散列索引#xff0c;然后就能够…  学习了一下https://github.com/TyCoding/ssm-redis-solr这个github上的solr搜索功能现在来记录一下。 我的理解就是solr有点类似于数据库但它是有索引的数据库按很多字段建立索引可能是b树或者散列索引然后就能够实现海量数据的查找。solr通过导入jar包就可以对这个库就行增删改查了后端逃不掉的增删改查。。。  1.配置tomcat 具体我就不说了因为我是直接用了github上配置好的毕竟站在巨人的肩膀上学习嘛 地址https://github.com/TyCoding/solr-tomcat 2.访问solr并使用 访问端口:localhost:8080/solr/index.html 这里的new_core就是项目中配置的路径就将商品的索引放在这里。  然后用Test测试它的使用测试的时候要引入配置文件不然会导致空指针错误我居然现在才知道。怪不得以前只要用Autowired的时候就会空指针错误。。而且还要Runwith注解引入包import org.springframework.test.context.junit4.*;eclipse点击不会有import提示需要自己加上去。     这里新建了一个实体对象然后把这个实体对象加入到索引库里在solr索引库里面就可以找到这个字段  在new_core的schema里面就以Id建好了索引  以及很多的信息  Testpublic void testFindById() {Goods goods  solrTemplate.getById(1, Goods.class);System.out.println(--------  goods.getTitle());}  通过id查找控制台会输出你刚刚插入的数据也就是通过solrTemplate找到了你的数据。  Testpublic void testAddList() {ListGoods list  new ArrayListGoods();//循环插入100条数据for (int i  0; i  100; i) {BigDecimal pricenew BigDecimal (2.3);Goods goods  new Goods(i  1L, 华为Mate  i,price, 手机, 手机, 华为专卖店);list.add(goods);}solrTemplate.saveBeans(list); //添加集合对象调用saveBeans()添加普通对象类型数据使用saveBean();solrTemplate.commit(); //提交}  还可以批量插入数据或者分页查询  Testpublic void testPageQuery() {Query query  new SimpleQuery(*:*);query.setOffset(20); //开始索引默认0query.setRows(20); //每页记录数默认10ScoredPageGoods page  solrTemplate.queryForPage(query, Goods.class);System.out.println(总记录数  page.getTotalElements());ListGoods list  page.getContent();}  3.学习一下项目中怎么配置 注意要在web.xml加一个过滤不然注入不了solrTemplate这个bean    spring-solr.xml  ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:solrhttp://www.springframework.org/schema/data/solrxsi:schemaLocationhttp://www.springframework.org/schema/data/solrhttp://www.springframework.org/schema/data/solr/spring-solr-1.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd!-- solr服务器地址 --solr:solr-server idsolrServer urlhttp://127.0.0.1:8080/solr/new_core/!-- solr模板使用solr模板可对索引库进行CRUD的操作 --bean idsolrTemplate classorg.springframework.data.solr.core.SolrTemplateconstructor-arg refsolrServer//bean/beans  就是加载一个solr的模板   SolrUtil.java 把数据库的数据库批量加入  Component
public class SolrUtil {Autowiredprivate GoodsMapper goodsMapper;Autowiredprivate SolrTemplate solrTemplate;/*** 实现将数据库中的数据批量导入到Solr索引库中*/public void importGoodsData() {ListGoods list  goodsMapper.findAll();System.out.println(商品列表);for (Goods goods : list) {System.out.println(goods.getTitle());}solrTemplate.saveBeans(list);solrTemplate.commit(); //提交System.out.println(结束);}public static void main(String[] args) {ApplicationContext context  new ClassPathXmlApplicationContext(classpath:spring/spring*.xml);SolrUtil solrUtil  (SolrUtil) context.getBean(solrUtil);solrUtil.importGoodsData();}
}     这样就把数据加入索引库中。 实体类有一个Field标识这个实体字段在索引库里的名称   Fieldprivate Long id; //商品IDField(item_title)private String title; //商品标题Field(item_price)private BigDecimal price; //商品价格Field(item_image)private String image; //商品图片Field(item_category)private String category; //商品类别Field(item_brand)private String brand; //商品品牌Field(item_seller)private String seller; //商品卖家  最后搜索功能的实现 按价格查找   //按价格区间查询if (searchMap.get(price) ! null) {if (!searchMap.get(price).equals()) {String[] price  ((String) searchMap.get(price)).split(-);if (!price[0].equals(0)) {//如果起点区间不等于0Criteria filterCriteria  new Criteria(item_price).greaterThanEqual(price[0]);FilterQuery filterQuery  new SimpleFilterQuery(filterCriteria);query.addFilterQuery(filterQuery);}if (!price[1].equals(*)) {//如果区间重点不等于*Criteria filterCriteria  new Criteria(item_price).lessThanEqual(price[1]);FilterQuery filterQuery  new SimpleFilterQuery(filterCriteria);query.addFilterQuery(filterQuery);}}}   4.实现效果     转载于:https://www.cnblogs.com/HannahLihui/p/10104416.html