一. 简介
solr是以lucene为内核开发的企业级搜索应用 应用程序可以通过http请求方式来提交索引,查询索引,提供了比lucene更丰富的查询语言,是一个高性能,高可用环境全文搜索引擎。
二. 安装环境
[root@localhost ~]# docker pull solr:5.5.5
docker exec -it --user=solr my_solr bin/solr create_core -c mycore
[root@localhost ~]# docker exec -it --user=solr my_solr bin/solr create_core -c mycore
Copying configuration to new core instance directory:
/opt/solr/server/solr/mycore
Creating new core 'mycore' using command:
http://localhost:8983/solr/admin/cores?action=CREATE&name=mycore&instanceDir=mycore
{
"responseHeader":{
"status":0,
"QTime":3037},
"core":"mycore"}
[root@localhost ~]#
访问 192.168.229.130:8983出现的页面如下:
模拟数据
{
"id":"1",
"title":"我爱中国",
"content":"中国地大物博"
}
找到 IKAnalyzer类 需要重写 protected TokenStreamComponents createComponents(String fieldName) 方法
找到 IKTokenizer类 需要重写构造方法 public IKTokenizer(Reader in, boolean useSmart) 为 public IKTokenizer(boolean useSmart)
进行打包,将修改后的类替换到新包中上传至linux上
./server/solr-webapp/webapp/WEB-INF/lib
//动态字段
//字段类型
[root@localhost ik]# docker stop my_solr
my_solr
[root@localhost ik]# docker cp ./managed-schema my_solr:/opt/solr/server/solr/mycore/conf/
[root@localhost ik]# docker start my_solr
my_solr
[root@localhost ik]#
solr@localhost:/opt/solr$ cp /opt/solr/dist/solr-dataimporthandler-5.5.5.jar /opt/solr/server/solr-webapp/webapp/WEB-INF/lib
solr@localhost:/opt/solr$ cp /opt/solr/dist/solr-dataimporthandler-extras-5.5.5.jar /opt/solr/server/solr-webapp/webapp/WEB-INF/lib
[root@localhost ik]# docker cp ./mysql-connector-java-5.1.24.jar my_solr:/opt/solr/server/solr-webapp/webapp/WEB-INF/lib
[root@localhost ik]# docker cp ./data-c.xml my_solr:/opt/solr/server/solr/mycore/conf
[root@localhost ik]# docker cp my_solr:/opt/solr/server/solr/mycore/conf/solrconfig.xml .
data-c.xml
[root@localhost ik]# docker cp ./solrconfig.xml my_solr:/opt/solr/server/solr/mycore/conf
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-solr
spring:
data:
solr:
host: http://192.168.229.130:8983/solr
server:
port: 8888
@RestController
public class SolrController {
@Autowired
private SolrTemplate solrTemplate;
@GetMapping("queryNews")
public List queryNews(String keyword){
SimpleQuery simpleQuery = new SimpleQuery("newtitle_ik:"+keyword);
Page query = solrTemplate.query(simpleQuery, News.class);
return query.getContent();
}
}
Insert title here
新闻:
@SpringBootApplication
public class SolrMain {
@Bean
public SolrTemplate solrTemplate(SolrClient client) {
return new SolrTemplate(client);
}
public static void main(String[] args) {
SpringApplication.run(SolrMain.class, args);
}
}
@SolrDocument(solrCoreName="mycore")
public class News {
private String id;
@Field(value="newtitle_ik")
private String title ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;
import lombok.Data;
@Data
@SolrDocument(solrCoreName="mycore")
public class Person {
private String id ;
@Field("country_ik")
private String country;
@Field("desc_ik")
private String desc;
@Field("age_i")
private String age;
}
{"id":"1","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"旧金山","age_i":"30","name_ik":"John","desc_ik":"John is come from austrina John,s Dad is Johh Super"}
{"id":"2","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"好莱坞","age_i":"40","name_ik":"Mike","desc_ik":"Mike is come from austrina Mike,s Dad is Mike Super"}
{"id":"3","country_ik":"美国","provice_ik":"加利福尼亚州","city_ik":"圣地牙哥","age_i":"50","name_ik":"Cherry","desc_ik":"Cherry is come from austrina Cherry,s Dad is Cherry Super"}
{"id":"4","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"休斯顿","age_i":"60","name_ik":"Miya","desc_ik":"Miya is come from austrina Miya,s Dad is Miya Super"}
{"id":"5","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"大学城","age_i":"70","name_ik":"fubos","desc_ik":"fubos is come from austrina fubos,s Dad is fubos Super"}
{"id":"6","country_ik":"美国","provice_ik":"德克萨斯州","city_ik":"麦亚伦","age_i":"20","name_ik":"marry","desc_ik":"marry is come from austrina marry,s Dad is marry Super"}
{"id":"7","country_ik":"中国","provice_ik":"湖南省","city_ik":"长沙市","age_i":"18","name_ik":"张三","desc_ik":"张三来自长沙市 是公务员一名"}
{"id":"8","country_ik":"中国","provice_ik":"湖南省","city_ik":"岳阳市","age_i":"15","name_ik":"李四","desc_ik":"李四来自岳阳市 是一名清洁工"}
{"id":"9","country_ik":"中国","provice_ik":"湖南省","city_ik":"株洲市","age_i":"33","name_ik":"李光四","desc_ik":"李光四 老家岳阳市 来自株洲 是李四的侄子"}
{"id":"10","country_ik":"中国","provice_ik":"广东省","city_ik":"深圳市","age_i":"67","name_ik":"王五","desc_ik":"王五来自深圳市 是来自深圳的一名海关缉私精英"}
{"id":"11","country_ik":"中国","provice_ik":"广东省","city_ik":"广州市","age_i":"89","name_ik":"王冠宇","desc_ik":"王冠宇是王五的儿子"}
-
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public Page findByDesc(String keyword,Pageable page);
public Page findByCountry(String keyword,Pageable page);
@GetMapping("/queryNews")
public Page queryNews(String keyword){
PageRequest pr = new PageRequest(0,2,new Sort(Direction.ASC,"age_i"));
return ps.findByCountry(keyword,pr);
}