solr(5)binder and grape

本文介绍了一个使用 Solr 的 Java 绑定进行对象转换的测试案例,包括从 Java 对象创建 Solr 文档及从 Solr 文档转换为 Java 对象的过程。同时还提供了一个 Groovy 脚本示例,用于通过 HTTP SolrServer 进行查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值