SpringBoot integrates Solr [super detailed version]

Table of Contents

Introduction

what is solr

This document requires all relevant files

1. Start Linux, open and configure the solr server

Solr installation

Configure the Chinese analyzer IK Analyzer

Introduction to IK Analyzer

IK Analyzer configuration

2. Use of solr domain

Copy domain

3. SpringBoot integrates Solr

1. Configure xml dependencies

2. Configure yaml

3. Configuration template

4. Create entity class

5. Test


Introduction

What is solr

?Solr is a top open source project under Apache, developed in Java. It is a full-text search service based on Lucene. Solr provides a richer query language than Lucene, is configurable, scalable, and optimizes indexing and search performance. Solr can run independently in Servlet containers such as Jetty and Tomcat. The implementation method of Solr index is very simple. Use the POST method to send an XML document describing the Field and its content to the Solr server. Solr adds, deletes, and updates the index according to the XML document. Solr search only needs to send HTTP GET requests, and then parse the query results returned by Solr in Xml, json and other formats to organize the page layout. Solr does not provide functionality for building UI. Solr provides a management interface through which you can query the configuration and operation of Solr.

In summary, Solr is a scalable, deployable, search/storage engine optimized for searching large amounts of text-centric data.

This document requires all related files

Link: https://pan.baidu.com/s/1qlmHdVzeBymYhXPuU0upnA?pwd=5432
Extraction code: 5432

1. Start Linux and configure the solr server

First, put the solr.war package into linux

Solr installation

1: Install Tomcat and unzip it.

2: Unzip the downloaded solr-4.10.3.zip

3: Deploy the dist directory solr-4.10.3.war under solr-4.10.3 to Tomcat\webapps in Linux (remove the version number).

  • Remove the version number by copying and pasting cp solr-4.10.3.war solr
  • Remove the version number through mv solr-4.10.3.war solr cut and paste method

4: Start Tomcat to decompress the war package

5: Add all jar packages in the example/lib/ext directory under solr-4.10.3solr to the solr project (\WEB-INF\lib directory).

6: Place the /example/solr directory under solr-4.10.3solr into /usr/local/ in Linux and change the name to solrhome

  • First put the /example/solr directory under solr-4.10.3solr into linux
  • Change the name through mv solr solrhome

7. Find the webapp/solr/WEB-INF/web.xml file in tomcat

8. Modify the web.xml file

9: Restart Tomcat

http://own ip:port number/solr/

Configure Chinese Analyzer IK Analyzer

Introduction to IK Analyzer

IK Analyzer is an open source, lightweight Chinese word segmentation toolkit developed based on Java language. Since the launch of version 1.0 in December 2006, IKAnalyzer has launched 4 major versions. Initially, it was a Chinese word segmentation component based on the open source project Luence, which combined dictionary word segmentation and grammatical analysis algorithms. Starting from version 3.0, IK has developed into a public word segmentation component for Java, independent of the Lucene project, and provides a default optimized implementation of Lucene. In the 2012 version, IK implemented a simple word segmentation ambiguity elimination algorithm, marking the evolution of the IK word segmenter from simple dictionary word segmentation to simulated semantic word segmentation.

IK Analyzer Configuration

1. Unzip IK Analyzer 2012FF_hf1.zip in the relevant files

2. Add IKAnalyzer2012FF_u1.jar to the lib directory of the /tomcat/webapp/solr/ project

3. Create classes under WEB-INF

4. Place stopword.dic and IKAnalyzer.cfg.xml in the decompressed IK Analyzer 2012FF_hf1 into the WEB-INF/classes directory of the solr project.

5. Modify the schema.xml file of solrhome/collection1/conf, configure a FieldType, and use IKAnalyzer

<fieldType name="text_ik" class="solr.TextField">
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

6. Restart tomcat

2. Use of solr domain

Fields are equivalent to table fields in a database. Users store data, so users define relevant Fields according to business needs. Generally speaking, each type corresponds to a type of data, and users perform the same operations on the same type of data.

Common properties of domains:

  • name: the name of the specified domain
  • type: Specify the type of domain
  • indexed: whether to index
  • stored: whether to store
  • required: whether it is required
  • multiValued: whether it is multi-valued

In general: when we want to query data, we set the domain ourselves to find the corresponding value information

If you want to query data by name, you need to configure the domain name and other information in your ownschema.xml file toset Business SystemField

as follows:

I configured two domains in schema.xml. The names are item_goodsid and item_title respectively. Since item_title involves Chinese, type uses the Chinese parser type just configured.

Copy domain

The purpose of copying a field is to copy the data in a Field to another field.

as follows:

I configured a replication domain and copied an item_title. Of course, I can configure many, so that I can query all the information in the replication domain only by querying item_keywords

Okay, that’s about it for the above configurations. Next, let’s enter the SpringBoot integration SolrDemo link.

3. SpringBoot integrates Solr

The springboot version is: 2.3.4.RELEASE

1. Configure xml dependencies

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

    </dependency>

2. Configure yaml

spring:
  data:
    Solr:
      host: http://own ip:own port number/solr

3. Configuration template

import org.apache.solr.client.solrj.SolrClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;

@Configuration
public class SolrConfig {

    @Bean
    public SolrTemplate solrTemplate(SolrClient solrClient){

        return new SolrTemplate(solrClient);
    }

}

4. Create entity class

public class TbItem implements Serializable{

@Field
    private Long id;

@Field("item_title")
    private String title;
//get set omitted


}

5. Test

@SpringBootTest
public class SolrUtilTest {

    @Autowired
    SolrTemplate solrTemplate;

    @Autowired
    ItemMapper itemMapper;
    @Test
    public void testAdd(){
        TbItem item=new TbItem();
item.setId(1L);
item.setTitle("Huawei Mate9");
       
        solrTemplate.saveBeans("collection1",item);
        solrTemplate.commit("collection1");
    }

    //Conditional query
 @Test
public void testPageQueryMutil(){
Query query=new SimpleQuery("*:*");
Criteria criteria=new Criteria("item_title");
query.addCriteria(criteria);
\t\t
ScoredPage<Item> items = solrTemplate.queryForPage("collection1", query, Item.class);
                System.out.print(items);
}
}

Okay, the above is the end of the demo session, go and use it!