Use of the "SOLR" Java integration solr5.0 SOLRJ

Source: Internet
Author: User
Tags solr

1, first import the SOLRJ need of the rack package

2, it should be noted that the low version is SOLR using solrserver for URL instances, 5.0 has replaced this class with solrclient , After adding the first we need to configure our word breaker according to Schema.xml

The msg_all here also needs to be configured in the Schema.xml

Its main function is to copy the value of the Msg_title,msg_content two domain to the Msg_all domain, we can search only this msg_all domain,

SOLR default search requires a domain, such as

SOLR changes the default search domain is also in Schema.xml, It is the default search text field, but after 5.0 does not configure the default search domain here, its documentation also tells us, in the Solrconfig.xml configuration

Configure the default search field in Solrconfig.xml so that we can search by our own domain

With the above configuration, you can use the code for curd.

Private Final  Static String url= "Http://localhost:8080/solr/java";      Public Solrclient  server=null;        @Before    publicvoidthrows  exception{        Server=New  httpsolrclient (URL);    }

Delete all participle

// Delete all participle     @Test    publicvoidthrows  exception{        server.deletebyquery ( "*:*");        Server.commit (); // Deleting a query-based delete deletes all established index files    }

Add participle

@Test      Public void throws exception{        solrinputdocument doc=new  solrinputdocument ();        Doc.addfield ("id", "3");        Doc.addfield ("Msg_title", "Sina Weibo");        Doc.addfield ("msg_content", "I have a Weibo account name called What?" ");        Server.add (DOC);        Server.commit ();    }

Add word segmentation based on bean

@Test      Public void throws exception{        List<Message> msgs=new arraylist<message>();        Msgs.add (newnew string[]{"Long live the People's Republic of China", "5,000 years of the Year"));        Msgs.add (newnew string[]{"Shanghai is a good place", "singing our beloved motherland once traversed the mountains and Rivers"});        Server.addbeans (msgs);        Server.commit ();    }

Query results

@Test Public voidtest04 ()throwsexception{//Define Query Content * Represents query all this is based on the result setSolrquery query =NewSolrquery ("SOLR"); Query.setstart (0);//Start PageQuery.setrows (3);//Display quantity per pageQueryresponse RSP =server.query (query); Solrdocumentlist Results=rsp.getresults (); System.out.println (Results.getnumfound ());//Query total number of bars          for(solrdocument doc:results) {System.out.println (doc); }    }

Encapsulate a query result set as an object bean

@Test      Public void throws exception{         new solrquery ("Zhonghua");   * Number is to query all data         queryresponse RSP = server.query (query);         List<Message> beans = Rsp.getbeans (Message.  Class); // This cannot get the total number of queries or highlight          for (Message message:beans) {             System.out.println (message.tostring ());         }    }

Highlight a result set

@Test Public voidTest06 ()throwsexception{//Define Query Content * Represents query all this is based on the result setSolrquery query =NewSolrquery ("SOLR"); Query.setstart (0);//Start PageQuery.setrows (5);//Display quantity per pageQuery.setparam ("Hl.fl", "msg_title,msg_content");//set which field fields are highlightedQuery.sethighlight (true). Sethighlightsimplepre ("<span class= ' hight ' >"). Sethighlightsimplepost ("</span>"); Queryresponse RSP=server.query (query); Solrdocumentlist Results=rsp.getresults (); System.out.println (Results.getnumfound ());//Query total number of bars          for(solrdocument doc:results) {String ID= (String) doc.getfieldvalue ("id");//ID is the UniqueKey field             if(Rsp.gethighlighting (). Get (ID)! =NULL){                 //highlight must require storage not stored words can not add highlightingSystem.out.println (Rsp.gethighlighting (). Get (ID). Get ("Msg_title")); }         }    }

The basic use of OK,SOLR is complete.

Use of the "SOLR" Java integration solr5.0 SOLRJ

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.