版本参数
solr 7.3.0
solrj 7.3.0
spring-data 3.0.6 RELEASE
spring:5.0.5.RELEASE
搭建Solr并用DIM导入数据
搭建solr及数据导入的步骤可以参见之前的内容。
笔者已经建好了一个环境,Document结构如下
{
"responseHeader":{
"status":0,
"QTime":77,
"params":{
"q":"*:*",
"_":"1529904219206"}},
"response":{"numFound":1000,"start":0,"docs":[
{
"id":"1",
"song_title":"棒球狂 (口白)",
"singer_name":"BABOO乐团",
"genre":"Pop 流行",
"song_name":"棒球狂",
"language":"纯音乐",
"public_time":"2008-01-01T00:00:00Z",
"_version_":1602522394494238720},
{
"id":"2",
"song_title":"保丽龙",
"singer_name":"BABOO乐团",
"genre":"Pop 流行",
"song_name":"保丽龙",
"language":"纯音乐",
"public_time":"1992-05-01T00:00:00Z",
"_version_":1602522394529890304},
{
...
solr 与 spring-data 集成
创建项目
创建一个Maven项目,并配置pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.5.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
编写Model类
新建一个Music类, 该类与Solr中的MusicCore进行映射
@SolrDocument(solrCoreName = "MusicCore")
public class Music {
public static final String FIELD_RES_ID = "res_id";
public static final String FIELD_SONG_ID = "song_id";
public static final String FIELD_SONG_NAME = "song_name";
public static final String FIELD_SINGER_NAME = "singer_name";
@Id
@Indexed(name = FIELD_RES_ID, type="string")
private Long resId;
@Indexed(name = FIELD_SONG_ID, type="string")
private Long songId;
@Indexed(name = FIELD_SONG_NAME, type="string")
private String songName;
@Indexed(name = FIELD_SINGER_NAME, type="string")
private String singerName;
//TODO: 此处省略了get, set
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
SolrCoreName 为 在Solr中建立的Core对应的名字
@Id 为 Solr中的主键
@Indexed 为标注该属性与Solr中的索引对象的映射关系,name为solr中的命名, type为solr中的类型名,如pint, plong等。
创建Repository
新建MusicRepository,该接口用于实现对Solr中对应的Core进行增删查改操作
public interface MusicRepository extends SolrCrudRepository<Music, String> {
}
SolrCrudRepository 实现了大部分的常用方法,继承它后就可以进行普通的操作。SolrCrudRepository第一个参数为model类,即与document映射的类,第二个为逐渐类型
配置
首先新建一个solr.properties文件,用于存放solr的访问地址
solr.server.url=http://192.168.3.11:8983/solr/
再创建Solr的配置类,用于读取solr的链接,及注入相关对象。
@Configuration
@PropertySource("classpath:solr.properties")
@EnableSolrRepositories(basePackages = "com.ali.repository")
public class HttpSolrContext {
private static final String SOLR_SERVER_URL = "solr.server.url";
@Resource
private Environment environment;
@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(environment.getRequiredProperty(SOLR_SERVER_URL))
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
}
@Bean
public SolrTemplate solrTemplate(SolrClient client){
return new SolrTemplate(client);
}
}
测试
集成已经完成,添加测试代码来测试一下是否能与solr连接成功。
configTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HttpSolrContext.class)
public class configTest {
@Autowired
private MusicRepository musicRepository;
@Test
public void findOne(){
Optional<Music> music = musicRepository.findById("13558");
System.out.println(music);
}
}
最后,控制台确实打印了该对象
网友评论