我使用SolrJ客户端调用Solr服务,有没有办法修改我从Solr获得的响应,比如在响应文档中添加新字段。
QueryResponse rsp = null;
if (server != null) {
try {
rsp = server.query(solrParams);
} catch (Exception e) {
e.getMessage();
}
}
如果我的回答如下所示
{
"responseHeader":{
"status":400,
"QTime":15,
"params":{
"q":"*:*",
"indent":"true",
"wt":"json",
"group":"false"}},
"response":{"numFound":1,"start":0,"docs":[
{
"Name":"Shirt",
"inventory":"Nonn",
"launchYear":2004,
"Desc":"Men's Shirt",
"ilnNumbers":"25326,25338,25341,29617,39267",}
}}
我需要更改此响应以添加新字段
{
"responseHeader":{
"status":400,
"QTime":15,
"params":{
"q":"*:*",
"indent":"true",
"wt":"json",
"group":"false"}},
"response":{"numFound":1,"start":0,"docs":[
{
"Name":"Shirt",
"inventory":"Nonn",
"launchYear":2004,
"myCustomField":"Test",
"Desc":"Men's Shirt",
"ilnNumbers":"25326,25338,25341,29617,39267",}
}}
这可能吗?
答案 0 :(得分:0)
为您的架构定义一个POJO类以及getter和setter。
class POJO {
@Field
private String name;
@Field
private Integer inventory;
@Field
private Integer launchYear;
@Field
private String desc;
@Field
private String ilnNumbersl;
private String myCustomField;
public POJO(String name, Integer inventory, Integer launchYear, String desc, String ilnNumbersl) {
this.name = name;
this.inventory = inventory;
this.launchYear = launchYear;
this.desc = desc;
this.ilnNumbersl = ilnNumbersl;
}
//getters and setters for each instance member.
}
现在将solr响应转换为List,并将所需的自定义字段添加到查询返回的每个结果中。
QueryResponse rsp = null;
if (server != null) {
try {
rsp = server.query(solrParams);
List<POJO> list = rsp.getBeans(POJO.class); // convert to list of POJO instance
// now add the custom field for each
} catch (Exception e) {
e.getMessage();
}
}
答案 1 :(得分:-1)
从Solr文档中尝试JsonResponceWriter。这有助于您解决问题。
<queryResponseWriter name="json" class="solr.JSONResponseWriter">
<!-- For the purposes of the tutorial, JSON response are written as
plain text so that it's easy to read in *any* browser.
If you are building applications that consume JSON, just remove
this override to get the default "application/json" mime type.
-->
<str name="content-type">text/plain</str>
</queryResponseWriter>
这将以Text格式为您提供queryResponce。修改根据您的使用做出响应。