Java calls the solrj5.5.3 interface to query the data

Source: Internet
Author: User

Pre-set up SOLR services

Refer to the previous article to build a SOLR search service.

Add dependency

Maven project, add the following dependencies,

<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.APACHE.SOLR/SOLR-SOLRJ -<Dependency>    <groupId>Org.apache.solr</groupId>    <Artifactid>Solr-solrj</Artifactid>    <version>5.5.3</version></Dependency>

You can also import the jar package yourself

In the SOLR installation directory, locate the Solr-5.5.3\dist\solrj-lib path, add all the jar packages inside to your project, and don't forget to have a Solr-solrj-5.5.3.jar package in the outside folder and copy it together.

Writing the calling code

Here's a simple example (with paging), the Spring MVC project:

@ResponseBody @RequestMapping (value= "/searchbykeyword", method ={requestmethod.post, requestmethod.get}, produces= "Application/json; Charset=utf-8 ")     Publicstring Search (HttpServletRequest request, httpservletresponse Response, string KeyWord,intPageSize,intPageoffset) {        Try{String urlstring= Configutil.getvaluebykey ("Solrserviceurl"); Solrclient SOLR=Newhttpsolrclient (urlstring); Solrquery Query=NewSolrquery (); Query.set ("Q", Urldecoder.decode (KeyWord, "UTF-8")); Query.set ("Start", string.valueof (Pageoffset *pageSize)); Query.set ("Rows", String.valueof (pageSize)); Queryresponse Queryresponse=solr.query (query); //solrdocumentlist list = Queryresponse.getresults ();Map<string, object> result =NewHashmap<string, object>(); List<OCSearchObject> rstlist = Queryresponse.getbeans (ocsearchobject.class); Result.put ("Success",true); Result.put ("Key", Urldecoder.decode (KeyWord, "UTF-8")); Result.put ("List", rstlist); Result.put ("TotalCount", Queryresponse.getresults (). Getnumfound ());            Solr.close (); returnjson (result); }        Catch(Exception e) {logger.error (e.tostring (), E); returnJsonpfalse (Request, Response, "Code_error"); }    }

Here is the Ocsearchobject class, notice that you want to correspond to the fields configured with Data-config.xml, and don't forget to precede the fields with @ Field annotations (Org.apache.solr.client.solrj.beans.Field).

 Public classocsearchobject{PrivateLong ID; PrivateString name; PrivateString Fulltext; PrivateString type; /**     * @returnThe ID*/     PublicLong getId () {returnID; }    /**     * @paramID The ID to set*/@Field ("id")     Public voidsetId (Long id) { This. ID =ID; }    /**     * @returnThe name*/     PublicString GetName () {returnname; }    /**     * @paramname the name to set*/@Field ("Name")     Public voidsetName (String name) { This. Name =name; }    /**     * @returnThe fulltext*/     PublicString Getfulltext () {returnFulltext; }    /**     * @paramFulltext the fulltext to set*/@Field ("Fulltext")     Public voidSetfulltext (String fulltext) { This. Fulltext =Fulltext; }    /**     * @returnThe type*/     PublicString GetType () {returntype; }    /**     * @paramType the type to set*/@Field ("Type")     Public voidsetType (String type) { This. Type =type; }}
Front-end examples

Other specific expansion according to business needs, in front of the query to the data can be some custom display (keyword Red highlight, each record category, click to jump to the record details page).

Java calls the solrj5.5.3 interface to query the data

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.