zelinka
zelinka

Reputation: 3413

Use solrj to look for results within only specific documents

I have a solr server and query it using solrj. Suppose I want to search for results within only specific documents, and I have the ID's of the documents I want to look in. How do I configure the query to only return results from a specified list of documents?

    List<String> documentList = ...; // collection of Strings of the 
                                     // ID's of the documents I want 
                                     // to look for
    this.query = new SolrQuery();
    this.query.setFields("id", "score");
    this.query.addSort("score", SolrQuery.ORDER.desc);
    this.query.addSort("id", SolrQuery.ORDER.desc);
    this.query.setQuery(searchString);

What do I need to to make it so that all of the documents returned by the query are documents whose id is in the list of acceptable documents?

Upvotes: 0

Views: 78

Answers (1)

David Faber
David Faber

Reputation: 12485

I've not used solrj much, but it should be as easy as adding a filter query (I'm assuming you don't want whether a document is acceptable to affect the score) with the document list, e.g.:

String filterQuery = "id:(1 OR 2 OR 3)";
this.query.addFilterQuery(filterQuery);

So you'll want to convert documentList into a string delimited by OR (and yes, I believe it does have to be uppercase).

If the number of acceptable documents is really large, then you'll have to make changes to your Solr configuration to allow a greater number of boolean terms in your query (I think the default is 512, or perhaps 1024; but I've used 32768 with no problems).

Upvotes: 1

Related Questions