Search Smith

ColdFusion, SQL queries, and, of course, searching

Solr Query Parameters

Posted by David Faber on January 10, 2012

A list of common Solr query parameters can be found here: I mentioned in a previous post that pagination is “built-in” to Solr. One of the more frustrating things about the <CFSEARCH> tag is that there is no provision to start on a particular row and return a set number of rows if you want to use a sort other than the default. In a previous example we wanted to index a collection of articles. It’s reasonable that a user might want to sort on publication date rather than relevance, but <CFSEARCH> doesn’t give us a sorting option along with the startrow and maxrows attributes. If we limit the search using startrow and maxrows, and then sort after the fact, our results will not be correct.

Solr, however, does have the parameters we need:

  • sort – We can sort on any field defined in schema.xml as long as it is (a) “indexed” (able to be searched) and (b) NOT “multiValue”. The field in question does not have to be “stored”.
  • start – This is actually an offset rather than a start row as it would be used in ColdFusion. So the value of the “start” parameter is zero (0) by default (while in ColdFusion’s <CFSEARCH>, the “startrow” attribute is 1 by default). Just FYI in case you’re seeing some off by one errors.
  • rows – This is the maximum number of rows to be returned by the query.

The combination of these three parameters allows us to paginate results without returning everything, sorting, and then paginating. Furthermore, the ability to customize the schema of the Solr collection allows us to create a search index with all of the data we need already included, alleviating the need to query our database based on the search results.

Update: I was mistaken about the fields on which the sort parameter can be used. In order for it to be used effectively, the field must be (a) indexed, (b) not multiValue, and (c) a single term. This means that fields that are tokenized can’t be sorted upon, unless they use an analyzer that produces a single term. In particular, you can’t sort on text and text_* fields. So if you want to sort on title, for example, you must either define title as something other than text (or text_ws), such as string, or create an additional field with the same contents:

<field name="title_s" type="string" indexed="true" stored="true" required="true" />

Common query parameters – sort

Sorting can be done on the “score” of the document, or on any multiValued="false" indexed="true" field provided that field is either non-tokenized (ie: has no Analyzer) or uses an Analyzer that only produces a single Term (ie: uses the KeywordTokenizer)

Hope this helps.

One Response to “Solr Query Parameters”

  1. […] web service and return XML; or (3) we could use the Solr web service and return JSON. We’ve talked about <cfsearch> and its limitations before so I’ll limit this discussion to the second and third […]

Leave a Comment

Your email address will not be published.