Sonson
Sonson

Reputation: 1139

Solr: Determine if a document exists

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. ...

Upvotes: 5

Views: 2766

Answers (1)

Jayendra
Jayendra

Reputation: 52799

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);

Upvotes: 4

Related Questions