2

I am using Solr-5.0.0.

I am tying to query Solr using Java. I am using group by clause here.

Its my code

SolrQuery qry = new SolrQuery();

qry.setQuery("product_name:(laptops)");
qry.addFilterQuery("brand:dell OR brand: sony OR brand:samsung");
qry.setParam("group", true);
qry.setParam("group.field", "brand");
qry.setParam("stats", true);
qry.setParam("stats.field", "product_price");
qry.setFields("brand");
System.out.println(qry.toString());  

QueryRequest qryReq = new QueryRequest(qry);
QueryResponse resp = qryReq.process(solr);
System.out.println(resp.getResponse().toString());

// Here I am getting required response;
SolrDocumentList docs = resp.getResults();

//Below code giving me exception null pointer
for(int i = 0; i < docs.size(); i++) {
  System.out.println(docs.get(i)); 
}

But I am not able to parse the result of the query.I just need to get the brands and its corresponding count from the query in list or array

cheffe
  • 9,345
  • 2
  • 46
  • 57
Juhan
  • 1,283
  • 2
  • 11
  • 30
  • Could you paste the stacktrace to see where the NPE is coming from. Also check solr logs to see how many records are returned by solr for this search request. – Prabhu Velayutham Apr 03 '15 at 22:50

2 Answers2

0

I am not familiar with the SolrQuery class you used.

I used solr in my program by creating a URL(Solr request) and getting the response as JSon as below.

Define the request as url

String url = "http://"+solr_server+":"+solr_port+"/solr/collection_name/select?q="+brand+"&wt=json&indent=true";

Read the solr result as JSon from request url

JSONObject json = readJsonFromUrl(url);

Get the reponse and then docs - then iterate through the docs

JSONObject j1 = (JSONObject) json.get("response");
JSONArray j2 = j1.getJSONArray("docs");
for(int i=0;i<j2.length();i++){
    //do whatever you want to do
}

Remember to have the jar from http://www.java2s.com/Code/JarDownload/java-json/java-json.jar.zip in your classpath.

Vanaja Jayaraman
  • 753
  • 3
  • 18
0

Debug the query using a link and see if the query is built properly. Then you can adopt to java

Community
  • 1
  • 1
Ramzy
  • 6,948
  • 6
  • 18
  • 30