Java Operations SOLR builds high-performance search engine __java

Source: Internet
Author: User
Tags apache solr apache solr download solr solr query

At present, more suitable for Java search engine construction will generally choose SOLR, the underlying operation will use SOLRJ interaction, in fact, SOLR is based on Lucene. The implementation process found that many of the Web documents on Java Integration SOLR are based on the solr5+ version, and for the solr7+ version of the document is very small, and many are pits, so spent a lot of time to share their own experience in the deployment process.

First go to the official website of Apache SOLR download SOLR 7.1, and then upload to the server, under the bin directory has an execution file, through the command/BIN/SOLR Start-force can easily start Solr7.

Then go on to start SOLR's related configuration:

1. Create a new project in SOLR and use the SOLR create-c ' library name ' in the bin directory

2. Edit Managed-schema configuration file, add IK word breaker, the word breaker for Chinese support better

   <fieldtype name= "Text_ik" class= "SOLR." TextField ">
    <analyzer type=" index "class=" Org.wltea.analyzer.lucene.IKAnalyzer "/>
    <analyzer Type= "Query" class= "Org.wltea.analyzer.lucene.IKAnalyzer"/>
   </fieldType>
3. Configure Managed-schema to add the field to the settings,

After using this configuration, the property fields in your program code model can be added to the index as XXX_IKBOOKSTR, and you can use this field when querying.
4. Solrcore copy created in the first step below the SOLR root directory, you can visit your SOLR Web site to see your solrcore

5. At this point, the SOLR server has been built, followed by coding in the program, first in the Pom.xml to introduce the SOLR package support

6. Then model of the property field set method above add @field annotation, this annotation is used to tell solr this field needs to be added to the index, but it is noteworthy that this field name must conform to the above set of specifications

7. The next is springboot for SOLR package operation, I did once again the package convenient and practical, post my Code

public class Solrrepository {@Autowired private solrclient client;
	Private Queryresponse response;

