Thursday, July 19, 2012

SharePoint search Step by Step

Part 1:
In this post we will show you only how to configure search from SSP.

Most of the time we have requirement for implementing custom SharePoint search. Because of custom business logic we need to search SharePoint content programmatically.

There is a lot of stuff available for SharePoint ECM search but not step by step from starting.

As this is our first post in search series so we are explaining the basic search.

The scenario: (in simple functional way)
We have a web application and in that a site collection.
In that site collection on the Top level site only we have lists and document libraries.
And we have to search on that site collection.
Search result will be on simple custom web part but not on the default one.
And the business logic must apply on result set. (We will take this in later posts)


Now here is how we start.
This post will explain how to configure SharePoint default search step by step.
Steps:
1) Make SharePoint default search up and running.
2) After getting result in SharePoint default search we will get the crawled content programmatically.
3) Use Microsoft.Office.Server.Search DLL to query and get specific result according to business logic.

Step 1)
Prerequisite: you must have and admin rights to configure search.
3 services should be started
1) Windows SharePoint Services Search
2) Office SharePoint Server Search
3) Indexing Service

Here is the link from where we got the basic reference.

First as per screen shot go to edit properties of SSP and change your password with the current one. This is none mandatory step but better to do so.



Go to SSP site.
Click on search setting.



Change Default content access account: with admin account



Click on “Content sources and crawl schedules”.
Edit “Local Office SharePoint Server sites”
Remove your site collection from Start Addresses and press ok.

Than create new contain source with Content Source Type as SharePoint Sites

And Start Addresses as your site collection. (that you removed from local office SharePoint Server Site)

And start full crawl



After these steps your default search should start.
Part2:
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>>
}


 

No comments:

Post a Comment