Question

I'm about to write some code that will run numerous solr queries. But I wont need to load the documents from these queries every time I run the query. I mean, I may run 6 queries but I may need to load the documents from the 5th query only.

So to speed things up, is there a way to run a solr query that would only return a value indicating if this query will return documents or not ? My aim is not to load documents unless I will actually use them. Just to speed up the process.

Was it helpful?

Solution

There is 2 ways to achieve that :

  • Using fl parameter you can set a field list to return : set an empty string, or set a field name that does not exist in your index so that the request doesn't fallback to the default field list. It should return a list of empty documents :

    SolrQuery query = new SolrQuery();
    query.setQuery("something to search");
    query.set("fl", "unknown_field");
    SolrDocumentList out = server.query(query).getResults();
    
  • using rows parameter (should be preferred), ask solr to return 0 document :

    query.setRows(0); 
    server.query(query).getResults();
    

In both cases getNumFound() method should return the number of matched documents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top