March 16th, 2010SharePoint Search on Steroids Part 3 – Storing Search Queries
This is a continuation of a series about creating a robust search U.I. for SharePoint.
We’ve created our U.I. elements and embedded them into the master page. We’ve also implemented JQuery and connected our U.I. to a web service to populate our auto completion.
The next questions is: “How do I populate it the auto completion with suggestions?”
Well, before we can answer that, we have to figure out how we are going to store the search queries in the first place. The challenge, as you might well imagine, is that we can’t just store every string value a user might type into the search box.
For instance, if several users type in the keywords “dress code”, we don’t want to store that phrase, multiple times, into the database.
After some lengthy discussions with my brother and a couple of napkin drafts, we came up with a rather simple solution.
Rather than store the entire phrase “dress code”, couldn’t we just parse the words “dress” and “code” and store them into a table with an identifier?
So, instead of this:
| ID | Phrase |
| 1 | dress code |
| 2 | dress code |
| 3 | dress code |
We get this:
| ID | Tag |
| 1 | dress |
| 2 | code |
I decided to create a stored procedure that would collect a search phrase, parse it into “tags”, and INSERT them into the “Tag” table. If the record already existed, then an error would be caught and I would move on.
Note: I won’t go into detail about how I connected the stored procedure to the user control. Suffice to say, I created my connections and parameters in my web service. The U.I. passes the phrase to the web service when the user executes a search. The web service then makes a connection to the database, collects the phrase into a parameter, and calls the stored procedure. This is all standard .NET data stuff and there are a plethora of articles on this subject.
Here is the SQL code for my stored procedure “spStoreSearch”:
-- Add the parameters for the stored procedure here @TagList nvarchar(255), @NetworkID nvarchar(10) AS BEGIN --Please refer to attached code.
Note:For some reason, my blog article wont post with the SQL code in it. Here’s a link to the script.
You might have noticed, I’m also storing a value of “NetworkID” into a table called “Context” and values of “ContextID”, “Ordinal”, and “TagID” in a table called “Context_Tag”. As you might’ve imagined at this point, I cannot just store the individual tags into the database. They would lose their context. In other words, I would only be able to say: “Guess what guys!!! The word ‘dress’ is the most popular search word!!!! WOOO HOOO!!! YEAH!” They’d look at me like I had lost my marbles. The word “dress” could have so many different contexts: “dress code”, “dress policy”, “informal dress”, “dress code violation”…You get the idea.
So I have to be able to store the contextual information in a meaningful way.
Also, I would like to know who’s completing the search.
Thankfully, SharePoint provides that information by way of collecting your network credentials. I wrote a blog post about how you can go about getting that kind of user information from SharePoint. I used the same methodology to get my “NetworkID” here.
Combined, the information would get stored like this:
Example: At 0750 on 03/16/2010, user “tlowderm (a.k.a Travis Lowdermilk) searched the phrase “dress code”
Tag Table
| ID | Keyword |
| 1 | dress |
| 2 | code |
Context Table
| ID | datetimestamp | NetworkID |
| 1 | 2010-03-16 07:50:04.343 | tlowderm |
Context_Tag
| ID | ContextID | Ordinal | TagID |
| 1 | 1 | 0 | 1 |
| 2 | 1 | 1 | 2 |
As you can see, this is as normalized as it’s gonna get (unless you can suggest otherwise?). You’ll also notice the column “Ordinal” in the Context_Tag table.
This is used to reference the order a sequence of tags were entered.
For example, if I were to query the phrase “dress code”, the result would be “code dress” if I were to ORDER BY tag alone.
The next challenge is putting together queries that present meaningful search data. Answering questions like: “What’s the most popular search phrase?” and “What searches has Travis requested?”.
In the next post, SharePoint Search on Steroids Part 4 – Retrieving Search Queries, I’ll detail how to retrieve this kind of information and how to enrich the user search experience by
injecting suggestions based on popular search phrases.

