1、需要在/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf的solrconfig.xml加上
<requestHandler name="/select" class="solr.SearchHandler">
<!-- default values for query parameters can be specified, these
will 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>
<!-- 这行代码非常重要,如果没有这行,拼写检查,是不起作用的-->
<arr name="last-components">
<str>spellcheck</str>
以便spell能够生效
2、在/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf的schema.xml修改,加上字段,否则会报错
如:
<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"/>
=======================================================
下面是具体的实现的方法
com.ys.controller;
com.ys.bean.Person;
org.apache.solr.client.solrj.SolrServer;
org.apache.solr.client.solrj.impl.HttpSolrServer;
org.apache.solr.common.SolrInputDocument;
org.springframework.beans.factory.annotation.;
org.springframework.web.bind.annotation.;
org.springframework.web.bind.annotation.RequestMethod;
org.springframework.web.bind.annotation.;
java.util.*;
java.util.Map.Entry;
org.apache.solr.client.solrj.SolrQuery;
org.apache.solr.client.solrj.impl.HttpSolrClient;
org.apache.solr.client.solrj.response.QueryResponse;
org.apache.solr.client.solrj.response.SpellCheckResponse;
org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
org.apache.solr.common.SolrDocument;
org.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(, , , ));
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);
SolrDocumentList results = query.getResults();
count = results.getNumFound();
(count == ) {
SpellCheckResponse spellCheckResponse = query
.getSpellCheckResponse();
List<Collation> collatedResults = spellCheckResponse
.getCollatedResults();
(Collation collation : collatedResults) {
numberOfHits = collation.getNumberOfHits();
System..println(+ numberOfHits);
List<Correction> misspellingsAndCorrections = collation
.getMisspellingsAndCorrections();
(Correction correction : misspellingsAndCorrections) {
String source = correction.getOriginal();
String current = correction.getCorrection();
System..println(+ current + + source);
}
}
} {
(SolrDocument solrDocument : results) {
Collection<String> fieldNames = solrDocument.getFieldNames();
(String field : fieldNames) {
System..println(+ field + + solrDocument.get(field));
}
}
}
(solrClient);
}
HttpSolrClient getSolrClient(String core) {
HttpSolrClient hsc = HttpSolrClient(+ core);
hsc;
}
commitAndCloseSolr(HttpSolrClient solrClient)
Exception {
solrClient.commit();
solrClient.close();
}
}
本文转自yushiwh 51CTO博客,原文链接:http://blog.51cto.com/yushiwh/1969222,如需转载请自行联系原作者