5

I am a SolrJ beginner and want to find out whats the fastest way to determine if an document with an unique ID exists? I don't need the document, I just want to find out if it is already in the index.

Now I try something like this in SolrJ:

private boolean solrContainsId(final String id) {
    SolrQuery query = new SolrQuery("id:" + id);

    try {
        long count = server.query(query).getResults().getNumFound();
        return count > 0;
    } catch (SolrServerException e) {
        return false;
    }
}

I think there will be better (faster?) ways which don't need scoring etc. ...

Sonson
  • 1,129
  • 1
  • 11
  • 14

1 Answers1

4

Instead of searching for id equals, use the filter query which would not have any scoring as well would enable to use the fieldcache

SolrQuery query = new SolrQuery();
query.addFilterQuery("id:"+id);
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • Thanks, that was the thing I have been looking for. – Sonson Oct 31 '11 at 15:12
  • 1
    The filter query might not be the best solution here, because it creates a filter cache (bitmap for all the documents in the index). Creating filter cache for each and every document id seems problematic. I think terms query is better here, SOLR will cache the unique id field anyway. – lexk Nov 15 '12 at 08:58