Objective:
The SOLR server is set up, and after the data is imported, it is applied to the project. So how do you integrate and apply SOLR in the project?
Next, you'll integrate and apply SOLR
I. Integration
1. Introduction of the JAR package
<Properties> <spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version> </Properties> <dependencymanagement> <Dependencies> <Dependency> <groupId>Org.springframework.data</groupId> <Artifactid>Spring-data-solr</Artifactid> <version>${spring.data.solr.version}</version> </Dependency> </Dependencies> </dependencymanagement> <Dependencies> <Dependency> <groupId>Org.springframework.data</groupId> <Artifactid>Spring-data-solr</Artifactid> </Dependency> <!--default starter will load SOLRJ come in, the following is not cited - <Dependency> <groupId>Org.apache.solr</groupId> <Artifactid>Solr-solrj</Artifactid> <version>6.6.2</version> </Dependency> </Dependencies>
2. Configuration files
Spring: data: SOLR: HOST:HTTP://127.0.0.1:8081/SOLR
Host can also be written as Http://127.0.0.1:8081/solr/collection1.
The collection1 here is the name of the core and can be configured in the core admin option.
Collection1 can be understood here as a database concept. When operating, you can switch the database if you have more than one core. That is, switch core
Two. Indexing and querying
PackageOrg.elvin.mysolr.controller;Importorg.apache.solr.client.solrj.SolrClient;ImportOrg.apache.solr.client.solrj.SolrQuery;ImportOrg.apache.solr.client.solrj.response.QueryResponse;Importorg.apache.solr.common.SolrDocument;Importorg.apache.solr.common.SolrDocumentList;Importorg.apache.solr.common.SolrInputDocument;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;Importjava.util.List;ImportJava.util.Map;ImportJava.util.UUID;@RestController @requestmapping ("SOLR") Public classSolrcontroller {@AutowiredPrivatesolrclient Client; /*** New/Modified Index
* When the ID exists, this method is modified (of course, I use the UUID here, will not exist), if the ID does not exist, it is new *@return */@RequestMapping ("Add") Publicstring Add () {string UUID= Uuid.randomuuid (). toString (). ReplaceAll ("-", "" "); Try{solrinputdocument doc=Newsolrinputdocument (); Doc.setfield ("id", UUID); Doc.setfield ("Content_ik", "I am Chinese, I love China"); /* if the spring.data.solr.host inside the core, then there is no need to pass Collection1 this parameter * the following are the same*/Client.add ("Collection1", Doc); //client.commit ();Client.commit ("Collection1"); returnuuid; } Catch(Exception e) {e.printstacktrace (); } return"Error"; } /*** Delete Index by ID *@paramID *@return */@RequestMapping ("Delete") Publicstring Delete (string id) {Try{Client.deletebyid ("Collection1", id); Client.commit ("Collection1"); returnId; } Catch(Exception e) {e.printstacktrace (); } return"Error"; } /*** Delete all indexes *@return */@RequestMapping ("DeleteAll") PublicString DeleteAll () {Try{client.deletebyquery ("Collection1", "*:*"); Client.commit ("Collection1"); return"Success"; } Catch(Exception e) {e.printstacktrace (); } return"Error"; } /*** Query index by ID *@return * @throwsException*/@RequestMapping ("GetById") PublicString GetById ()throwsException {solrdocument document= Client.getbyid ("Collection1", "536563"); System.out.println (document); returndocument.tostring (); } /*** Comprehensive query: In the comprehensive query, there are conditional query, conditional filtering, sorting, paging, highlighting, get some domain information *@return */@RequestMapping ("Search") PublicMap<string, Map<string, list<string>>>search () {Try{solrquery params=NewSolrquery (); //query conditions, where Q corresponds to the image below the red placeParams.set ("Q", "Phone"); //Filter ConditionsParams.set ("Fq", "product_price:[100 to 100000]"); //SortParams.addsort ("Product_price", SOLRQUERY.ORDER.ASC); //page OutParams.setstart (0); Params.setrows (20); //Default DomainParams.set ("DF", "Product_title"); //querying only the specified domainParams.set ("FL", "Id,product_title,product_price"); //Highlight//turn on the switchParams.sethighlight (true); //Specify highlighted fieldsParams.addhighlightfield ("Product_title"); //Set prefixParams.sethighlightsimplepre ("<span style= ' color:red ' >"); //set suffixParams.sethighlightsimplepost ("</span>"); Queryresponse Queryresponse=client.query (params); Solrdocumentlist Results=queryresponse.getresults (); LongNumfound =Results.getnumfound (); System.out.println (Numfound);
//Get highlighted results, highlighted results and query results are split open Map<string, map<string, list<string>>> 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<string, list<string>> map = Highlight.get (result.get ("id"))); List<String> list = Map.get ("Product_title"); System.out.println (List.get (0)); System.out.println ("------------------"); System.out.println (); } returnhighlight; } Catch(Exception e) {e.printstacktrace (); } return NULL; }}
Three. Supplement
In particular, there is a need to add that, on the page to perform the modification, are very convenient, feeling is to do the blanks.
But when the deletion, it seems that can not directly fill in the blanks. It's special.
This needs to be done in XML, and must be committed, the following 1000ms autocommit, not for delete
<!--Delete all indexes -
<Delete> <Query>*:*</Query></Delete><Commit/>
<!--deleted by ID--
<Delete>
<ID>1</ID>
</Delete>
<commit />
SOLR (iv): Springboot consolidated SOLR