I wrote a bit of code to perform a search using Sharepoint search functionality. The first thing I implemented was the keywords AND and OR – the normal boolean flags for searches. The next thing I implemented needed a bit more consideration. I wanted any words which were surrounded by quotes to be considered as 1 block of text. For example, “this is a quote” would be a search for the words this is a quote, in that order and with each word required. So the problem that I needed to solve was to separate out those blocks of text from the other words in the search.
My approach was to populate a generic dictionary with two kinds of objects – quoted blocks of text and separate words. The separate words could easily be rounded up into a string array. The quoted blocks needed to be separated out. I did that using a regular expression. Once I had identified those blocks of text, I added each one to the dictionary, with a key that had a sequential numbering scheme:
protected Dictionary<string, object> ParseSearchTerms(string searchTerm)
{
string[] words = null;
Dictionary<string, object> items = new Dictionary<string, object>(StringComparer.Ordinal);
// If there's no quoted text, just add a string array to the dictionary.
if (searchTerm.IndexOf('"') < 0)
{
words = searchTerm.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
items.Add("nonQuotedWords", words);
}
else
{
// Identify quoted text and add it to the dictionary with the key "quotedText".
// Remove such text from the string to be left with a bunch of words (or nothing)
// which will be added as a simple string array, with the key "nonQuotedWords".
Regex regex = new Regex("\"[^\"]*\\w*\"");
MatchCollection matches = regex.Matches(searchTerm);
int counter = 1;
foreach (Match match in matches)
{
items.Add("quotedText" + counter++, match.Value);
searchTerm = searchTerm.Replace(match.Value, string.Empty);
}
if (searchTerm.Length > 0)
{
words = searchTerm.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
items.Add("nonQuotedWords", words);
}
}
return items;
}