Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr: Determine if a document exists

Tags:

java

solr

solrj

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

like image 352
Sonson Avatar asked Oct 10 '22 14:10

Sonson


1 Answers

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);
like image 73
Jayendra Avatar answered Oct 13 '22 12:10

Jayendra