Sateesh K
Sateesh K

Reputation: 1091

SolrJ doesn't return any documents where as URL in browser returns Data

I have indexed lot of documents (by reading data from DB) using Solr and the admin console shows the No.of Documents properly.

And when i query for these documents using the following URLs i get the Results back.

`http://localhost:8983/solr/objects/select?q=(OBJ_ID:20110259365%20OR%20OBJ_ID:20110259364)&wt=json&indent=true

http://localhost:8983/solr/objects/select?q=(OBJ_ID:20110259365)&wt=json&indent=true`

How ever when i tried to get the Documents using java program it doesn't return any documents. Not sure what i am doing wrong.

Here is my Java Code.

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr/objects");
    Map<String,String> docsMap = new HashMap<String,String>();
    org.apache.solr.client.solrj.SolrQuery query = new org.apache.solr.client.solrj.SolrQuery();
    //query.setStart(0);
    query.addFilterQuery("(OBJ_ID:20110259364)");
    try{
        logger.info("Query=["+query+"]");
        QueryResponse response = solrServer.query(query);
        SolrDocumentList docList = response.getResults();
        SolrDocument doc =null;
        Iterator<SolrDocument> docIter = docList.iterator();
        logger.info("docList Size=["+docList.size()+"]");
        if(docList!=null && docList.size()>0){
            while(docIter.hasNext()){
                doc = docIter.next();
                docsMap.put(doc.getFieldValue("OBJ_ID").toString(),doc.toString());
            }
        }
    catch(Exception e){
        e.printStackTrace();
    }

I am using the Solr Version 5.0.0.

And in my pom.xml i have the following dependency.

<dependency>
  <groupId>org.apache.solr</groupId>
  <artifactId>solr-solrj</artifactId>
  <version>5.0.0</version>
</dependency>

Thanks

Upvotes: 2

Views: 506

Answers (2)

Yann
Yann

Reputation: 1019

In your Java code, you are generating the following query (which should show in your log):

select?fq=OBJ_ID:20110259364

note the fq (filter query) instead of q. Because there is no query (q) parameter, no document will be matched, and the filter will run on an empty list, returning an empty list as a result.

What you want to do is:

org.apache.solr.client.solrj.SolrQuery query = new org.apache.solr.client.solrj.SolrQuery("OBJ_ID:20110259364");

and leave out the addFilterQuery. This will generate select?q=OBJ_ID:20110259364.

Upvotes: 2

Swaraj
Swaraj

Reputation: 589

I think you gave got some syntax error in this, Solr need a query parameter as q=*:* or any query but addFilterQuery() adds fq=*:*, as per your query I am posting the code for this. Hope this will help you.

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr/objects"); Map<String, String> docsMap = new HashMap<String, String>(); SolrQuery query = new SolrQuery(); query.setQuery("OBJ_ID:20110259364"); query.setStart(0); query.setRows(0); try { QueryResponse response = solrServer.query(query); query.setRows((int) response.getResults().getNumFound()); response = solrServer.query(query); SolrDocumentList docList = response.getResults(); for (SolrDocument doc : docList) { docsMap.put(doc.getFieldValue("OBJ_ID").toString(), doc.toString()); } } catch (Exception e) { e.printStackTrace(); }

Upvotes: 1

Related Questions