今日目标:
1.solr的安装与配置
2.solr的核心配置文件
3.配置业务域
4.spring Date Solr集成到项目
5.批量数据导入
一、Solr的介绍
1.solr是什么?
大多数搜索引擎应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能。
这就是为什么转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr 是一个流行的开源搜索服务器,它通过使用类似REST 的 HTTP API,这就确保你能从几乎任何编程语言来使用 solr。
Solr 是一个开源搜索平台,用于构建搜索应用程序。 它建立在 Lucene(全文搜索引擎)之上。 Solr 是企业级的,快速的和高度可扩展的。 使用 Solr 构建的应用程序非常复杂,可提供高性能。
为了在CNET网络的公司网站上添加搜索功能,Yonik Seely 于 2004 年创建了 Solr。并在2006 年 1 月,它成为 Apache软件基金会下的一个开源项目。并于 2016 年发布最新版本 Solr6.0,支持并行 SQL查询的执行。Solr 可以和Hadoop 一起使用。由于 Hadoop 处理大量数据,Solr 帮助我们从这么大的源中找到所需的信息。不仅限于搜索,Solr 也可以用于存储目的。像其他 NoSQL 数据库一样,
它是一种非关系数据存储和处理技术。
总之,Solr 是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的
数据。
2.安装和部署
1.首先解压solr的文件
2.solr是war工程所以直接找到war工程解压到tomcat中即可
我们查看tomcat的日志文件可以看到如下错误信息
十二月 16, 2018 3:52:48 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter SolrRequestFilter
java.lang.NoClassDefFoundError: Failed to initialize Apache Solr: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see: http://wiki.apache.org/solr/SolrLogging
at org.apache.solr.servlet.CheckLoggingConfiguration.check(CheckLoggingConfiguration.java:28)
错误原因是:缺少相关的jar包,我们根据提示拷贝相关的jar到solr项目的lib包下
3.创建本地索引库
我们修改名字为solrhome 见名之意
4.接下来,我们还要配置,让solr知道我们创建的solrhome的位置,在这另一个方法是,修改tomcat的配置文件,不建议用。
重新启动tomcat如图
查看日志,有警告
5.我们处理一下上面的警告,虽然不影响使用,有点强迫症,理解万岁
6.修改配置文件
该目录下
警告已经解决
8.核心schema.xml的解析
中文分词:Lucene 默认逐字分词。
solr默认也是逐字分词,需要集成中文分词器 ik分词器。
solr域字段配置:
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
field:配置域字段的标签
name:字段名称
type:字段类型
indexed:是否索引。含义:是否可以根据该字段进行搜索操作。true可以根据该字段搜索,false,不能基于该字段进去全文检索。
stored:是否存储。含义:该字段内容是否存储在索引库中。
业务:是否存储,基于页面是否需要展示决定的。需要展示时,存储。
solr域字段类型配置:
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
fieldType:配置域字段类型的标签
class:域字段类型对应的solr类
sortMissingLast:是否可以根据该字段类型排序
<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
动态域:
作用:简化配置域字段。
<field name="item_i" type="int" indexed="true" stored="true"/>
<field name="item1_i" type="int" indexed="true" stored="true"/>
<field name="item2_i" type="int" indexed="true" stored="true"/>
<field name="item3_i" type="int" indexed="true" stored="true"/>
<field name="item4_i" type="int" indexed="true" stored="true"/>
...
<copyField source="cat" dest="text"/>
复制域:解决多字段搜索功能。例如:商品关键字搜索
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
source:源域
dest:目标域
二、Spring Data Solr入门
详情请见day10-1 springData solr入门
三、批量导入数据
solr集成IK分词器 第三方分词器
中文分词:
优势:动态添加扩展词:高富帅、白富美
动态停用停用词:的、得、地
1、导入ik的jar包到solr工程中
2、导入ik配置文件到solr工程的classes类路径下
3、 <!-- 配置业务域字段类型,使用ik分词器 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
1.业务相关的域字段
2.首先编写一个工具类获取数据的数据
@Component
public class SolrUtil {
@Autowired
private TbItemMapper itemMapper;
@Autowired
private SolrTemplate solrTemplate;
public void dataImport(){
//从数据库中查询满足条件的的商品列表数据 注意查询的是符合条件的: 上架的 状态为 1的
List<TbItem> itemList = itemMapper.findAllGrounding();
for (TbItem item : itemList) {
String spec = item.getSpec();
//组装动态域属性
Map<String,String> map = JSON.parseObject(spec, Map.class);
item.setSpecMap(map);
}
//2.将查询的结果导入到索引库
solrTemplate.saveBeans(itemList);
solrTemplate.commit();
System.out.println("itemList import finished.....");
}
}
3.在mapper.xml的映射文件中添加
<select id="findAllGrounding" resultMap="BaseResultMap">
SELECT item.* FROM tb_item item,tb_goods goods WHERE item.goods_id=goods.id AND item.status='1' AND goods.is_marketable='1'
</select>
4.在Tbitem中配置动态域
满足条件的商品数据导入到索引库
业务:什么商品导入索引库?
1、上架商品导入索引库 tb_goods is_marketable='1'
2、商品状态为1,正常状态 tb_item status='1'
/* 配置规格动态域 */ @Dynamic @Field("item_spec_*") private Map<String,String> specMap;
5.编写测试方法导入数据
/** * 导入商品数据 */ @Test public void dataImport(){ solrUtil.dataImport(); }
四、今日总结
域和复制域 和 动态域