简体   繁体   中英

Modify the Solr response using SolrJ

I'm using the SolrJ client to call the Solr service, is there any way to modify the response that I got from Solr like adding a new field in the response docs.

QueryResponse rsp = null;
    if (server != null) {
        try {
            rsp = server.query(solrParams);

        } catch (Exception e) {
            e.getMessage();
        }
    }

If my response is something like below

    {
  "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",}
    }}

I need to change this response to add a new field

 {
  "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",}
    }}

Is this possible?

Define a POJO class for your schema along with getters and setters.

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.
}

Now convert the solr response into List and add the required custom field to the each result returned by the query.

  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();
     }
   }

Try JsonResponceWriter from Solr Documentation.This will help you solve your problem.

    <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>

This will give you queryResponce in Text format. Modify That responce according to your use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM