Monday, 5 May 2008

Partial Keyword Search/Wildcard Search Webpart

Im in the process of writting a webpart that does partial keyword search (or wildcard search). I have it working and Im prettying it up at the moment. I will post the code up there once i've completed the web part, so if anyone want it, shoot me an email and Ill forward it to them once it's complete.

Fadi

8 comments:

Bernhard said...

Hi,

have to solve the same problem in our intranet.

Would be great if you could publish it to the world!

Rgds
Benno

Fadi Noja said...

Hi Bernhard,

here's a bit of the code. I have the compelte code at work, but here's where i started from

private void fullTextQueryExecute(string strQueryText)
{
//Create the FullTextSqlQuery object for searching the index
FullTextSqlQuery kRequest = new FullTextSqlQuery(ServerContext.Current);

////Prepare the string to use wildcards
string strQuery = "'\"" + strQueryText.Replace(" ", "*\" AND \"") + "*\"'";
////Set the query text to return columns and search appropriate fields.
////The fields are based on what's indexed in the search settings.
kRequest.QueryText = "select " + this._searchColumns + " FROM Scope() WHERE (\"scope\"='All Sites' "
+ ") AND CONTAINS(*," + strQuery + ") ORDER BY Rank DESC";
//Return all result types except best bets
kRequest.ResultTypes = ResultType.RelevantResults;
//Get the result table collection from the query
ResultTableCollection resultTbls;
try
{
resultTbls = kRequest.Execute();
}
catch (Exception ex)
{
///Write error out to trace listener
System.Diagnostics.Trace.TraceError("Exception: " + Environment.NewLine + ex.ToString());
resultTbls = null;
return;
}
if (((int)ResultType.RelevantResults != 0) && (resultTbls.Exists(ResultType.RelevantResults)))
{
ResultTable tblResult = resultTbls[ResultType.RelevantResults];
if (tblResult.TotalRows == 0)
{
lblQueryResult.Text = "No Search Results Returned.";
}
else
{
lblQueryResult.Text = string.Empty;
ReadResultTable(tblResult);
}

}
}

What you have to do is create a function called ReadResultTable to display the results properly. You can expand it to return the XML to the out of the box search results webpart (that's what I'm doing now).

You might also want to expand it to allow AND and OR statements in there.

If you need a hand, let me know.

sachin said...

Hi,

We have also same requirement for our intranet. It will be great if you can let us know how to build this.

It will be really helpful if you can show us how.

Thanks
Sachin

Fadi Noja said...

Hi,

In a few days Ill post a complete webpart for you guys to download and play with. I won't include everything, but enough for you to do the basic search with. That way you can extend it to meet your needs.

Fadi

Anonymous said...

Hi Fadi.

Did you get a chance to post the completed webpart referred to below?

I'd be interested to see what you've managed to do and build upon it if possible.

Thanks,
Nick.

Nick said...

Hi Fadi.

I've just come across this and it would suit what I'm doing perfectly.

Did you end up completing your work on this and publishing it?

Thanks,
Nick.

Fadi Noja said...

It's finished and working. You can check it out on www.fahcsia.gov.au. I can't put up the exact one that I did there, however Im working on a new one and will put it up.

Bramley said...

Can you please send me the details of your web Part.