Tuesday, September 16, 2008

SharePoint search Step by Step (Part 2)

Hi we are back for search again.
If you missed then here is the link of SharePoint search Step by Step (Part 1)

Now as per continuation to the part 1 we are discussing for the second step.
After getting result in SharePoint default search we will get the crawled content programmatically.
Now for that we referred a lot of link on the net and here are the most useful ones and we are very thankful to them for guidance.
MSDN: Windows SharePoint Services Search SQL Syntax

The secrets of SQL Syntax Queries for Relevant Results in MOSS 2007 Enterprise Search

Here we go.

First of all we will show you how to search for a particular lists column programmatically. If you want the search from some related lists or entire site or in documents only then everything can be done but for that concept should be clear.

As you already created your separated content source (check part one). now we have to create a scope for the same.

How create a Scope?
Go to “Shared Service Provider site (SSP site)”
Go to “Search Settings.”
In the “Scopes “section click “View scopes”
Click in the “New Scope “
Give Title and description and press ok.
After that click on add rules link appear as per screen shot.

Click on that Add rules link.
Here is the catch.
Please refer this technet link for Manage scope rules

Now as we are searching from only one specific list so we are using the first option (Web Address (http://server/site)).

After adding rule to the scope go to search setting page and click “Start update now”

Wait for scopes to Update.
After that click in view scopes in your newly created scope items is start appearing.
Now time to create a metadata property.

How to create metadata property?

Go to SSP site -- search setting -- Metadata property mappings – New Managed Property

Name the property

Select second option[Include values from a single crawled property based on the order specified]

Click on add mapping

Select a “Sharepoint” category.
And find your column. It should named with prefix “ows_(your field name)”
Press ok

If you will not find your column then crawl your content source once.
And do not forget to check Allow this property to be used in scopes.
Only then we can use it in Code.

Full crawl your content source.

Now you are ready to rock.

Here is the simple code snippet for the query
SPSite spSite = new SPSite("<<your site name>>");
ServerContext src = ServerContext.GetContext(spSite);

using (Microsoft.Office.Server.Search.Query.FullTextSqlQuery fquery = new Microsoft.Office.Server.Search.Query.FullTextSqlQuery(src))
fquery.StartRow = 0;
fquery.RowLimit = 10;
fquery.HighlightedSentenceCount = 3;
fquery.EnableStemming = true;
fquery.TrimDuplicates = false;
fquery.KeywordInclusion = KeywordInclusion.AllKeywords;
fquery.ResultTypes = ResultType.RelevantResults;
string strcolumns = "Rank,Title, IsDocument,WorkId,Title,Author,HitHighlightedSummary,Size,Path,PictureThumbnailURL,CollapsingStatus,SiteName,Write,Description,ContentClass,HitHighlightedProperties";

fquery.QueryText = "SELECT " + strcolumns + " FROM Scope() WHERE ";
fquery.QueryText += " \"scope\" ='<<your ' AND ";
fquery.QueryText += “ FREETEXT(<<Your managed property name”, '" + << your search text >> + "') ) ";
fquery.QueryText += " ORDER BY Rank Desc";
ResultTableCollection rtCol = fquery.Execute();
ResultTable relevantResults = rtCol[ResultType.RelevantResults];
DataTable dt = new DataTable();
dt.Load(relevantResults, LoadOption.OverwriteChanges);
<<bind dt with grid>>

There is another discussion for how to use FullTextSqlQuery.
We will try provide you full information regarding this code, that how this code works, soon.
Try this stuff and let us know where you stuck.


Juan said...

Hey thanks for taking the time for a good blog article. It's not groundbreaking but it's nice to have it laid out for you. Thanks


Anonymous said...

Good article...

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com