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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how to use jackson library to get just the id's as an arraylist from json(as mentioned in above example)?
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.

Comments

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.