Solr介紹
Solr是一個(gè)高性能崩哩,采用Java5開發(fā)巡球,基于Lucene的全文搜索服務(wù)器。同時(shí)對(duì)其進(jìn)行了擴(kuò)展琢锋,提供了比Lucene更為豐富的查詢語(yǔ)言辕漂,同時(shí)實(shí)現(xiàn)了可配置、可擴(kuò)展并對(duì)查詢性能進(jìn)行了優(yōu)化吴超,并且提供了一個(gè)完善的功能管理界面钉嘹,是一款非常優(yōu)秀的全文搜索引擎。
SpringBoot集成Solr
- 添加springboot集成solr的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
- application.yml指向solr地址
spring:
data:
solr:
host: http://192.168.0.197:8983/solr/ik_core
- 添加solr實(shí)時(shí)更新操作
@SpringBootApplication
@EnableScheduling
public class SpringbootSolrApplication {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RestTemplateBuilder builder;
@Autowired
private RestTemplate restTemplate;
// 使用RestTemplateBuilder來實(shí)例化RestTemplate對(duì)象鲸阻,spring默認(rèn)已經(jīng)注入了RestTemplateBuilder實(shí)例
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
public static void main(String[] args) {
SpringApplication.run(SpringbootSolrApplication.class, args);
}
//每五秒執(zhí)行一次
@Scheduled(cron = "0/5 * * * * *")
public void updateSolr() {
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
postParameters.add("command", "full-import");
postParameters.add("verbose", "false");
postParameters.add("clean", "true");
postParameters.add("commit", "true");
postParameters.add("core", "ik_core");
postParameters.add("name", "dataimport");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity <MultiValueMap <String, Object>> r = new HttpEntity<>(postParameters, headers);
String time = String.valueOf(new Date().getTime());
String url = "http://192.168.0.197:8983/solr/ik_core/dataimport?_=" + time + "&indent=on&wt=json";
String responseMessage = restTemplate.postForObject(url, r, String.class);
logger.info("更新solr索引:返回值:{}", responseMessage);
}
}
- 測(cè)試查詢(增加了高亮顯示)
@Test
public void test() throws IOException, SolrServerException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "title:*");
solrQuery.set("start", 0);
solrQuery.set("rows", 20);
//======高亮設(shè)置===
//開啟高亮
solrQuery.setHighlight(true);
//高亮域
solrQuery.addHighlightField("title");
//前綴
solrQuery.setHighlightSimplePre("<span style='color:red'>");
//后綴
solrQuery.setHighlightSimplePost("</span>");
QueryResponse response = solrClient.query(solrQuery);
SolrDocumentList results = response.getResults();
System.out.println("查詢內(nèi)容:" + solrQuery);
System.out.println("文檔數(shù)量:" + results.getNumFound());
System.out.println("查詢花費(fèi)時(shí)間:" + response.getQTime());
//獲取高亮信息
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for (SolrDocument solrDocument :results) {
System.out.println(solrDocument);
System.out.println(solrDocument.getFieldValue("cover"));
System.out.println(solrDocument.getFieldValue("service_area"));
//輸出高亮
Map<String, List<String>> map = highlighting.get(solrDocument.get("id"));
List<String> list = map.get("title");
if(list != null && list.size() > 0){
System.out.println(list.get(0));
}
}
}