6.solr4.10.3api Use (curd)

Source: Internet
Author: User
Tags solr

reprint please from Source: http://www.cnblogs.com/hd3013779515/

1. Introduction of Solr-solrj-4.10.3.jar in the project

<Dependency>    <groupId>Org.apache.solr</groupId>    <Artifactid>Solr-solrj</Artifactid>    <version>4.10.3</version></Dependency>

2.SOLR additions and deletions to change the search

(1) schema.xml configuration Modifications

<Fieldname= "Stu_name"type= "Text_ik"indexed= "true"stored= "true"multivalued= "false" /> <Fieldname= "Stu_sex"type= "int"indexed= "true"stored= "true"multivalued= "false" /> <Fieldname= "Stu_address"type= "Text_ik"indexed= "true"stored= "true"multivalued= "false" />

(2) Student.java

 Packagecn.ljh.ssm.test;ImportOrg.apache.solr.client.solrj.beans.Field; Public classstudent{@Field ("id")    PrivateString ID; @Field ("Stu_name")    PrivateString name; @Field ("Stu_sex")    Private intsex; @Field ("Stu_address")    PrivateString address;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetsex () {returnsex; }     Public voidSetsex (intsex) {         This. Sex =sex; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; } @Override PublicString toString () {return"Student [id=" + ID + ", name=" + name + ", sex=" +Sex+ ", address=" + address + "]"; }}

(3) Httpsolrserversingleton.java

 Packagecn.ljh.ssm.test;ImportOrg.apache.solr.client.solrj.impl.HttpSolrServer; Public classHttpsolrserversingleton {//SOLR Server URL refers to the access path when SOLR publishes to the Web project     Private Final StaticString Solrurl = "HTTP://192.168.137.168:8080/SOLR"; //using static internal classes in lazy-type singleton cases     Private Static classhttpsolrserversingletoncontainer{Private StaticHttpsolrserver instance =NewHttpsolrserver (Httpsolrserversingleton.solrurl); }     //Solrserver is thread-safe, so you need to use a singleton mode to reduce resource consumption      Public Statichttpsolrserver getinstance () {returnhttpsolrserversingletoncontainer.instance; }}

(4) Solrhelloworldtest.java

 Packagecn.ljh.ssm.test;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Random;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.SolrDocument;Importorg.apache.solr.common.SolrDocumentList;Importorg.apache.solr.common.SolrInputDocument;Importorg.junit.Test; Public classsolrhelloworldtest {@Test Public voidTestadd () {Try{httpsolrserver server=httpsolrserversingleton.getinstance (); //Delete all data firstServer.deletebyquery ("*:*"); Solrinputdocument Doc=Newsolrinputdocument (); Doc.addfield ("id", string.valueof (1)); Doc.addfield ("Name", "Apple Phone"); Doc.addfield ("Price", "6000");            Server.add (DOC); Solrinputdocument doc2=Newsolrinputdocument (); Doc2.addfield ("id", string.valueof (2)); Doc2.addfield ("Name", "Huawei Phone"); Doc2.addfield ("Price", "1000");            Server.add (DOC2); Solrinputdocument DOC3=Newsolrinputdocument (); Doc3.addfield ("id", string.valueof (3)); Doc3.addfield ("Name", "Mi Phone"); Doc3.addfield ("Price", "2000");                        Server.add (DOC3); Solrinputdocument doc5=Newsolrinputdocument (); Doc5.addfield ("id", string.valueof (15)); Doc5.addfield ("Name", "Mi phone02"); Doc5.addfield ("Price", "2000");                                    Server.add (DOC5); Random Random=NewRandom ();  for(inti = 0; I < 10; i++) {solrinputdocument doc4=Newsolrinputdocument (); Doc4.addfield ("id", i+4); Doc4.addfield ("Name", "Phone" +i); Doc4.addfield ("Price", Random.nextint (2000));                            Server.add (DOC4); } server.commit ();//Commit, commit all updates to the index}Catch(solrserverexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }        /*** Add document using Pojo*/@Test Public voidtestaddstudent () {Try{httpsolrserver server=httpsolrserversingleton.getinstance (); List<Student> studentlist =NewArraylist<student>(); Student STU1=NewStudent (); Stu1.setid ("103"); Stu1.setname ("Zhang Qiang"); Stu1.setsex (1); Stu1.setaddress ("Zhi Chun Lu, Haidian District, Peking City");                        Studentlist.add (STU1); STU1=NewStudent (); Stu1.setid ("104"); Stu1.setname ("Liu Mi"); Stu1.setsex (0); Stu1.setaddress ("Bei Yuan Lu, Haidian District, Peking City");                        Studentlist.add (STU1);            Server.addbeans (studentlist);        Server.commit (); } Catch(solrserverexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }} @Test Public voidtestquerystudent () {Try{httpsolrserver server=httpsolrserversingleton.getinstance (); String strquery= "Stu_name: Millet";//Q Indicates the contents of the querySolrquery query =Newsolrquery (strquery); Queryresponse resp=server.query (query); Solrdocumentlist sdlist=resp.getresults (); LongTotalresults = Sdlist.getnumfound ();//total number of records hitSystem.out.println ("Totalresults-->" +totalresults);  for(solrdocument sd:sdlist) {Student Student= Server.getbinder (). Getbean (Student.class, SD);            SYSTEM.OUT.PRINTLN (student); }        } Catch(solrserverexception e) {e.printstacktrace (); }} @Test Public voidTestquery () {Try{httpsolrserver server=httpsolrserversingleton.getinstance (); String strquery= "Name:apple";//Q Indicates the contents of the querySolrquery query =NewSolrquery (); Query.set ("Q", strquery); Queryresponse resp=server.query (query); Solrdocumentlist sdlist=resp.getresults (); LongTotalresults = Sdlist.getnumfound ();//total number of records hitSystem.out.println ("Totalresults-->" +totalresults);  for(solrdocument sd:sdlist) {System.out.print ("ID:" + sd.getfieldvalue ("id") + "" + "Name:" + sd.getfieldvalue ("name") + "+" Price: "+ sd.getfieldvalue (" price "))); }        } Catch(solrserverexception e) {e.printstacktrace (); }} @Test Public voidTestdelete () {Try{httpsolrserver server=httpsolrserversingleton.getinstance (); Server.deletebyid ("1"); Server.deletebyquery ("Id:2 Id:3");         Server.commit (); } Catch(Exception e) {e.printstacktrace (); }    }}

6.solr4.10.3api Use (curd)

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.