搜索解决方案-Solr

本文介绍了Apache Solr作为开源搜索服务器的强大功能,包括其与Tomcat集成、中文分析器IKAnalyzer的配置,以及如何在Spring Data Solr中实现搜索功能和数据模型设置。重点讲解了安装步骤、Schema配置和数据管理实践。
摘要由CSDN通过智能技术生成

1什么是Solr?
大多数搜索引擎应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能。
这就是为什么转移负载到一个外部的搜索服务器是一个不错的主意,Apache Solr是一个流行的开源搜索服务器,它通过使用类似REST的HTTP API,这就确保你能从几乎任何编程语言来使用solr。
Solr是一个开源搜索平台,用于构建搜索应用程序。 它建立在Lucene(全文搜索引擎)之上。 Solr是企业级的,快速的和高度可扩展的。 使用Solr构建的应用程序非常复杂,可提供高性能。
为了在CNET网络的公司网站上添加搜索功能,Yonik Seely于2004年创建了Solr。并在2006年1月,它成为Apache软件基金会下的一个开源项目。并于2016年发布最新版本Solr 6.0,支持并行SQL查询的执行。
Solr可以和Hadoop一起使用。由于Hadoop处理大量数据,Solr帮助我们从这么大的源中找到所需的信息。不仅限于搜索,Solr也可以用于存储目的。像其他NoSQL数据库一样,它是一种非关系数据存储和处理技术。
总之,Solr是一个可扩展的,可部署,搜索/存储引擎,优化搜索大量以文本为中心的数据。
2 Solr安装
官方网址:https://lucene.apache.org/solr/downloads.html
历史版本:http://archive.apache.org/dist/lucene/solr/
1:安装 Tomcat,解压缩即可。
2:解压 solr。
3:把 solr 下的dist目录solr-4.10.3.war部署到 Tomcat\webapps下(去掉版本号)。
4:启动 Tomcat解压缩 war 包
5:把solr下example/lib/ext 目录下的所有的 jar 包,添加到 solr 的工程中(\WEB-INF\lib目录下)。
6:创建一个 solrhome 。solr 下的/example/solr 目录就是一个 solrhome。复制此目录到D盘改名为solrhome
7:关联 solr 及 solrhome。需要修改 solr 工程的 web.xml 文件。

<env-entry>
	<env-entry-name>solr/home</env-entry-name>
	<env-entry-value>F:\20200728\solrtools\solrhome</env-entry-value>
	<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

粘贴到solr 工程的 web.xml 文件的最后,修改F:\20200728\solrtools\solrhome
8:启动 Tomcat
http://IP:8080/solr/
在这里插入图片描述

3中文分析器IK Analyzer
IK Analyzer 是一个开源的,基亍 java 语言开发的轻量级的中文分词工具包。从 2006年 12 月推出 1.0 版开始, IKAnalyzer 已经推出了 4 个大版本。最初,它是以开源项目Luence 为应用主体的,结合词典分词和文法分析算法的中文分词组件。从 3.0 版本开始,IK 发展为面向 Java 的公用分词组件,独立亍 Lucene 项目,同时提供了对 Lucene 的默认优化实现。在 2012 版本中,IK 实现了简单的分词歧义排除算法,标志着 IK 分词器从单纯的词典分词向模拟语义分词衍化。
IK Analyzer配置
步骤:
1、把IKAnalyzer2012FF_u1.jar 添加到 solr 工程的 lib 目录下
2、创建WEB-INF/classes文件夹 把扩展词典、停用词词典、配置文件放到 solr 工程的 WEB-INF/classes 目录下。
3、修改 Solrhome 的solrhome\collection1\conf\schema.xml 文件,配置一个 FieldType,使用 IKAnalyzer

<fieldType name="text_ik" class="solr.TextField">
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

http://127.0.0.1:8080/solr/
在这里插入图片描述

配置自己的扩展字典
1.在apache-tomcat服务器的webapps\solr\WEB-INF\classes,创建一个ext.dic文件
2.apache-tomcat服务器的webapps\solr\WEB-INF\classes中的IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<properties>  
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典-->
	<entry key="ext_dict">ext.dic;</entry> 
	<!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">stopword.dic;</entry> 
</properties>

3.在ext.dic文件中添加自己需要的词语

配置域
域相当于数据库的表字段,用户存放数据,因此用户根据业务需要去定义相关的Field(域),一般来说,每一种对应着一种数据,用户对同一种数据进行相同的操作。
域的常用属性:
name:指定域的名称
type:指定域的类型
indexed:是否索引
stored:是否存储
required:是否必须
multiValued:是否多值

修改solrhome的schema.xml 文件 设置业务系统 Field

<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />

复制域
复制域的作用在于将某一个Field中的数据复制到另一个域中

<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"/>

