Solr 是Apache的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。
Solr能够独立运行在Jetty、Tomcat等这些Servlet容器中,Solr 索引的实现方法很简单,用 POST 方法向 Solr 服务器发送一个描述 Field 及其内容的 XML 文档,Solr根据xml文档添加、删除、更新索引 。Solr 搜索只须要发送 HTTP GET 请求,而后对 Solr 返回Xml、json等格式的查询结果进行解析,组织页面布局。Solr不提供构建UI的功能,Solr提供了一个管理界面,经过管理界面能够查询Solr的配置和运行状况。mysql
拉取 Solr 镜像:docker pull solr:7.4.0
git
启动 Solr 容器docker run --name taotao-solr -d -p 8983:8983 -t solr:7.4.0
github
访问 http://114.115.215.xxx:8983/ web
Solr界面功能spring
docker exec -it --user=solr mysolr bin/solr create_core -c shop
分词器安装参考文章:https://github.com/ik-analyzersql
文件传输传输参考:Docker容器和本机之间的文件传输docker
wget方式把jar包下载到根目录:数据库
wget https://search.maven.org/remotecontent?filepath=com/github/magese/ik-analyzer/7.7.0/ik-analyzer-7.7.0.jar
找到容器长ID:docker inspect my-solr | grep Id
apache
将jar包放入Solr服务的webapp/WEB-INF/lib/目录下json
docker cp /root/ik-analyzer-7.6.0.jar 9d7cf2c5315e405e7a8cadc40dcef93fd893f39f0cf70ad42c3b1b70729d2423:/opt/solr/server/solr-webapp/webapp/WEB-INF/lib
将resources目录下的5个配置文件放入solr服务的webapp/WEB-INF/classes/目录下;
① IKAnalyzer.cfg.xml ② ext.dic ③ stopword.dic ④ ik.conf ⑤ dynamicdic.txt
进入solr容器:docker exec -it -u root my-solr /bin/bash
编辑managed-schema文件:vim /opt/solr/server/solr/shop/conf/managed-schema
把下面一段加到最后
<!-- ik分词器 --> <fieldType name="text_ik" class="solr.TextField"> <analyzer type="index"> <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType> <!-- 设置业务系统Field --> <field name="item_title" type="text_ik" indexed="true" stored="true"/> <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/> <field name="item_price" type="plong" indexed="true" stored="true"/> <field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category_name" type="string" indexed="true" stored="true" /> <field name="item_desc" type="text_ik" indexed="true" stored="false" /> <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="item_title" dest="item_keywords"/> <copyField source="item_sell_point" dest="item_keywords"/> <copyField source="item_category_name" dest="item_keywords"/> <copyField source="item_desc" dest="item_keywords"/>
别忘了重启容器:docker restart my-solr
代码解释:加了type="text_ik"
表示这个字段使用分词器分拆,<copyField source="item_title" dest="item_keywords"/>
表示把这个字段加入一个以前建立新的集合item_keywords
中,之后使用这个字段检索可达到全文检索的目的。
新建springboot项目,加依赖:
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>7.7.1</version> </dependency>
配置文件添加:
spring.data.solr.host=http://114.115.215.xxx:8983/solr
核心代码:
@RestController public class HelloController { @Autowired private SolrClient client; @RequestMapping("/hello") public Object hello() throws IOException, SolrServerException { SolrInputDocument doc = new SolrInputDocument(); List<ItemInfo> list= itemInfoMapper.findAll(); doc.setField("item_title","Apple iPhone X (A1865) 64GB 深空灰色 移动联通电信4G手机"); doc.setField("item_sell_point","【抢券立减200元!】5.8英寸视网膜全面屏,无线充电,面容ID,1200万后置双摄。"); doc.setField("item_price",6299.00); doc.setField("item_category_name","手机"); doc.setField("item_desc","单卡单待网络制式:4G LTE全网通机身内存:64GB4G LTE网络特性:移动4G"); client.add("shop",doc); client.commit("shop"); return "ok"; } }
测试:
为何这么多数据,不是只加了一条嘛? 我已经走完了下面一步,这里只是为了演示给你看。
先把mysql、mybatis依赖所有加进来
新建实体类
public class Item { private Long id; private String title; private String sell_point; private Long price; private String image; private String category_name; }
dao层
public interface ItemInfoMapper { @Select("SELECT\n" + "\ta.id,\n" + "\ta.title,\n" + "\ta.sell_point,\n" + "\ta.price,\n" + "\ta.image,\n" + "\tb. NAME category_name\n" + "FROM\n" + "\ttb_item a\n" + "LEFT JOIN tb_item_cat b ON a.cid = b.id") List<ItemInfo> findAll(); }
记得让springboot扫描哦
@SpringBootApplication @MapperScan("com.yungou.shop.shopsearch.Mapper") public class ShopSearchApplication { public static void main(String[] args) { SpringApplication.run(ShopSearchApplication.class, args); } }
下面开发核心代码了,登登登登:
@RestController public class HelloController { @Autowired private SolrClient client; @Autowired private ItemInfoMapper itemInfoMapper; @RequestMapping("/hello") public Object hello() throws IOException, SolrServerException { SolrInputDocument doc = new SolrInputDocument(); List<ItemInfo> list= itemInfoMapper.findAll(); for (ItemInfo itemInfo:list){ doc.setField("item_title",itemInfo.getTitle()); doc.setField("item_sell_point",itemInfo.getSell_point()); doc.setField("item_price",itemInfo.getPrice()); doc.setField("item_category_name",itemInfo.getCategory_name()); client.add("shop",doc); } client.commit("shop"); return "ok"+list.size(); } }
让数据加载一会,数据多的话可能要等几分钟。
回到管理界面
咱们看到已经有6666条数据加载进来了,大功告成,666!
通过这个案例,应该是把线上部署的大多数状况都过了一遍。享受docker部署便利的同时,因为第一次操做docker文件,这里耗了点时间。最蠢的事情是明明加入的是text_ik
,本身一直在搜ik
,怎么搜来搜去搜不到,明明已经启动成功了呀。而后弄来弄去才发现,也费了点时间。警戒本身之后必定要细心,注意细节呀。除此以外,其余的都比较顺利。