Did you know - previousCriteria and cfsearch

This post is more than 2 years old.

Here is a little feature I don't see folks use too often, and it wasn't actually working right for Solr in ColdFusion 9.0 (but corrected in 9.0.1), but did you know that it was possible to perform a search within the results of a previous search using ColdFusion's Solr integration? (Technically this is also possible with Verity, but it's best we ignore this as it doesn't have much a life expectancy anymore in ColdFusion.)

In simplest terms, providing previousCriteria to the cfsearch tag will perform a "search within a search", so if searching for food, for example, returned 100 results out a possible 1000, doing a second search for beer and using previousCriteria="food" will return a number smaller.

Here is an incredibly simple example:

<cfsearch collection="cfdocs" criteria="cfsearch" name="results"> <cfoutput>Search for coldfusion: Returned #results.recordCount# results.<p/></cfoutput>

<cfsearch collection="cfdocs" criteria="solr" previouscriteria="cfsearch" name="results"> <cfoutput>Search for cffeed within coldfusion: Returned #results.recordCount# results.<p/></cfoutput>

<cfsearch collection="cfdocs" criteria="solr" name="results"> <cfoutput>Search for cffeed by itself: Returned #results.recordCount# results.<p/></cfoutput>

In this example my initial search is for cfsearch itself (I'm searching the ColdFusion docs so hopefully that makes sense). I then search for solr and provide cfsearch as the previousCriteria. As a final test, I search for solr by itself. My results were:

Search for coldfusion: Returned 39 results.
Search for cffeed within coldfusion: Returned 7 results.
Search for cffeed by itself: Returned 24 results.

Nice little feature, and interesting to use, but it seems like something you don't encounter in the wild too much. I wonder if it confuses users too much? Either way - hope this little tidbit helps.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Mihai Baboi posted on 4/11/2011 at 9:11 AM

Actually, it's not that confusing. I just can't seem to think about a real life example where you would use it. Maybe a multi-step search, that goes from broad to narrow. Just a thought...

Comment 2 by Tom Jenkins posted on 4/11/2011 at 11:40 AM

Actually I needed this very function recently for a client who wanted the ability to refine search results. They have literally 1000's of documents / pages indexed, so this proved hugely useful.


Comment 3 by Bud Fox posted on 4/12/2011 at 12:54 AM

Hey man! Ray Camden isn't the only one who reads the ColdFusion documentation, ya know! Nah, just teasin! Thanks for the tip Raymond! :)

Comment 4 by Jaana Gilbert posted on 4/13/2011 at 8:47 PM

I've had to use similar approach when I used Lucene for product search on a custom built eCommerce apps that carries over 180k products. Eventually gave up on Lucene and went with full-text search leveraging the power of MSSQL 2008 and caching.

Might be fun to try SOLR now that this client is on dedicated 9.01 server.

Comment 5 by Raymond Camden posted on 4/13/2011 at 8:53 PM

Does MSSQL/Full Text support something like previousCriteria?

Comment 6 by Jaana Gilbert posted on 4/13/2011 at 9:03 PM

No, it doesn't, but I found a very fast way to process through the huge amount of products by utilizing
CONTAINSTABLE and then returning only the desired amount of products per page with ROW_NUMBER() OVER.

For the search within previous results, I just passed in the previously found items in a string and had that as the first filter inside the SQL query.
Super fast; both Verity (2009) and Lucene (2010) were left on the starting line by the time my current approach had already searched through several varchar(MAX) fields looking for the criteria in question and returned the first set of data on the page. We went from over 15000+ms and larger to 200-300ms on identical searches.

Comment 7 by Raymond Camden posted on 4/13/2011 at 9:27 PM

Interesting, thanks for sharing that Jaana.