solr安装
注意:需要提前配置好linux下的java环境
1. 上传压缩包至linux,并解压到/usr/local/solr目录下(软件\solr\solr-7.2.1.tgz)
2. 进入解压目录,进入bin目录,启动:
./solr start -force //root用户启动需要增加 -force
3. 访问:Solr服务的默认服务端口是8983,此刻访问你的 http://IP:8983 , 应该可以看到Solr的控制面板了。
访问 8983端口 连接失败,原因可能有两个:
(1). Solr 服务没有启动,启动即可,可以通过命令查看Solr的当前状态
cd /usr/local/solr/bin
./solr status
(2). 打开防火墙端口
firewall-cmd --add-port=8983/tcp --permanent
firewall-cmd --reload
4. 创建core:core是solr的特有概念,每个core是一个查询数据、索引等的集合体,你可以把它想象成一个独立数据库,我们创建一个新core:名字[collection1]
cd /usr/solr/bin
./solr create -c collection1 -force // -c 指定要创建的Core名称 root用户启动需要增加 -force
中文分析器IK Analyzer
1. 下载IKAnalyzer for solr5的源码包,然后使用Maven编译,得到一个文件IKAnalyzer-5.0.jar
https://github.com/EugenePig/ik-analyzer-solr5
2. 上传ik jar包到/usr/local/solr/server/solr-webapp/webapp/WEB-INF/lib目录中
3. 修改/usr/local/solr/server/solr/collection1/conf/managed-schema,添加fieldType内容:
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" useSmart="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" useSmart="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
然后重启solr
配置域
域相当于数据库的表字段,用户存放数据,因此用户根据业务需要去定义相关的Field(域),一般来说,每一种对应着一种数据,用户对同一种数据进行相同的操作。
name 指定域的名称 type 指定域的类型 indexed 是否索引
stored 是否存储 required 是否必须 multiValued 是否多值
修改/usr/local/solr/server/solr/collection1/conf/managed-schema,设置业务系统 Field
删除多余的field,保留id,_version_,_text_这三个field
<field name="item_goodsid" type="plong" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="pdouble" 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 name="item_updatetime" type="pdate" 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"/>
动态域
当我们需要动态扩充字段时,就需要使用动态域。对于东易买,规格的值是不确定的,所以我们需要使用动态域来实现。如:
配置
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
入门小案例
1. 创建maven工程,引入依赖
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.5.5.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.12</version>
</dependency>
</dependencies>
2. 在resources下创建 spring-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/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://192.168.188.102:8983/solr/collection1" />
<!-- solr模板,使用solr模板可对索引库进行CRUD操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
@Field 注解
创建 com.offcn.pojo 包,实体类拷入本工程 ,属性使用@Field注解标识 。 如果属性与solr配置文件定义的域名称不一致,需要在注解中指定域名称。
public class TbItem implements Serializable{
@Field
private Long id;
@Field("item_title")
private String title;
@Field("item_price")
private BigDecimal price;
@Field("item_image")
private String image;
@Field("item_updatetime")
private Date updateTime;
@Field("item_goodsid")
private Long goodsId;
@Field("item_category")
private String category;
@Field("item_brand")
private String brand;
@Field("item_seller")
private String seller;
.......
}
增加(修改)
创建测试类TestTemplate.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-solr.xml")
public class TestTemplate{
@Autowired
private SolrTemplate solrTemplate;
public void testAdd(){
TbItem item=new TbItem();
item.setId(3L);
item.setBrand("小米");
item.setCategory("手机pluse");
item.setGoodsId("1L");
item.setSeller("小米1号专卖店");
item.setTitle("红米Mate9");
item.setPrice(new BigDecimal(2200));
solrTemplate.saveBean(item);
solrTemplate.commit();
}
}
按主键查询
@Test
public void testFindOne(){
TbItem item = solrTemplate.getById(1,TbItem.class);
System.out.println(item.getTitle);
}
按主键删除
@Test
public void testDelete(){
solrTemplate.deleteById("1");
solrTemplate.commit();
}
分页查询
@Test
public void testAddList(){
List<TbItem> list=new ArrayList();
for(int i=1;i<101;i++){
TbItem item=new TbItem();
item.setId(Long.valueOf(i));
item.setBrand("华为");
item.setCategory("手机");
item.setGoodsId(1L);
item.setSeller("华为"+i+"号专卖店");
item.setTitle("华为Mate"+i);
item.setPrice(new BigDecimal(2000+i));
list.add(item);
}
solrTemplate.saveBeans(list);
solrTemplate.commit();
}
分页查找
@Test
public void testPageQuery() {
Query query=new SimpleQuery("*:*");
query.setOffset(10);//开始索引
query.setRows(20);//每页记录数
ScorePage<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() + item.getPrice());
}
}
条件查询
@Test
public void testPageQueryMutil() {
Query query = new SimpleQuery("*:*");
Criteria criteria = new Criteria("item_title").contains("2");
criteria = criteria.and("item_price").greaterThan(2020);
query.addCriteria(criteria);
Sort s = new Sort(Direction.DESC, "item_price");
query.addSort(s);
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() + "," + item.getPrice());
}
}
删除全部
@Test
public void testDeleteAll(){
Query query=new SimpleQuery("*:*");
solrTemplate.delete(query);
solrTemplate.commit();
}