Spring Boot series (ix) Spring boot integrated SORL search Client

Source: Internet
Author: User
Tags apache solr solr solr query
Spring Boot series (ix) Spring boot integrated sorl search client

This article describes the Spring boot integrated sorl search client, which requires springboot full video tutorial, click here.


Apache SOLR is a search engine. Spring boot provides a basic configuration for the SOLR client library and the abstraction of the SOLR client-based library provided by spring Data SOLR. Spring Boot provides a spring-boot-starter-data-solr ' starter POM ' for aggregation dependencies.


Introducing SPRING-BOOT-STARTER-DATA-SOLR dependencies, add the following in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>


You can inject an automatically configured Solrserver instance like any other spring beans. By default, the instance will attempt to connect to a server using LOCALHOST:8983/SOLR.


@Component
public class Mybean {
Private Solrserver SOLR;
@Autowired
Public Mybean (Solrserver SOLR) {
THIS.SOLR = SOLR;
}
// ...
}


If you add a @bean of your own solrserver type, it will replace the default.


Application integration SOLR Search client case

Spring Boot configuration is centralized (can be split into different configuration files), so add the following configuration to the Application.properties file:


# SOLR (solrproperties)
Spring.data.solr.host=http://localhost:8983/solr
#spring. data.solr.zkhost=
Spring.data.solr.repositories.enabled=true


Using SPRING-DATA-SOLR similar to SPRING-DATA-JPA, configure @bean to accept ZK server-related properties (custom configuration method, you can use the default mode directly)


Import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties (prefix= "SPRING.SOLR")
public class Solrconfig {
Private String host;
Private String Zkhost;
Private String defaultcollection;
Public String getdefaultcollection () {
return defaultcollection;
}
public void Setdefaultcollection (String defaultcollection) {
This.defaultcollection = defaultcollection;
}
Public String GetHost () {
return host;
}
public void Sethost (String host) {
This.host = host;
}
Public String Getzkhost () {
return zkhost;
}
public void Setzkhost (String zkhost) {
This.zkhost = Zkhost;
}


Configure the Solrserver service with the following code:


@Configuration
@EnableConfigurationProperties (Solrconfig.class)
public class Solrclientconfig {
@Autowired
Private Solrconfig Solrconfig;
Private Cloudsolrserver Solrserver;
@PreDestroy
public void Close () {
if (this.solrserver! = null) {
try {
This.solrServer.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

Public Cloudsolrserver Solrserver () {
if (Stringutils.hastext (This.solrConfig.getZkHost ())) {
Solrserver = new Cloudsolrserver (This.solrConfig.getZkHost ());
Solrserver.setdefaultcollection (This.solrConfig.getDefaultCollection ());
}
return this.solrserver;
}
}


To test the SOLR query, the code is as follows:


@RestController
public class Hellocontroller {

@Autowired
Private Cloudsolrserver Solrserver;


Public String Hello () {
Return "Say hello";
}
@RequestMapping ("Test")
public void Test () {

Modifiablesolrparams params = new Modifiablesolrparams ();
Params.add ("Q", "Demo: Motobumi blog");
Params.add ("ws", "JSON");
Params.add ("Start", "0");
Params.add ("Rows", "10");
Queryresponse response = null;

try{
Response=solrserver.query (params);
Solrdocumentlist results = Response.getresults ();
for (Solrdocument document:results) {
System.out.println (Document.getfieldvalue ("demo"));
System.out.println (Document.getfieldvalue ("id"));
}
}catch (Exception e) {
E.getstacktrace ();
}
System.out.println (Response.tostring ());
}
}


Source: Motobumi Blog

Links: https://blog.yoodb.com/yoodb/article/detail/1412

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.