solr7.2.0的安装以及springboot整合solr
本案例使用的是solr7.2.0内嵌jeety服务器,solr5之前的版本都需要自己在tomcat上去搭建。
solr7.2.0下载地址:http://archive.apache.org/dist/lucene/solr/7.2.0/
1.解压安装
2.进入solr的bin目录启动
3.访问默认端口8983(新建core这里会报出错误因为新建的core中没有conf配置文件-看下一个步骤)
4.进入以下目录将conf拷贝到刚/solr-7.2.0/server/solr/new_core中(拷贝时cp -r 。。。迭代拷贝)
cp -r conf ../../../solr/new_core/
5.添加属性
***这是添加的属性***
(1).(属性文件的配置有一点小坑需要注意solr默认按照一个text的属性查找如果删除了这个属性会报):
"SolrException: undefined field text"的错误
解决方法参考:https://blog.****.net/zhangchao19890805/article/details/54602711
(2).managed-schema(schema.xml文件)具体的文件属性含义:
参考:https://blog.****.net/sun5769675/article/details/50717453
6.springboot中引入依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
7.添加solr的连接配置
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: root redis: database: 0 host: localhost port: 6379 password: rabbitmq: username: bobo password: bobo host: localhost virtual-host: facelive port: 5672 data: solr: host: http://127.0.0.1:8983/solr/
8.创建对应的类和repository
/** * @author bobo * @date 18-4-24 */ @SolrDocument(solrCoreName = "new_core") @Data public class ShortSolrVO { @Field("id") private Integer id; @Field("coinId") private Integer coinId; @Field("type") private String type; @Field("value") private String value; }
/** * @author bobo * @date 18-4-24 */ public interface SearchSolrRepository extends SolrCrudRepository<ShortSolrVO, String> { /** * @param key * @param pageable * @return */ @Query("?0") List<ShortSolrVO> findAll(String key, Pageable pageable); }
9.service层(加入分页pageable因为findAll是自定义方法需要加 new Solr()排序):
/**
* @author bobo
* @date 18-4-24
*/
@Service
public class SearchSolrService {
private static final int DEFAULT_SEARCH_LIMIT = 50;
private static final String SEARCH_TEMPLATE = "(all:{key} OR type:{key}^3 OR value:{key}*^2 ";
private SearchSolrRepository searchSolrRepository;
private SolrClient solrClient;
@Autowired
public SearchSolrService(SearchSolrRepository searchSolrRepository, SolrClient solrClient) {
this.searchSolrRepository = searchSolrRepository;
}
public List<ShortSolrVO> listSolrShort(String key) {
Pageable pageable = new PageRequest(0, DEFAULT_SEARCH_LIMIT,new Sort(Sort.Direction.DESC, "id"));
return searchSolrRepository.findAll(buildSearchKeyword(key), pageable);
}
/**
* build search keyword
*
* @param keyword
* @return
*/
private static String buildSearchKeyword(String keyword) {
String[] tokens = keyword.split("\\s+");
String searchKeyword = "";
for (String item : tokens) {
searchKeyword += "AND " + SEARCH_TEMPLATE.replace("{key}", item);
}
return searchKeyword.substring(4);
}
}
10.结果展示:
集群的使用:https://segmentfault.com/a/1190000011619699
solrl连接mysql的使用:https://blog.****.net/u014378181/article/details/79454998/