wenchanter 2012-05-21
solr(5)binderandgrape
1.bindertoconvertjavaobject2documentdocument2object.
Hereismytestcaseclass,wecancovertthemveryeasily.
packagecom.sillycat.easyhunter.plugin.solr;
importorg.apache.solr.client.solrj.beans.DocumentObjectBinder;
importorg.apache.solr.common.SolrDocument;
importorg.apache.solr.common.SolrDocumentList;
importorg.apache.solr.common.SolrInputDocument;
importorg.apache.solr.common.SolrInputField;
importorg.junit.Test;
importcom.sillycat.easyhunter.model.User;
publicclassSolrBinderTest{
@Test
publicvoidcreateDoc(){
SolrInputDocumentdoc=newSolrInputDocument();
doc.addField("id",System.currentTimeMillis());
doc.addField("name","SolrInputDocument");
doc.addField("age",22,2.0f);
doc.addField("like",newString[]{"music","book","sport"});
doc.put("address",newSolrInputField("guangzhou"));
doc.setField("sex","man");
doc.setField("remark","chinapeople",2.0f);
System.out.println(doc);
}
@Test
publicvoiddocAndBean4Binder(){
SolrDocumentdoc=newSolrDocument();
doc.addField("id",456);
doc.addField("name","SolrInputDocument");
doc.addField("likes",newString[]{"music","book","sport"});
doc.put("address","guangzhou");
doc.setField("sex","man");
doc.setField("remark","chinapeople");
DocumentObjectBinderbinder=newDocumentObjectBinder();
Useruser=newUser();
user.setId(222);
user.setName("JavaBean");
user.setLike(newString[]{"music","book","sport"});
user.setAddress("guangdong");
System.out.println(doc);
//User->>SolrInputDocument
System.out.println(binder.toSolrInputDocument(user));
//SolrDocument->>User
System.out.println(binder.getBean(User.class,doc));
SolrDocumentListlist=newSolrDocumentList();
list.add(doc);
list.add(doc);
//SolrDocumentList->>List
System.out.println(binder.getBeans(User.class,list));
}
@Test
publicvoiddocMethod(){
SolrInputDocumentdoc=newSolrInputDocument();
doc.addField("id",System.currentTimeMillis());
doc.addField("name","SolrInputDocument");
doc.addField("age",23,1.0f);
doc.addField("age",22,2.0f);
doc.addField("age",24,0f);
System.out.println("keyset:"+doc.entrySet());
System.out.println(doc.get("age"));
//排名有用,类似百度竞价排名
doc.setDocumentBoost(2.0f);
System.out.println(doc.getDocumentBoost());
System.out.println(doc.getField("name"));
System.out.println(doc.getFieldNames());//keys
System.out.println(doc.getFieldValues("age"));
System.out.println(doc.getFieldValues("id"));
System.out.println(doc.values());
}
}
2.WritegroovyclientinHttpSolrServerGroovyTest.groovy
packagecom.sillycat.easyhunter.plugin.solr
importorg.apache.solr.client.solrj.SolrQuery
importorg.apache.solr.client.solrj.SolrServerException
importorg.apache.solr.client.solrj.impl.HttpSolrServer
importorg.apache.solr.client.solrj.response.QueryResponse
importorg.apache.solr.common.SolrDocumentList
importorg.apache.solr.common.params.SolrParams
classHttpSolrServerGroovyTest{
staticmain(args){
Stringurl="http://localhost:8983/solr"
HttpSolrServerserver=newHttpSolrServer(url);
Stringquery="name:DDR";
SolrParamsparams=newSolrQuery(query);
try{
QueryResponseresponse=server.query(params);
SolrDocumentListlist=response.getResults();
for(inti=0;i<list.size();i++){
println(list.get(i));
}
}catch(SolrServerExceptione){
e.printStackTrace();
}
println'done'
}
}
errormessage:
java.lang.ExceptionInInitializerError
Causedby:java.lang.RuntimeException:NosuitableClassLoaderfoundforgrab
atsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
solution:
No,Ididnotsolvethisproblem.
Atlast,wecanrunthefunctionas'GroovyScript'and'JavaApplication'.
Iwillcheckthissomeotherday,ifIreallyneedgroovyinmyarchitect.
references:
http://wiki.apache.org/solr/Solrj
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://groovy.codehaus.org/Grape