CSharpFeeds - All your C# feeds in one place.

Sponsors

Wednesday, November 08, 2006

CS Dev Guide: Search with APIs

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:

  • Defining a SearchQuery object.
  • Calling CSSearch.Search() method by passing a SearchQuery object and getting a SearchResultSet object.
  • Working on this SearchResultSet to get appropriate results.

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:

  • Searches in Weblog and Forum applications.
  • Searches for results between first day of 2006 and current date and time.
  • Searches for all posts by "nayyeri", "X.Static" or "daveburke" users.
  • Sorts results by date descending.
  • Lists results in pages with 20 items per page and returns the first page.

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:

  • HasResults: Boolean value to check if search has had any result.
  • Posts: An ArrayList of Post objects which are returned by search.
  • Replies: An ArrayList of Post objects which are returned by search but they're replies indeed.
  • SearchDuration: Double value of duration of search process.
  • ThreadStarter: A Post object which is the starter thread of results.
  • TotalRecords: Integer value of total records in search results.

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()

{

    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;

 

    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

email it!bookmark it!digg it!

Original Post: CS Dev Guide: Search with APIs

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

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.

Advertise with us