Using SOLR in Java

Source: Internet
Author: User

Importing SOLR jar packages in a 1.pom file

<Dependency>    <groupId>Org.slf4j</groupId>    <Artifactid>Slf4j-log4j12</Artifactid>    <version>1.7.7</version></Dependency>

2. Create a new entity class message

 PackageCom.example.demo.domain;ImportLombok. Data;ImportOrg.apache.solr.client.solrj.beans.Field;Importjava.io.Serializable; @Data Public classMessageImplementsserializable{Private Static Final LongSerialversionuid = 1L; @Field ("id")    PrivateInteger ID; @Field ("TitleName")    PrivateString TitleName; @Field ("Label")    PrivateString label;}

3. Modify the Managed-schema profile under SOLR core to increase the field you need note the type is the IK Chinese word breaker that corresponds to the field in your entity class

4. New Solrutil Tool Class

 PackageCom.example.demo.util;ImportCom.example.demo.domain.Message;ImportOrg.apache.solr.client.solrj.SolrQuery;Importorg.apache.solr.client.solrj.SolrServerException;Importorg.apache.solr.client.solrj.impl.HttpSolrClient;ImportOrg.apache.solr.client.solrj.response.QueryResponse;Importorg.apache.solr.common.SolrDocument;Importorg.apache.solr.common.SolrDocumentList;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List; Public classSolrutil {PrivateString Solrurl = "Http://localhost:8983/solr/newcore"; /*** Add Data *@parammessage *@throwsIOException *@throwssolrserverexception*/     Public voidAdd (Message message)throwsIOException, solrserverexception {httpsolrclient client=Newhttpsolrclient (Solrurl);        Client.addbean (message);    Client.commit (); }    /*** Search Data *@paramkeyword *@paramPage *@paramrows *@return     * @throwsIOException *@throwssolrserverexception*/     Publiclist<message> Search (String keyword, integer page, integer rows)throwsIOException, solrserverexception{httpsolrclient client=Newhttpsolrclient (Solrurl); Solrquery Query=NewSolrquery (); Query.set ("Q", "TitleName:" +keyword+ "*");//Search KeywordsQuery.setstart ((page-1) * rows);//Set Page PagingQuery.setrows (rows);//set Number of query barsqueryresponse Response= client.query (query);//Perform a searchSolrdocumentlist resultlist = Response.getresults ();//Search ResultsList<Message> list =NewArraylist<>();  for(solrdocument document:resultlist) {Message message=NewMessage (); Message.setid (Integer.parseint (Document.get ("id"). toString ())); Message.setlabel (Document.get ("Label"). toString ()); Message.settitlename (Document.get ("TitleName"). toString ());        List.add (message); }        returnlist; }    /*** Delete Data *@paramID *@throwsIOException *@throwssolrserverexception*/     Public voidDelete (String ID)throwsIOException, solrserverexception {httpsolrclient client=Newhttpsolrclient (Solrurl);    Client.deletebyid (ID); }}
View Code

5. Testing

Insert

 Packagecom.example.demo.test;ImportCom.example.demo.domain.Message;ImportCom.example.demo.util.SolrUtil;Importjava.util.List; Public classTest { Public Static voidMain (string[] args) {Try{solrutil util=NewSolrutil (); Message Message=NewMessage (); Message.setid (2); Message.settitlename ("Heading 2"); Message.setlabel ("Java language");        Util.add (message); }Catch(Exception e) {e.printstacktrace (); }    }}

Inquire

 Packagecom.example.demo.test;ImportCom.example.demo.domain.Message;ImportCom.example.demo.util.SolrUtil;Importjava.util.List; Public classTest { Public Static voidMain (string[] args) {Try{solrutil util=NewSolrutil (); List<Message> list = Util.search ("superscript", 1,5);  for(Message age:list) {System.out.println (Age.getlabel ()); }        }Catch(Exception e) {e.printstacktrace (); }    }}

can also be viewed in the SOLR console

Using SOLR in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.