Apache SOLR using the SOLRJ Operation Index Library

Source: Internet
Author: User
Tags apache solr solr fluentd

SOLRJ is a relatively basic client tool for the SOLR Search server, which makes it very easy to interact with the SOLR search server. The most basic function is to manage SOLR indexes, including additions, updates, deletions, and queries. For some of the more basic applications, using SOLJ is basic enough, and you can easily interact with the SOLR search server by using the SOLRJ API to achieve basic management functionality for SOLR. If your application is more complex, you can extend the SOLRJ to meet your needs.

To manipulate the index library using SOLRJ:

Package Com.hcm.solr.test;import Java.io.ioexception;import Java.util.arraylist;import java.util.Iterator;import Java.util.list;import Java.util.map;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.apache.solr.client.solrj.solrquery;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.client.solrj.response.UpdateResponse ; Import Org.apache.solr.common.solrdocument;import Org.apache.solr.common.solrdocumentlist;import Org.apache.solr.common.solrinputdocument;import Org.apache.solr.common.params.modifiablesolrparams;import Org.junit.before;import Org.junit.test;import Com.hcm.solr.entity.message;public class SolrTest {private static Log Logger = Logfactory.getlog (solrtest.class);p rivate static final String URL = "HTTP://127.0.0.1:8086/SOLR";p rivate Httpsolrserver server = null; @Beforepublic void init () {// Create Serverserver = new Httpsolrserver (URL);} /** * Add document */@Testpublic void Adddoc () {solrinputdocument doc = new solrinputdocument ();d Oc.addfield ("id", "one");d OC.ADDF Ield ("title", "This is my document!!"); try {updateresponse response = Server.add (doc);//Submit server.commit (); Logger.info ("########## Query time:" + Response.getqtime ()); Logger.info ("########## Elapsed Time:" + response.getelapsedtime ()); Logger.info ("########## Status: "+ response.getstatus ());} catch (Solrserverexception | IOException e) {logger.error ("", E);}} /** * Add multiple documents */@Testpublic void Adddocs () {string[] titles = new string[] {"Aaaaaaa", "bbbbbbb", "CCCCCCC", "dddddd", "ee Eeee "}; list<solrinputdocument> docs = new arraylist<solrinputdocument> (); int i = 0;for (String str:titles) {SOLRINP Utdocument doc = new solrinputdocument ();d Oc.addfield ("id", i++);d Oc.addfield ("title", str);d Ocs.add (DOC);} try {updateresponse response = Server.add (docs); Server.commit (); Logger.info ("########## Query Time:" + responsE.getqtime ()); Logger.info ("########## Elapsed Time:" + response.getelapsedtime ()); Logger.info ("########## Status:" + Response.getstatus ());} catch (Solrserverexception | IOException e) {logger.error ("", E);}} /** * Add an entity to the index library */@Testpublic void Addbean () {Message msg = new Message ("1001", "What is Fluentd?", new string[] {"F Luentd is a open source data collector for Unified logging layer "," FLUENTD allows your to unify data collection and Consum Ption for a better use and understanding of data. "," Fluentd decouples data sources from backend systems by providing a uni fied logging layer in between. "," Fluentd proves your can achieve programmer happiness and performance at the same time. A Great example of Ruby beyond the Web. "," fluentd to differentiate their products with better use of data. "}); try {updateresponse response = Server.addbean (msg); Server.commit (); Logger.info ("########## Query time:" + Response.getqtime ()); Logger.info ("########## Elapsed Time:" + response.getelapsedtime (); Logger.info ("########## Status:" + response.getstatus ());} catch (Solrserverexception | IOException e) {logger.error ("", E);}} /** * Add multiple entity to index library */@Testpublic void Addbeans () {list<message> msgs = new arraylist<message> ();  Message msg = new Message ("1001", "What are Fluentd?", new string[] {"Fluentd is a open source data collector for Unified Logging layer "," FLUENTD allows you to unify data collection and consumption for a better use and understanding of data. " , "Fluentd decouples data sources from backend systems by providing a unified logging layer in between.", "Fluentd proves yo U can achieve programmer happiness and performance at the same time. A Great example of Ruby beyond the Web. "," fluentd to differentiate their products with better use of data. "}); Message MSG2 = new Message ("1002", "What are Fluentd?", new string[] {"Fluentd is a open source data collector for Unifie D Logging Layer "," FLUENTD allows you to unify data collection and consumption for a better USE and understanding of data. "," Fluentd decouples data sources from backend systems by providing a unified logging layer in Between. "," Fluentd proves you can achieve programmer happiness and performance at the same time. A Great example of Ruby beyond the Web. "," fluentd to differentiate their products with better use of data. "}); Msgs.add (msg); Msgs.add (MSG2); try {updateresponse response = Server.addbeans (msgs); Server.commit (); Logger.info ("# # # ####### Query Time: "+ response.getqtime ()); Logger.info (" ########## Elapsed Time: "+ response.getelapsedtime ()); logger . info ("########## Status:" + response.getstatus ());} catch (Solrserverexception | IOException e) {logger.error ("", E);}} /** * Delete index */@Testpublic void Deletedoc () {try {Server.deletebyid ("0"); Server.commit ();} catch (Solrserverexception | IOException e) {logger.error ("", E);}} /** * Update Index <br> * SOLR Index Library is different from the database and has no updated functionality. If you want to update, first delete the corresponding document by ID, and then add a new document. */@Testpublic void Updatedoc () {//...} /** * Query */@Testpublic void Testquery () {STring querystr = "*:*"; Solrquery params = new Solrquery (querystr);p arams.set ("Rows", ten); try {queryresponse response = Server.query (params); Solrdocumentlist list = Response.getresults () logger.info ("########### total:" + list.getnumfound () + "record"); for (SOLRDOCU ment doc:list) {logger.info ("######### ID:" + doc.get ("id") + "title:" + doc.get ("title"));}} catch (Solrserverexception e) {logger.error ("", E);}} /** * Simple Query (paging) */@Testpublic void Querysimple () {modifiablesolrparams params = new Modifiablesolrparams ();p arams.set ("q" , "This my");p arams.set ("Q.op", "and");p Arams.set ("Start", 0);p arams.set ("Rows", 5);p arams.set ("FL", "*,score"); try { Queryresponse response = Server.query (params); Solrdocumentlist list = Response.getresults () logger.info ("########### total:" + list.getnumfound () + "record"); for (SOLRDOCU ment doc:list) {logger.info ("######### ID:" + doc.get ("id") + "title:" + doc.get ("title"));}} catch (Solrserverexception e) {logger.error ("", E);}} /** * Query (paging, highlighting) */@TestpuBlic void Querycase () {String querystr = "Title:this"; Solrquery params = new Solrquery (querystr);p arams.set ("Start", 0);p arams.set ("Rows", 5);//enable highlight components, Set the Highlight Params.sethighlight (true). Addhighlightfield ("title"). Sethighlightsimplepre ("<span class=\" red\ ">"). Sethighlightsimplepost ("</span>"). Sethighlightsnippets (2). Sethighlightfragsize (+). Setstart (0). setrows (Ten). Set ("Hl.usefastvectorhighlighter", "true"). Set ("Hl.fragsize", "Max"); try {queryresponse response = Server.query (params); Solrdocumentlist list = Response.getresults () logger.info ("########### total:" + list.getnumfound () + "record"); for (SOLRDOCU ment doc:list) {logger.info ("######### ID:" + doc.get ("id") + "title:" + doc.get ("title")); Map<string, map<string, list<string>>> Map = response.gethighlighting ();Iterator<String> iterator = Map.keyset (). iterator (); while (Iterator.hasnext ()) {String key = Iterator.next (); map<string, list<string>> values = Map.get (key); Logger.info ("# ########################################################### "); Logger.info (" ############ ID: "+ key); for (Map.Entry <string, list<string>> Entry:values.entrySet ()) {String subkey = Entry.getkey (); list<string> subvalues = Entry.getvalue () logger.info ("############ subkey:" + subkey); for (String str:subvalues {Logger.info ("############ subvalues:" + str);}}} catch (Solrserverexception e) {logger.error ("", E);}}}

Pojo:

Package Com.hcm.solr.entity;import Java.io.serializable;import Org.apache.solr.client.solrj.beans.field;public Class Message implements Serializable {/** *  */private static final long serialversionuid = 1l;private String Id;priva Te String title;private string[] content;public Message () {super ();} Public Message (string ID, string title, string[] content) {super (); this.id = Id;this.title = title;this.content = content; }public String getId () {return ID;} @Field ("id") public void SetId (String id) {this.id = ID;} Public String GetTitle () {return title;} @Field ("title") public void Settitle (String title) {this.title = title;} Public string[] GetContent () {return content;} @Field ("content") public void SetContent (string[] content) {this.content = content;}}


Finish



Apache SOLR using the SOLRJ Operation Index Library

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.