Adding, deleting, modifying and querying solr index by springboot

Keywords: Programming solr Spring Java Apache

solr version: 8.x

springboot version: 1.5.6

1. To prepare the springboot project, pom.xml needs to introduce the following jar package

<properties>
    <project.final.name>htsolr</project.final.name>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--solr Supported versions-->
    <spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version>
</properties>
 
<!--solr Supported versions-->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
</dependency>
 
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>${spring.data.solr.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Mainly introduce two support packages of spring data Solr, and paste the whole pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	
	<!-- Configuration parameters of the project -->
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.hornet.solr</groupId>
	<artifactId>ht-server-solr</artifactId>
	<packaging>jar</packaging>
	<name>ht-server-solr</name>
	<description>operation solr project</description>
	
	<!-- Define constants used by some projects -->
	<properties>
		<project.final.name>htsolr</project.final.name>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version>
	</properties>
	
	<!-- Introduce springboot Parent class pom Explain -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
	</parent>
	
	<dependencies>
		<!-- Introduce springboot Support -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>
 
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
        </dependency>
		
	</dependencies>
	
	<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-solr</artifactId>
                <version>${spring.data.solr.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
	
	<!-- Pack -->
	<build>
		<finalName>${project.final.name}</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	
</project>

2. In terms of the project directory structure, the whole demo sample project mainly includes three parts: controller class, startup class, and properties configuration file

Document description:

SolrApplication.java //Startup class

SolrController.java //The method and example of web request based on solr

application-dev.properties //Project related configuration, provided that the environment in application.properties should be configured as dev

In addition, the logback log plug-in is used in the project. In the following code, if your environment does not use it, replace all logs with syso output.  

3. Main function source code

SolrApplication.java

package com.hornet;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * solr Background service demo start portal
 * 
 * @author avatar
 * @since 2019 April 23, 2015 3:15:15 PM
 */
@SpringBootApplication
public class SolrApplication {
 
	/**
	 * Program entry
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(SolrApplication.class);
	}
 
}

 SolrController.java

package com.hornet.solr.web;
 
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * solr Adding, deleting, modifying and querying the demo controller
 * 
 * @author avatar
 * @since 2019 3:21:46 PM, April 23, 2004
 */
@RestController
@RequestMapping("/solr")
public class SolrController {
	
	/** Log record */
	private static final Logger log = LoggerFactory.getLogger(SolrController.class);
 
    @Autowired
    private SolrClient client;
 
    /**
     * Add or modify the index. When the id exists, it is update. If the id does not exist, it is new
     * @return
     */
    @RequestMapping("addOrUpdate")
    public String addOrUpdate() {
    	//No duplicate UUID, current existing data starts from id=10
    	String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    	try {
    		SolrInputDocument doc = new SolrInputDocument();
    		doc.setField("id", uuid);
    		doc.setField("name", "Little medical Fairy");
    		doc.setField("sex", 0);
    		doc.setField("address", "2008, Dragon Island, Yincheng, five families of Douqi");
    		doc.setField("isDeleted", 0);
    		//If spring.data.solr.host is configured to demo core, there is no need to transfer demo core
            client.add("demo_core", doc);
            client.commit("demo_core");
            log.info("Newly added/Update succeeded!");
            return "Newly added/Update succeeded!";
        } catch (Exception e) {
            e.printStackTrace();
        }
    	return null;
    }
 
    /**
     * Delete index by id
     * 
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(String id)  {
        try {
            client.deleteById("demo_core",id);
            client.commit("demo_core");
            log.info("delete id=[{}]Success!",id);
            return "Delete successfully!";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * Delete all indexes
     * 
     * @return
     */
    @RequestMapping("deleteAll")
    public String deleteAll(){
        try {
            client.deleteByQuery("demo_core","*:*");
            client.commit("demo_core");
            log.info("Delete all indexes successfully!");
            return "Delete all indexes successfully!";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * Query index by id
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("getById")
    public String getById(String id) throws Exception {
        SolrDocument document = client.getById("demo_core", id);
        log.info("according to id=[{}]Index query succeeded!", id);
        log.info("The query result is:{}", document.toString());
        return document.toString();
    }
 
    /**
     * Comprehensive query: query by conditions, sorting, paging, highlighting, and some domain information
     * 
     * @return
     */
    @RequestMapping("search")
    public String search(String q){
        try {
            SolrQuery params = new SolrQuery();
            //Query condition, corresponding value of q
            params.set("q", q);
            //Filter conditions, multiple filter conditions can be added
            //params.set("fq", "updateTime:[2019-04-19 08:10:59 TO 2019-04-19 09:10:59]");
            //Sort, do not transfer default sort
            params.addSort("id", SolrQuery.ORDER.asc);
            //Paging parameters
            params.setStart(0);
            params.setRows(20);
            //Default domain
            params.set("df", "address");
            //Query only the specified domain and specify the display field
            params.set("fl", "id,name,address");
            //Highlight
            //Turn on the switch.
            params.setHighlight(true);
            //Specify highlight field
            params.addHighlightField("address");
            //Highlight css to set prefix
            params.setHighlightSimplePre("<span style='background-color:yellow;'>");
            //Highlight css to set suffix
            params.setHighlightSimplePost("</span>");
            //Highlight to set the maximum length of each slice
            params.setHighlightFragsize(100000);
 
            QueryResponse queryResponse = client.query("demo_core", params);
            SolrDocumentList results = queryResponse.getResults();
            long numFound = results.getNumFound();
            log.info("The number of query results is:{}strip",numFound);
            log.info("The query result is:{}",results.toString());
            
            //Get the highlighted results. The highlighted results and query results are open
            Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();
            log.info("The query results are highlighted as:{}",highlight.toString());
//            for (SolrDocument result : results) {
//                Map<String, List<String>> map = highlight.get(result.get("id"));
//                List<String> list = map.get("address");
//                log.info(list.get(0));
//            }
            return results.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}

application.properties

spring.profiles.active=dev

application-dev.properties 

#Development environment profile
 
#Development mode
debug=false
#Specify the address of the server binding
server.address=127.0.0.1
#Service start port
server.port=9983
 
#solr service configuration
spring.data.solr.host=http://127.0.0.1:8983/solr
 

4. After the configuration of springboot project is completed, the project is started, and the test tool uses postman, and the test work is carried out below

4.1. Add / update index

Request address: localhost:9983/solr/addOrUpdate

4.2. Query index according to id

Take the ID value inserted in step 4.1 to query, id=a6573f43864d49dba6ff4bcef3c1d2aa

Request address: localhost:9983/solr/getById?id=a6573f43864d49dba6ff4bcef3c1d2aa

4.3. Delete index according to id

It is the id value of this data to delete. id=a6573f43864d49dba6ff4bcef3c1d2aa

Request address: localhost:9983/solr/delete?id=a6573f43864d49dba6ff4bcef3c1d2aa

 

4.4. Delete all indexes (omitted)

4.5. Comprehensive query

Before the test, insert the data of xiaoyixian into solr to facilitate the test. First, look at the data found by solr management platform:

We find out 4 pieces of data in total, and use postman to test as follows:

Request address: localhost:9983/solr/search?q=address: Mainland

Highlight the queried result data:

First, take a look at the highlighted data found by the solr management platform:

Highlighting is actually to add CSS highlighting code before and after the queried fields, and control the specific highlighted style freely.

Let's use postman test to see how to get the highlighted data in the code:

localhost:9983/solr/search?q=address:Mainland 

Or start the request, and then look at the code logic. The corresponding highlighted data will be output successfully:

At this point, the spring boot integration solr to add, delete, modify and query the normal test operation is completed.  

Posted by j007w on Tue, 24 Dec 2019 02:11:39 -0800