[SOLR]-solrj: add, delete, and query

Source: Internet
Author: User
Tags solr

Use solrj to add, delete, and query SOLR.

Reference:

Http://wiki.apache.org/solr/Solrj

 

Create a new project in Eclipse: testsolr

 

The LIB package of sorlj includes:

 

Code:

Create an item entity

package com.my.entity;import java.util.Date;import org.apache.solr.client.solrj.beans.Field;public class Item {    @Field    private long id;    @Field    private String subject;    @Field    private String content;    @Field    private Date last_update_time;        public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getSubject() {        return subject;    }    public void setSubject(String subject) {        this.subject = subject;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    public Date getLast_update_time() {        return last_update_time;    }    public void setLast_update_time(Date last_update_time) {        this.last_update_time = last_update_time;    }}

 

Test code:

Package COM. my. SOLR; import Java. io. ioexception; import Java. util. date; import Java. util. list; import Org. apache. SOLR. client. solrj. solrquery; import Org. apache. SOLR. client. solrj. solrquery. order; import Org. apache. SOLR. client. solrj. solrquery. sortclause; import Org. apache. SOLR. client. solrj. solrserverexception; import Org. apache. SOLR. client. solrj. impl. httpsolrserver; import Org. apache. SOLR. client. solrj. impl. XM Lresponseparser; import Org. apache. SOLR. client. solrj. response. queryresponse; import COM. my. entity. item; public class testsolr {public static void main (string [] ARGs) throws ioexception, solrserverexception {string url = "http: // localhost: 8899/SOLR/mycore "; httpsolrserver core = new httpsolrserver (URL); core. setmaxretries (1); core. setconnectiontimeout (5000); core. setparser (New xmlresponseparser ()); // Binary parser is used by default core. setsotimeout (1000); // socket read timeout core. setdefamaxmaxconnectionsperhost (100); core. setmaxtotalconnections (100); core. setfollowredirects (false); // defaults to false core. setallowcompression (true); // -------------------------------------------------------- // remove all data // extract core. deletebyquery ("*: *"); // -------------------------------------------------------- // Add item = new item (); item. setid (1); item. setsubject ("solrj test"); item. setcontent ("this is my solrj test, ha ha. "); item. setlast_update_time (new date (); core. addbean (item); // -------------------------------------------------------- // Add Unicode item //----- Using item item_cn = new item (); item_cn.setid (2); item_cn.setsubject ("My tests"); item_cn.setcontent ("This is a magical website! "); Item. setlast_update_time (new date (); core. addbean (item_cn); // commit core. commit (); // queries // search // ---------------------------------------------------------- solrquery query = new solrquery (); query. setquery ("*: *"); query. addsort (New sortclause ("ID", order. DESC); queryresponse response = core. query (query); List <item> items = response. getbeans (item. class); For (item I: Items) {system. out. println ("ID =" + I. GETID () + "\ tcontent =" + I. getcontent ());}}}

 

Running result:

[SOLR]-solrj: add, delete, and query

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.