Fourth step Eclipse Implementation SOLR Client

Source: Internet
Author: User
Tags solr

1. Create SOLR client:

First import the package, put the apache-solr-4.0.0\dist under the APACHE-SOLR

-dataimporthandler-4.0.0.jar and Apache-solr-dataimporthandler-extras-4.0.0.jar copied to

Webroot\web-inf\lib under

or pom.xml file add

<!---Search dependent--><dependency> <groupId>org.apache.solr</groupId> <ARTIFACTID>SOLR-SOLRJ </artifactId> <version>4.7.2</version></dependency>

2. Create a Solrjsearchedemo class

import java.net.malformedurlexception;import org.apache.solr.client.solrj.solrquery;import  Org.apache.solr.client.solrj.solrserver;import org.apache.solr.client.solrj.solrserverexception;import  org.apache.solr.client.solrj.impl.HttpSolrServer;import  Org.apache.solr.client.solrj.response.queryresponse;import org.apache.solr.common.solrdocument;import  org.apache.solr.common.SolrDocumentList;public class SolrJSearcheDemo { /**  &NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;SOLR Access Address      */    private  static final string solr_url = "http://localhost:8080/solr/";         //  get Solrserver objects by network address     public SolrServer  Getsolrserver ()  throws MalformedURLException {         Return new httpsolrserver (Solr_url);     }        //  Query Method     public void search (String  key)  throws malformedurlexception{        solrserver  solrserver = getsolrserver ();        //  Query Object         solrquery query = new solrquery (key);         query.setquery (Key);             try {             query.sethighlight (True)                  //Set Start                  .sethighlightsimplepre ("<span class= ' Highlight ' >")             &nbSp;     .sethighlightsimplepost ("</span>")  //set End                   .setstart (0)                  .setrows (10);//Set Number of lines               //set which areas are highlighted                 query.setparam ("Hl.fl",  "content");             queryresponse response=solrserver.query (query);             solrdocumentlist list= Response.getresults ();                         system.out.println ("Highlight:");         &nBsp;   for (solrdocument sd:list) {                 string id= (String)  sd.getfieldvalue ("id");                 if ( Response.gethighlighting (). Get (ID)!=null) {                     system.out.println (Response.gethighlighting (). Get (ID). Get ("content"));                 }            }                          system.out.println ("——————————————");                    &nbSp;    solrdocumentlist docs = response.getresults ();             system.out.println ("Number of documents:"  + docs.getnumfound ( ));             system.out.println ("Query time:"  +  response.getqtime ());              System.out.println ("—————————————— –");                         for  (solrdocument doc :  docs)  {                             //  get query returns results                  string id =  doc.getfieldvalue ("id"). ToString ();                 String title = doc.getfirstvalue ("title"). ToString ();                 string content = doc.getfirstvalue (" Content "). ToString ();                                     //  Print Query Results                     system.out.println ("No.:" +id);                 system.out.println ("title:" +title);                 system.out.println ("Content: " +content );                             system.out.println ("—————————————-");                                 }          } catch  ( Solrserverexception e)  {              e.printstacktrace ();        }    }     public static void main (String[] args)  throws  malformedurlexception {        //  Creating a Solrjsearchedemo Object            solrjsearchedemo sj = new  solrjsearchedemo ();            //  Query Conditions             string query = "Life";        //  Call Query method          sj.search (Query);     }}


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/58/E3/wKiom1S_dyyji-fEAAJ8gbJYhiA600.jpg "title=" Qq20150121175331.png "alt=" Wkiom1s_dyyji-feaaj8gbjyhia600.jpg "/>

This article from "Ertan to win" blog, declined reprint!

Fourth step Eclipse Implementation SOLR Client

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.