1

I am searching through solr. it gives me response in json. like following:

 {response
 {numfound:# , docs{
  [
   {
    id="#"
    model="#"
   }
   {
    id="#"
    model="#"
   }
  ]
  }
  }

I want to extract just the ids from this and make java array list from them.

Can someone please tell me how i can do that in coding language?

question: how to just extract id from son string and convert then into java array list if i am using hashmap or objectmapper in java?

thanx

user1964901
  • 29
  • 1
  • 7

3 Answers3

1

If you want to convert into java objects, you can work with Solr Client Solrj
Solrj will give you and easy option to query Solr and read the xml to convert into java objects
For JSON you can use jackson library for parsing Solr response.

Jayendra
  • 52,349
  • 4
  • 80
  • 90
1

Here is a small program that will read the data using the Gson library.

@Test
public void testRead() throws IOException {
    final String testUrl = "http://localhost:8983/solr/collection1/select?q=*%3A*&wt=json&indent=true";
    String out = new Scanner(new URL(testUrl).openStream(), "UTF-8").useDelimiter("\\A").next();
    SolrResult result = new Gson().fromJson(out, SolrResult.class);
    List<String> ids = new ArrayList<String>();
    for (Doc doc : result.response.docs) {
        ids.add(doc.id);
    }
    System.err.println(ids);
}

It uses the following three small classes as a Java representation of the response:

public class SolrResult {
    public Response response;
}

class Response {
    public int numFound;
    public int start;
    public List<Doc> docs;
}

class Doc {
    public String id;
}

I hope this helps.

user152468
  • 3,202
  • 6
  • 27
  • 57
0

As Jayendra pointed you can use SolrJ if you are querying Solr using Java. I don't suggest to use XML as response format due to XML parsing overhead. SolrJ supports java binary object encoding (using commons-codec) out-of-the-box and by default on queries.

To use it on updates you have to set it up manually using

server.setRequestWriter(new BinaryRequestWriter());

and you have to enable the BinaryUpdateHandler on the server too, using:

 <requestHandler name="/update/javabin" class="solr.BinaryUpdateRequestHandler" />

In the case you don't want to use SolrJ you can parse it using GSON. Here you have and example.

Community
  • 1
  • 1
Samuel García
  • 2,199
  • 14
  • 21
  • So, doing this. What is the outcome? i mean in my question by doing this i will get result of the query in json format? how can i extract just the ids(above mentioned example in my question). – user1964901 Feb 11 '13 at 02:02