Implementing +/- boolean search in .Net version

Welcome to my first real post as a Mondosoft blogger.

Today I am going to share some code I initially made to follow up on a feature request from the Vatican. But first a little backstory, so bear with me.

Backstory:
At some point back in time, MondoSearch became .Net compatible meaning that search and result pages was now available as both standard webform .aspx pages - and through a web service interface. It's actually so long ago that I honestly do not remember.
Since then, I have one more than one occasion been building customized solutions for various customers. Although these were often merely of a layout nature, some have required a bit of creativity and development. Different customers have different needs, I guess this is true for most business. Fortunately, the .Net interface provided with MondoSearch allows for almost anything you can think of. And after MQL was introduced, it has just become easier to tweak the product to fit special needs.


Getting back to the subject I'd like to share the fact that many companies check wellknown global search engines, and then ask if we can do the same (or provide similar features). This is only natural, since people can relate to something they use on a daily basis when talking about search. Of course there are many differences between global search and what we do - but thats a whole other story.

So when we we're asked if that handy way of doing boolean search by utilizing the '+' and '-' prefix was possible, I
had to dig into the abilities of the SearchProvider. To make something like this work I needed a way to "keep out" some parts of a query while acting on the rest.

The following settings / searchoptions came in handy:
  • Option to switch between AND / OR search
  • Ability to exclude words from the query
These two settings plus some C# code was all it took.

First off a function to switch between doing 'AND' or 'OR' searches had to be made. Since an 'OR' search should always be standard (or in most cases is) the function should only switch to 'AND' in case somebody was searching for the existence of two or more given words where at least one of these had to be present. This was simply achieved by looking for the '+' prefix.

In case one or more words had the '-' prefix, they should be added to the SearchExcludeQuery parameter. A simple loop through the entered search query took care of that.

Finally the actual search query had to be cleaned of any of the above commands before proceeding with the request.


3 functions and 1 prepared SearchControl resulted in this code (.txt file).
The code example should speak for itself. But feel free to visit the Vatican and test it yourself.
An example from the top of my head would be to do a standard search for "pope benedict" (without the quotes) which should yield over 6000 results (all documents containing either "pope" or "benedict").

Then try doing a '-' prefix search for "pope -benedict" (without the quotes) which should yield about 4600 results (since we are getting all documents written by a Pope who did not go by the name Benedict).

Finally try something like searching for "pope +ratzinger -benedict" (without the quotes) which yields about 90 results (here we get results about Ratzinger before he was named Benedict and entitled as Pope).

All thanks to impleting prefixed search queries or +/- booleans as I often call it.

Print | posted on Tuesday, April 17, 2007 11:42 PM

Feedback

6/8/2007 4:06 PM
#1

# 汽气美容

XIAO MEI
Title  
Name  
Email
Url
Comments   
Please add 3 and 3 and type the answer here: