How to check if a solr query will return data before actually loading the data?

StackOverflow https://stackoverflow.com/questions/23351545

  •  11-07-2023
  •  | 
  •  

質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top