Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate solr in springboot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to integrate solr in springboot, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

First, the introduction of the version:

Jdk1.8

Tomcat8

Springboot 2.1.3RELEASE (there is a pit here, see below for details)

Solr 7.4.0 (the latest version is not selected because the boot version of the project is 2.1.3 and its corresponding solr-solrj.jar version is 7.4.0. In order to avoid unpredictable and irresistible problems, choose the same version carefully)

2. Build and download solr server

The download of 1.tomcat8 does not go into detail.

2.solr download: go to the solr official website and find the historical version to download the v7.4.0 compression package. In fact, solr archive himself did not successfully access it.

If netizens can't access it as I do, it only means your face is black, and the official website should be broken, so I certainly have no problem. Here is a resource from the Internet to share, with a compressed package of ik participle, which is useful later: Baidu network disk extraction code: 6mhk

Decompression

1. Extract solr to D:\ JAVA\ solr\ solr-7.4.0\ (the full path is replaced by solr-7.4.0 below)

two。 Extract tomcat to D:\ JAVA\ solr\ apache-tomcat-8.5.42 (this full path is replaced by tomcat-8.5.42 below)

Configure solrhome

1. Create a new folder solrhome under the path D:\ JAVA\ solr\ (the full path is replaced by solrhome below)

two。 Create a new logs folder under solrhome (write down this path: d:\ JAVA\ solr\ solrhome\ logs)

3. Copy the folders solr-7.4.0\ contrib and solr-7.4.0\ dist to solrhome

4. Copy all files under solr-7.4.0\ server\ solr to solrhome

The specific code is as follows:

Data-config.xml

7. Create a new file data-config.xml under solrhome\ new_core\ conf (the file name is the same as the above xml configuration), and the file content is as follows:

8. Modify conf\ managed-schema

If the ik word splitter is configured, you can modify it here:

Configure tomcat

1. Copy the webapp folder under solr-7.4.0\ server\ solr-webapp\ to tomcat-8.5.42\ webapps and change the name to solr

two。 Create a new classes package under tomcat-8.5.42\ webapps\ solr\ WEB-INF\ and copy solr-7.4.0\ server\ resources\ log4j2.xml to the classes package.

3. Copy the following illustration jar to tomcat-8.5.42\ webapps\ solr\ WEB-INF\ lib (the database driver package can be found in your web project and copied to this directory as well)

Modify D:\ JAVA\ solr\ apache-tomcat-8.5.42\ webapps\ solr\ WEB-INF\ web.xml as follows:

Solr/home D:\ JAVA\ solr\ solrhome java.lang.String

Modify the tomcat port number.... conf\ server.xml to 8888

Modify D:\ JAVA\ solr\ apache-tomcat-8.5.42\ bin\ catalina.bat (the last line is newly added)

If not "% JSSE_OPTS%" = = "goto gotJsseOptsset JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048 ": gotJsseOptsset" JAVA_OPTS=%JAVA_OPTS%% JSSE_OPTS% "set" JAVA_OPTS=%JAVA_OPTS%-Dsolr.log.dir=D:\ JAVA\ solr\ solrhome\ logs "ik Separator Integration

Put the two jar unzipped by the ik word splitter under tomcat-8.5.42\ webapps\ solr\ WEB-INF\ lib, and then configure solrhome\ new_core\ conf\ managed-schema to add at the end of the file:

III. Solr experience

Start tomcat and visit http://localhost:8888/solr/index.html#/

Parse Chinese sentences with ik. The matching test is as follows

Retrieval test

Use solr to configure pom org.springframework.boot spring-boot-starter-data-solr in springboot to configure ymlspring: data: solr: host: http://127.0.0.1:8888/solr

Note: if you are using springboot 2.1.3RELEASE, follow the steps below and the startup project will report an error

Hint: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

It is said to be a bug of springboot 2.1.3RELEASE, which needs to be configured in yml:

Spring: main: allow-bean-definition-overriding: true # whether override registration is allowed to write a demo when the same name is encountered, as follows:

1.entity:

Import org.apache.solr.client.solrj.beans.Field;import org.springframework.data.solr.core.mapping.SolrDocument;@SolrDocument (collection= "new_core") public class SolrDemo {@ org.springframework.data.annotation.Id private String id; @ Field ("demoName") private String name; / / omit getset}

2.dao:

Import org.jeecg.modules.demo.test.entity.SolrDemo;import org.springframework.data.solr.repository.SolrCrudRepository;import org.springframework.stereotype.Repository;@Repositorypublic interface SolrDemoRepository extends SolrCrudRepository {}

3.service interface

Import java.io.IOException;import java.util.List;import org.apache.solr.client.solrj.SolrServerException;import org.jeecg.modules.demo.test.entity.SolrDemo;public interface ISolrService {void addDemo (); void updatedemo (); void deletedemo (); List queryList (String keyword) throws SolrServerException, IOException;}

4.service implementation class

Import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList Import org.jeecg.modules.demo.test.entity.SolrDemo;import org.jeecg.modules.demo.test.mapper.SolrDemoRepository;import org.jeecg.modules.demo.test.service.ISolrService;import org.springframework.stereotype.Service;@Servicepublic class SolrServiceImpl implements ISolrService {@ Resource private SolrDemoRepository solrDemoRepository @ Override public void addDemo () {/ / normally, parameters should be defined in the method to pass the entity in, where demo simply says SolrDemo demo = new SolrDemo (); demo.setId ("001"); demo.setName ("We are all casual "); solrDemoRepository.save (demo) } @ Override public void updatedemo () {SolrDemo demo = new SolrDemo (); demo.setId ("001"); demo.setName ("what are you doing to sweep the topic"); solrDemoRepository.save (demo) } @ Override public void deletedemo () {solrDemoRepository.deleteById ("001") } @ Override public List queryList (String keyword) throws SolrServerException, IOException {/ * * there is a trick here. Normally, you can directly inject SolrClient into service, * but because the address configured in yml is http://127.0.0.1:8888/solr/, the injected SolrClient will report an error if it executes the following query * We have no choice but to play it this way. If netizens have a good solution, please leave a message below * or call the query method in SolrCrudRepository (I need to query and page according to specific fields, and welcome to post sample code) * personal guess is that springboot has been encapsulated twice and instantiated different SolrClient according to the notes on the entity? * * / String solrUrl = "http://127.0.0.1:8888/solr/new_core"; SolrClient testSolrClient = new HttpSolrClient.Builder (solrUrl) .withConnectionTimeout (10000) .withSocketTimeout (60000) .build (); SolrQuery query = new SolrQuery () / / set query criteria query.setQuery ("demoName:" + keyword); / / sort by time / / query.addSort ("create_time", SolrQuery.ORDER.desc); / / start page query.setStart (0) / / how many query.setRows (50) are displayed on a page; / / turn on highlight / / query.setHighlight (true); / / set highlight field / / query.addHighlightField ("demoName"); / / prefix / / query.setHighlightSimplePre ("") / / suffix / / query.setHighlightSimplePost (""); / / perform lookup QueryResponse response = testSolrClient.query (query); SolrDocumentList results = response.getResults (); / / get the total amount of data queried long numFound = results.getNumFound () If (numFound

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report