1, need in/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf of Solrconfig.xml Plus
<requesthandler name= "/select" class= "SOLR. Searchhandler ">
<!--default values for query parameters can specified, these
'll be overridden by parameters in the request
-
<lst name= "Defaults" >
<str name= "Echoparams" >explicit</str>
<int name= "Rows" >10</int>
<str name= "DF" >text</str>
</lst>
<!--This line of code is very important, and without this line, the spell checker is not working--
<arr name= "Last-components" >
<str>spellcheck</str>
So that spell can take effect.
2, in the/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf schema.xml modification, plus the field, or will be error
Such as:
<field name= "id" type= "string" indexed= "true" stored= "true" required= "true" multivalued= "false"/>
<field name= "SKU" type= "Text_en_splitting_tight" indexed= "true" stored= "true" omitnorms= "true"/>
<field name= "name" type= "Text_general" indexed= "true" stored= "true"/>
<field name= "addr" type= "Text_general" indexed= "true" stored= "true"/>
<field name= "age" type= "int" indexed= "true" stored= "true"/>
<field name= "Manu" type= "Text_general" indexed= "true" stored= "true" omitnorms= "true"/>
=======================================================
The following is a concrete implementation of the method
Com.ys.controller;com.ys.bean.person;org.apache.solr.client.solrj.solrserver;o Rg.apache.solr.client.solrj.impl.httpsolrserver;org.apache.solr.common.solrinputdocument;o Rg.springframework.beans.factory.annotation.;o Rg.springframework.web.bind.annotation.;o Rg.springframework.web.bind.annotation.requestmethod;org.springframework.web.bind.annotation.; Java.util.*;java.util.map.entry;org.apache.solr.client.solrj.solrquery;o Rg.apache.solr.client.solrj.impl.httpsolrclient;org.apache.solr.client.solrj.response.queryresponse;o Rg.apache.solr.client.solrj.response.spellcheckresponse;o Rg.apache.solr.client.solrj.response.spellcheckresponse.collation;o Rg.apache.solr.client.solrj.response.spellcheckresponse.correction;org.apache.solr.common.solrdocument;o rg.apache.solr.common.solrdocumentlist;org.apache.solr.common.solrinputdocument; (value = ) SolrTest { () String ; () string ; (Value = , method = requestmethod.) add () Exception { Map< String, string> map = hashmap<string, string> (); map.put (, ); map.put (, ); map.put (, ); map.put (, ); adddocument (map, ); } (Value = , method = requestmethod.) addbean () exception { list <Person> persons = ArrayList<Person> (); persons.add (Person (, , , )); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; persons.add (Person (, , , )); Adddocumentbybean (persons, ); } (value = , Method = requestmethod.) del () Exception { List< String> ids = arraylist<string> (); Ids.add (); ids.add (); ids.add (); deletedocumentbyids (ids, ); } (Value = , method = requestmethod.) search () Exception { GetDocument (); } (value = , method = RequestmethoD.) spell () exception { getspell ( ); } adddocument (map<string, string> map, String core) exception { solrinputdocument sid = solrinputdocument (); (entry<string, string> entry : Map.entryset ()) { sid.addfield ( Entry.getkey (), entry.getvalue ()); } httpsolrclient solrclient = getsolrclient (+ core); solrclient.add (SID); (solrclient); } adddocumentbybean (List<person> persons, string core) Exception { httpsolrclient solrclient = getsolrclient (+ core); solrclient.addbeans (persons); ( solrclient); } deletedocumentbyids (List<String> ids, string core) Exception { httpsolrclient solrclient = getsolrclient (+ core); solrclient.deletebyid (IDs); (solrclient); } getdocument (String core) exception { httpsolrclient solrclient = Getsolrclient (+ core); solrquery sq = Solrquery (); sq.set (, ); sq.addfilterquery (); sq.setsort (, Solrquery.order.); sq.setstart (); Sq.setrows (); sq.sethighlight (); sq.addhighlightfield (); Sq.sethighlightsimplepre (); sq.sethighlightsimplepost (); queryresponse result = solrclient.query (SQ); sYstem. println (); solrdocumentlist results = Result.getresults (); system. println (+ results.getnumfound () + ); ( Solrdocument solrdocument : results) { system. println (+ solrdocument.get ()); System: println (+ solrdocument.get ()); System: println (+ solrdocument.get ()); System: println (+ solrdocument.get ()); } system. println (); list<person> persons = result.getbeans (person.); system. println (+ persons.size () + ); (person Person : persons) { System: println (person); } (solrclient) } getspell (String core) Exception { HttpSolrClient solrClient = Getsolrclient (+ core); solrquery sq = Solrquery (); sq.set (, ); sq.set (, ); queryresponse query = solrclient.query (sq); &nbSp; solrdocumentlist results = query.getresults (); count = results.getnumfound (); (count == ) { spellcheckresponse spellcheckresponse = query .getspellcheckresponse (); list<collation> collatedresults = spellCheckResponse .getcollatedresults (); (collation collation : collatedresults) { &nbSp; numberofhits = collation.getnumberofhits (); system. println (+ numberofhits); List<Correction> misspellingsAndCorrections = collation .getmisspellingsandcorrections (); (correction correction : Misspellingsandcorrections) { string source = correction.getoriginal (); &nBsp; string current = correction.getcorrection (); system. println (+ current + + source); } } } { (solrdocument solrdocument : results) { collection<string> fieldnames = solrdocument.getfieldnames (); (string field : fieldnames) { &Nbsp; system. println (+ field + + solrdocument.get (field)); } } } (solrclient); } httpsolrclient getsolrclient (String core) { HttpSolrClient hsc = Httpsolrclient (+ core); hsc; } &NBSP;&NBSP;&NBSP;&NBSP;COMMITANDCLOSESOLR (httpsolrclient solrclient) Exception { Solrclient.commit (); &nBsp;solrclient.close (); }}
This article is from the "Good Water" blog, please be sure to keep this source http://yushiwh.blog.51cto.com/2911795/1969222
SOLR API Operations