solr初次使用

    科技2022-07-11  85

    1.引入jar包

    <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>1.5.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.github.promeg</groupId> <artifactId>tinypinyin</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>com.offcn</groupId> <artifactId>dongyimai_dao</artifactId> <version>1.0</version> </dependency> <!--转汉语拼音--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.github.promeg</groupId> <artifactId>tinypinyin</artifactId> <version>2.0.3</version> </dependency> </dependencies>

    2.配置xml文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd"> <context:component-scan base-package="com.offcn.util"/> <!--配置solr的连接--> <solr:solr-server url="http://192.168.188.128:8983/solr/collection1" id="solrServer"/> <!--声明solrTemplate--> <bean class="org.springframework.data.solr.core.SolrTemplate" id="solrTemplate"> <constructor-arg ref="solrServer"/> </bean> </beans>

    3.demo展示

    package com.offcn.util; import com.alibaba.fastjson.JSON; import com.github.promeg.pinyinhelper.Pinyin; import com.offcn.mapper.TbItemMapper; import com.offcn.pojo.TbItem; import com.offcn.pojo.TbItemCatExample; import com.offcn.pojo.TbItemExample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.Query; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; //批量导入,solr的工具类 @Component public class SolrUtils { @Autowired private TbItemMapper tbItemMapper; @Autowired private SolrTemplate solrTemplate; public void importTtem(){ //查询审核通过的,mybatis TbItemExample tbItemExample = new TbItemExample(); TbItemExample.Criteria criteria = tbItemExample.createCriteria(); criteria.andStatusEqualTo("1"); List<TbItem> list = tbItemMapper.selectByExample(tbItemExample); for (TbItem item:list){ Map<String,String> specMap = JSON.parseObject(item.getSpec(), Map.class); System.out.println(specMap); Map<String,String> pinyinMap = new HashMap<>(); //遍历key值,转拼音(数据格式:{"网络":"移动4G","机身内存":"64G"}) for (String key:specMap.keySet()){ //将key做拼音转化 System.out.println(key); pinyinMap.put( Pinyin.toPinyin(key,"").toLowerCase(),specMap.get(key)); } item.setSpecMap(pinyinMap); System.out.println(item.getTitle()+"==="+item.getPrice()); } solrTemplate.saveBeans(list); solrTemplate.commit(); System.out.println("导入成功!!!!!"); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/spring/applicationContext-*.xml"); SolrUtils solrUtils = (SolrUtils)context.getBean("solrUtils"); solrUtils.importTtem(); // solrUtils.delete(); } public void delete(){ Query query = new SimpleQuery("*:*"); solrTemplate.delete(query); solrTemplate.commit(); } }
    Processed: 0.009, SQL: 8