Search Smith

ColdFusion, SQL queries, and, of course, searching

Posts Tagged ‘ColdFusion’

Departing from the confines of CFSEARCH

Posted by David Faber on January 11, 2012

Here are the beginnings of a series of blog posts on using Apache Solr with ColdFusion:

http://fusion.dominicwatson.co.uk/categories/Solr/

Unfortunately, it doesn’t look like the author has made any further posts on the topic. However, the first post, Departing from the confines of cfsearch, may be helpful in showing how Solr can be used on multiple platforms (ColdFusion, Railo, BlueDragon); while the second post, Installing Solr with Tomcat, may be helpful for those who want to set up a Solr server independently of ColdFusion.

Posted in ColdFusion, Solr | Tagged: , , , | Leave a Comment »

Solr Query Parameters

Posted by David Faber on January 10, 2012

A list of common Solr query parameters can be found here: http://wiki.apache.org/solr/CommonQueryParameters. 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.

Posted in ColdFusion, Solr | Tagged: , , , , | 1 Comment »

ColdFusion 9 and Solr: Weak typing vs. strong

Posted by David Faber on January 10, 2012

I was trying to index a collection recently, and I kept getting an error when trying to add a value to a particular field. The field in question was defined in schema.xml as an “slong,” indexed, stored, and required. ColdFusion returned a weird error like the following:

Error_while_creating_field_user_cnttypeslongproperties=indexedstoredomitNormssortMissingLast_from_value100__orgapachesolrcommonSolrException_Error_while_creating_field

So even though the value appeared to be a number, Solr was seeing it as something other than a number. It took me a couple of tweaks, but I finally hit upon a solution to the problem – using ColdFusion’s javaCast() function when invoking the addField() method:

<cfset temp.addField("user_cnt", javaCast("long", user_cnt)) />

The underlying problem is that ColdFusion is weakly-typed, and is quite happy to convert strings to numbers and vice versa, while Java is not.

Posted in ColdFusion, Solr | Tagged: , , , , , , , | 1 Comment »