solr(5)binder and grape

solr(5)binder and grape

1. binder to convert java object2document document2object.
Here is my test case class, we can covert them very easily.
package com.sillycat.easyhunter.plugin.solr;

import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.junit.Test;

import com.sillycat.easyhunter.model.User;

public class SolrBinderTest {

@Test
public void createDoc() {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", System.currentTimeMillis());
doc.addField("name", "SolrInputDocument");
doc.addField("age", 22, 2.0f);

doc.addField("like", new String[] { "music", "book", "sport" });

doc.put("address", new SolrInputField("guangzhou"));

doc.setField("sex", "man");
doc.setField("remark", "china people", 2.0f);

System.out.println(doc);
}

@Test
    public void docAndBean4Binder() {
        SolrDocument doc = new SolrDocument();
        doc.addField("id", 456);
        doc.addField("name", "SolrInputDocument");
       
        doc.addField("likes", new String[] { "music", "book", "sport" });
       
        doc.put("address", "guangzhou");
       
        doc.setField("sex", "man");
        doc.setField("remark", "china people");
       
        DocumentObjectBinder binder = new DocumentObjectBinder();
        User user = new User();
        user.setId(222);
        user.setName("JavaBean");
        user.setLike(new String[] { "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));
       
        SolrDocumentList list = new SolrDocumentList();
        list.add(doc);
        list.add(doc);
        //SolrDocumentList ->> List
        System.out.println(binder.getBeans(User.class, list));
    }

@Test
    public void docMethod() {
        SolrInputDocument doc = new SolrInputDocument();
        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. Write groovy client in HttpSolrServerGroovyTest.groovy
package com.sillycat.easyhunter.plugin.solr

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.common.SolrDocumentList
import org.apache.solr.common.params.SolrParams

class HttpSolrServerGroovyTest {

static main(args) {
String url = "http://localhost:8983/solr"
HttpSolrServer server = new HttpSolrServer( url );

String query = "name:DDR";
SolrParams params = new SolrQuery(query);
try {
QueryResponse response = server.query(params);

SolrDocumentList list = response.getResults();
for (int i = 0; i < list.size(); i++) {
println (list.get(i));
}
} catch (SolrServerException e) {
e.printStackTrace();
}

println 'done'
}

}

error message:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: No suitable ClassLoader found for grab
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
solution:
No, I did not solve this problem.

At last, we can run the function as 'Groovy Script' and 'Java Application'.

I will check this some other day, if I really need groovy in my architect.

references:
http://wiki.apache.org/solr/Solrj
http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html
http://groovy.codehaus.org/Grape

你可能感兴趣的:(Binder)