	Private solrdocumentlist results; /** * TODO-data query for SOLR @param query keyword @param offset start @param limit the number of bars per query @param c Real Body Class object @param bool Closes the SOLR link @param QF search keyword matches some of the fields higher than the other fields @param pf for some fields, the percentage of the search string density (phrase) @p Aram FL Specifies the fields that need to be returned comma-delimited @param fq Specify filter conditions @return @throws Exception October 3, 2017 MAZKC/public List g Etsolrdata (String query,string offset,string limit,string c,boolean bool,string qf,string pf,String solrFl,String FQ)
		Throws Exception {modifiablesolrparams params = new Modifiablesolrparams ();
		List beans = null; If the limit is empty or exceeds the restricted crawl quantity, return NULL if (null = = Limit | | "". Equals (limit) | |
		Integer.parseint (limit) > Finalargs.serach_limit) {return null;
		} params.set ("Q", query);
		Params.set ("WT", "JSON");
		Params.set ("Start", "0");
		Params.set ("Rows", "10"); params = ChecKnotnull (PARAMS,OFFSET,LIMIT,QF,PF,SOLRFL,FQ);
		Response = Client.query (params); if (null = c | |
		"". Equals (c)) {beans = Response.getresults ();
			}else{beans = Response.getbeans (Class.forName (c));
				if (null = = Beans | | beans.size () = = 0) {beans = new arraylist<> ();
			Beans.add (Class.forName (c). newinstance ());
		} COMMITANDCLOSESOLR (Client,bool);
	return beans; /** * TODO-Verify that the SOLR query has an optimized @param params parameter keyword @param the offset start position @param the number of Limit request data bars @pa 
	Ram QF matching weight @param pf appears word weight @param solrfl The field to be returned @param FQ filter which data @return October 17, 2017 MAZKC * * Private Modifiablesolrparams checknotnull (modifiablesolrparams params,string offset,string limit,String qf,String pf, String solrfl,string FQ {if (null!= offset &&! "".
		Equals (offset)) {Params.set ("Start", offset); } if (null!= limit &&! "".
			Equals (limit)) {if (Integer.parseint (limit) >=) {limit = "200"; } params.set("Rows", limit); } if (null!= QF &&! "".
		Equals (QF)) {Params.set ("QF", QF); } if (null!= PF &&! "".
		Equals (PF)) {Params.set ("pf", pf); } if (null!= SOLRFL &&! "".
		Equals (SOLRFL)) {params.set ("fl", SOLRFL); } if (null!= FQ &&! "".
		Equals (FQ)) {Params.set ("Fq", FQ);
		} params.set ("Deftype", "Edismax");
	return params;  /** * TODO-indexes data on SOLR for aggregate data @param map @throws Exception October 2, 2017 MAZKC/public void Savesolrdatalist (list<object> li,boolean bool) throws Exception {if (null!= li && li.size () > 0)
			{Client.addbeans (LI);
		COMMITANDCLOSESOLR (Client,bool);  }/** * * TODO-query SOLR data by index ID @param map @throws Exception October 2, 2017 MAZKC/public
		Solrdocument Getsolrdatabyid (String id,boolean bool) throws Exception {solrdocument so = Client.getbyid (ID);
		COMMITANDCLOSESOLR (Client,bool);
	return so; }/** * * TODO- Data index to SOLR for a single data @param map @throws Exception October 2, 2017 MAZKC/public void Savesolrdata (Object bea
			N,boolean bool) throws Exception {if (null!= bean) {Client.addbean (bean);
		COMMITANDCLOSESOLR (Client,bool); }/** * * TODO-Deletes all indexed documents by collection @param map @throws Exception October 2, 2017 MAZKC/Public V
			OID delsolrdatalist (list<string> li,boolean bool) throws Exception {if (null!= li && li.size () > 0) {
			Client.deletebyid (LI);
		COMMITANDCLOSESOLR (Client,bool); }/** * * TODO-Bulk Delete all indexed documents @param map @throws Exception October 2, 2017 MAZKC * * Public voi
		D delsolrdataall (String query,boolean bool) throws Exception {client.deletebyquery (query);
	COMMITANDCLOSESOLR (Client,bool);  /** * * TODO-Deletes all indexed documents by ID @param map @throws Exception October 2, 2017 MAZKC/public void Delsolrdataid (String id,boolean bool) throws Exception {Client.deletebyid (iD);
	COMMITANDCLOSESOLR (Client,bool);  /** * * TODO-update index data based on index ID @param map @throws Exception October 2, 2017 MAZKC/public void  Updatesolrdata (String queryid,hashmap<string,object> map,boolean bool) throws Exception {Solrinputdocument doc = 
		New Solrinputdocument ();
		Solrdocument sd = Getsolrdatabyid (Queryid, false);
		Iterator<string> it = Sd.keyset (). iterator ();
		String key = "";
			Gets all source data information while (It.hasnext ()) {key = It.next ();
		Doc.addfield (Key, Sd.getfieldvalue (key));
		Iterator<string> ITT = Map.keyset (). iterator ();
			Add the data that needs to be updated into the source data while (Itt.hasnext ()) {key = Itt.next ();
			Doc.remove (key);
		Doc.addfield (Key, Map.get (key));
		Client.add (DOC);
	COMMITANDCLOSESOLR (Client,bool); Private Solrinputdocument Checkdoc (hashmap<string,object> map,solrinputdocument doc) {if (Null!= map &&am P
			Null!= doc) {String key = "";
			Iterator<string> it = Map.keyset (). iterator (); WhilE (It.hasnext ()) {key = It.next ();
			Doc.addfield (Key, Map.get (key));
	} return doc; /** * Submit and Close service * * @param solrclient * @throws Exception/public void commitandcloses    		OLR (solrclient client,boolean bool) throws Exception {if (null!= client) {client.commit ();//
 if (bool) {//Client.close ();//}}}}


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.