Querying data using SOLRJ (Java code)

Source: Internet
Author: User
Tags solr

Entity class student, adding field annotations

 Packagecom.cs.solr.entity;ImportOrg.apache.solr.client.solrj.beans.Field; Public classStudent {@Field ("id")    PrivateInteger ID; @Field ("Name")    PrivateString name; @Field ("Gender")    PrivateString Gender; @Field ("Major")    PrivateString Major; @Field ("Grade")    PrivateString grade;  PublicStudent () {Super(); }     PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getgender () {returngender; }     Public voidSetgender (String gender) { This. Gender =gender; }     PublicString getmajor () {returnMajor; }     Public voidSetmajor (String major) { This. Major =Major; }     PublicString Getgrade () {returngrade; }     Public voidSetgrade (String grade) { This. grade =grade; }}

The Java code of the query.

 PackageCom.cs.teset;Importjava.util.List;ImportOrg.apache.solr.client.solrj.SolrQuery;Importorg.apache.solr.client.solrj.SolrServerException;ImportOrg.apache.solr.client.solrj.impl.HttpSolrServer;ImportOrg.apache.solr.client.solrj.response.QueryResponse;Importorg.apache.solr.common.SolrDocumentList;Importcom.cs.solr.entity.Student; Public classSolrquerytest {Private Static intConnectionTimeout = 60000; Private Static intSoTimeout = 60000; Private Static intMaxretries = 1; Private Static intDefaultmaxconnectionperhost = 100; Private Static intmaxtotalconnections = 100; Private Static BooleanFollowredirects =false; Private Static BooleanAllowcompression =true;  Public Static voidMain (string[] args)throwsclassnotfoundexception {queryall (); }     Public Static voidQueryall ()throwsClassNotFoundException {Try{httpsolrserver server=NewHttpsolrserver ("Http://localhost:8983/solr/mycore");            Server.setsotimeout (SoTimeout);            Server.setconnectiontimeout (ConnectionTimeout);            Server.setdefaultmaxconnectionsperhost (Defaultmaxconnectionperhost);            Server.setmaxtotalconnections (maxtotalconnections);            Server.setfollowredirects (followredirects);            Server.setallowcompression (allowcompression);            Server.setmaxretries (maxretries); Solrquery params=NewSolrquery ("*:*");//Query AllQueryresponse QS =server.query (params); Solrdocumentlist Results=qs.getresults (); List<Student> list =qs.getbeans (Student.class);  for(Student student:list) {System.out.println (Student.getname ()); }            //System.out.println (list);}Catch(solrserverexception e) {e.printstacktrace (); }    }}

Querying data using SOLRJ (Java code)

Related Article

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.