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 springboot integrates solr

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how springboot integrates solr. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Download solr

Download address, select the version you want to download

Http://archive.apache.org/dist/lucene/solr/7.5.0/

Download and decompress

2. Start solr

There are many ways to start solr, which can be combined with tomcat, and the new version of solr can be started directly. Start here from the command line.

Open cmd and start the project

Write down two common commands here

Start bin\ solr.cmd start and stop bin\ solr.cmd stop-all

Note: if startup failure indicates that managed-schema is missing, the solution is as follows

Copy the conf folder under\ server\ resources to the newly created core\ server\ solr\ new_core

After starting successfully, open a browser to enter the solr console http://localhost:8983/solr/#/

Add core and fields

Core is equivalent to a data table, and we can manage core

Select core, create the field, enter the field name and the corresponding word splitter

IV. Demonstration of data operation

/ update: add or update data

JSON: adding data in json format

After the addition is successful, let's check it.

Q: query keyword, you can specify the query field name sort: sort start rows: paging query fl: specify the fields to be displayed for the returned results df: the default field specifies the fields to be queried. 5. Chinese word splitter link: https://pan.baidu.com/s/1i5DJbj0lBeaJpgd1BInMxg extraction code: prfe

After downloading, put it in the\ server\ solr-webapp\ webapp\ WEB-INF\ lib directory

Then edit the managed-schema.xml. Add the following, and finally restart solr

Now let's test the difference before and after the use of the Chinese word splitter.

You can see that the text_ik word splitter is more in line with the habit of Chinese character segmentation.

VI. Springboot integrates solr

Jar package dependency

Org.springframework.boot spring-boot-starter-data-solr

Application.properties configuration, which is the case where core is specified, or may not be specified, and then specified at the time of operation

Spring.data.solr.host= http://localhost:8983/solr/new_core

Test case

Package org.elvin.mysolr.controller;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.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List Import java.util.Map;import java.util.UUID;@RestController@RequestMapping ("solr") public class SolrController {@ Autowired private SolrClient client / * * add / modify index * when id exists, this method is modified (of course, the uuid I use here will not exist). If id does not exist, add * @ return * / @ RequestMapping ("add") public String add () {String uuid = UUID.randomUUID () .toString () .replaceAll ("-", ""). Try {SolrInputDocument doc = new SolrInputDocument (); doc.setField ("id", uuid); doc.setField ("studentName", "Xiao Wang") / * if core is configured in spring.data.solr.host, there is no need to pass the parameter new_core here * the following is the same * / client.add ("new_core", doc); / / client.commit (); client.commit ("new_core"); return uuid } catch (Exception e) {e.printStackTrace ();} return "error";} / * delete the index according to id * @ param id * @ return * / @ RequestMapping ("delete") public String delete (String id) {try {client.deleteById ("new_core", id) Client.commit ("new_core"); return id;} catch (Exception e) {e.printStackTrace ();} return "error" } / * delete all indexes * @ return * / @ RequestMapping ("deleteAll") public String deleteAll () {try {client.deleteByQuery ("new_core", "*: *"); client.commit ("new_core"); return "success";} catch (Exception e) {e.printStackTrace () } return "error";} / * query index according to id * @ return * @ throws Exception * / @ RequestMapping ("getById") public String getById () throws Exception {SolrDocument document = client.getById ("new_core", "536563"); System.out.println (document); return document.toString () } / * Comprehensive query: in the comprehensive query, there are conditional query, conditional filtering, sorting, paging, highlighting, obtaining partial domain information * @ return * / @ RequestMapping ("search") public Map search () {try {SolrQuery params = new SolrQuery () / / query condition. The Q here corresponds to the params.set ("Q", "mobile phone") marked in the following image; / / filter condition params.set ("fq", "product_price: [100TO 100000]"); / / sort params.addSort ("product_price", SolrQuery.ORDER.asc) / / paging params.setStart (0); params.setRows (20); / / default domain params.set ("df", "product_title"); / / query only specified domain params.set ("fl", "id,product_title,product_price") / / highlight / / turn on the switch params.setHighlight (true); / / specify the highlight field params.addHighlightField ("product_title"); / / set the prefix params.setHighlightSimplePre (""); / / set the suffix params.setHighlightSimplePost (") QueryResponse queryResponse = client.query (params); SolrDocumentList results = queryResponse.getResults (); long numFound = results.getNumFound (); System.out.println (numFound); / / get the highlighted result, which is separated from the query result by Map highlight = queryResponse.getHighlighting () For (SolrDocument result: results) {System.out.println (result.get ("id")); System.out.println (result.get ("product_title")); / / System.out.println (result.get ("product_num")); System.out.println (result.get ("product_price")) / / System.out.println (result.get ("product_image")); Map map = highlight.get (result.get ("id")); List list = map.get ("product_title"); System.out.println (list.get (0)) System.out.println ("-"); System.out.println ();} return highlight;} catch (Exception e) {e.printStackTrace ();} return null }} the above is how springboot integrates solr. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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