动态域
当我们需要动态扩充字段时,我们需要使用动态域。对于品优购,规格的值是不确定的,所以我们需要使用动态域来实现。需要实现的效果如下:
在这里插入图片描述

配置:

Spring Data Solr入门
(1)创建maven工程,pom.xml中引入依赖

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-solr</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

2.完善项目
3.在src/main/resources下创建 applicationContext-solr.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: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/data/solr
      http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd">
    <!-- solr服务器地址 -->
    <solr:solr-server id="solrServer" url="http://127.0.0.1:8080/solr" />
    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>
</beans>

4.创建com.wangxing.datasolr.bean,将品优购的TbItem实体类拷入本工程,属性使用@Field注解标识 。如果属性与配置文件定义的域名称不一致,需要在注解中指定域名称。

public class TbItem implements Serializable {
    @Field
    private Long id;
    @Field("item_title")
    private String title;
    private String sellPoint;
    @Field("item_price")
    private BigDecimal price;
    private Integer stockCount;
    private Integer num;
    private String barcode;
    @Field("item_image")
    private String image;
    private Long categoryid;
    private String status;
	private Date createTime;
    private Date updateTime;
    private String itemSn;
    private BigDecimal costPirce;
    private BigDecimal marketPrice;
    private String isDefault;
    @Field("item_goodsid")
    private Long goodsId;
    private String sellerId;
    private String cartThumbnail;
    @Field("item_category")
    private String category;
    @Field("item_brand")
    private String brand;
    private String spec;
    @Field("item_seller")
    private String seller;
......
}

增加
创建测试类TestMain .java

package com.wangxing.springdatasolrdemo;

import com.wangxing.datasolr.bean.TbItem;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.solr.core.SolrTemplate;

import java.math.BigDecimal;

public class TestMain {
    public static void main(String[] args) {
        ApplicationContext  ac=new ClassPathXmlApplicationContext("applicationContext-solr.xml");
        SolrTemplate solrTemplate=(SolrTemplate)ac.getBean("solrTemplate");
        //测试添加
        TbItem item=new TbItem();
        item.setId(2L);
        item.setBrand("苹果");
        item.setCategory("笔记本");
        item.setGoodsId(2L);
        item.setSeller("苹果专卖店");
        item.setTitle("苹果 12");
        item.setPrice(new BigDecimal(8000));
        solrTemplate.saveBean(item);
        solrTemplate.commit();
    }
}

在这里插入图片描述

修改

 public static void main(String[] args) {
        ApplicationContext  ac=new ClassPathXmlApplicationContext("applicationContext-solr.xml");
        SolrTemplate solrTemplate=(SolrTemplate)ac.getBean("solrTemplate");
        //测试添加
        TbItem item=new TbItem();
        item.setId(2L);
        item.setBrand("苹果");
        item.setCategory("手机");
        item.setGoodsId(2L);
        item.setSeller("苹果手机专卖店");
        item.setTitle("苹果 iPhone12");
        item.setPrice(new BigDecimal(5000));
        solrTemplate.saveBean(item);
        solrTemplate.commit();
    }
//修改与添加操作相同,只要id一样就会修改。

主键查询

//测试主键查询
TbItem item= solrTemplate.getById(1,TbItem.class);
solrTemplate.commit();
System.out.println(item.getTitle());
分页查询
为了测试分页查询,我们为solr添加一组数据
for(long i=3;i<=50;i++) {
    TbItem item = new TbItem();
    item.setId(i);
    item.setBrand("苹果-"+i);
    item.setCategory("手机");
    item.setGoodsId(i);
    item.setSeller("苹果手机专卖店");
    item.setTitle("苹果 iphone-"+i);
    item.setPrice(new BigDecimal(5000));
    solrTemplate.saveBean(item);
    solrTemplate.commit();
}
//分页查询
//创建查询对象
Query query=new SimpleQuery("*:*");
//设置查询的开始索引(默认0)【(当前页码-1)*每页记录数】
query.setOffset(10);
//设置每页记录数(默认10)
query.setRows(10);
//执行查询
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List<TbItem> list = page.getContent();
for(TbItem item:list){
    System.out.println(item.getTitle());
}

条件查询
Criteria 用于对条件的封装:

Query query=new SimpleQuery("*:*");
//并且
//Criteria criteria=new Criteria("item_title").contains("2");
//criteria=criteria.and("item_title").contains("5");
//或者
//Criteria criteria=new Criteria("item_title").contains("2");
//criteria=criteria.or("item_title").contains("5");
Criteria criteria=new Criteria("item_title").contains("华为");
query.addCriteria(criteria);
//query.setOffset(20);//开始索引(默认0)
//query.setRows(20);//每页记录数(默认10)
ScoredPage<TbItem> page = solrTemplate.queryForPage(query, TbItem.class);
System.out.println("总记录数:"+page.getTotalElements());
List<TbItem> list = page.getContent();
for(TbItem item:list){
    System.out.println(item.getTitle());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值