by Keyvan Nayyeri via Keyvan Nayyeri on 11/8/2006 5:49:00 PM
You know Community Server has a default search engine (Search Barrel) and a commercial Enterprise Search engine. It's also possible to write custom search engines for it.
But there are some cases when you need to search with APIs and use search results in your custom modules or controls so it's worth to talk about search APIs in this post. Whether you use default Search Barrel, Enterprise Search or any other search engine, can access to your application search results through same APIs.
All means which let you to apply a search with APIs are available in CommunityServer.Components namespace. Search APIs are simple and straightforward and you'll see it.
The process of applying a search consists of three steps:
SearchQuery is a class in CommunityServer.Components namespace which lets you to define a search query by setting its properties as same as any search that you do on a site but it provides more options than what you have on site online.
As an example below is a code snippet of definition for an instance of SearchQuery object which searches for ".NET" with below clauses:
SearchQuery query = new SearchQuery();
query.QueryText = ".NET";
ApplicationType[] applications = {ApplicationType.Weblog,
ApplicationType.Forum};
query.ApplicationTypes = applications;
query.StartDate = DateTime.Parse("January 01, 2006");
query.EndDate = DateTime.Now;
String[] usernames = {"nayyeri", "X.Static", "daveburke"};
query.UserNamesToSearch = usernames;
query.SortBy = SearchSort.DateDescending;
query.PageSize = 20;
query.PageIndex = 0;
Next step is to pass a SearchQuery object to CSSearch.Search() static method in order to get a SearchResultSet object.
SearchResultSet has a collection of properties that contain search results and some properties about them. These properties are listed here:
By iteration through Posts and Replies properties you can get what you wanted. An example is presented below. It returns the most popular post (on basis of views) from first page of search results for above query.
Post GetMostPopularDotNetPost()
{
SearchResultSet results = CSSearch.Search(query);
if (results.HasResults)
int maxViews = 0;
Post popularPost = null;
foreach (Post post in results.Posts)
if (post.Views > maxViews)
maxViews = post.Views;
popularPost = post;
}
return popularPost;
return null;
Now playing: Ricky Martin - It's Alright
Original Post: CS Dev Guide: Search with APIs
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.