Get latest news using Sitecore AdvancedDatabaseCrawler Lucene index

This is the first of 2 follow-up posts on how to use the Advanced Database Crawler open source module for Sitecore. From Sitecore 6.5, Sitecore is deprecating the old Lucene index. This means that we have to redo our index work. Fortunately, Alex Shyba has created an open source module that makes indexing and retrieving easy.

This is article 2 of 3 articles:

Part 1 – Configuring the index: Using the Sitecore open source AdvancedDatabaseCrawler Lucene indexer
Part 2 – Simple search: Get latest news using Sitecore AdvancedDatabaseCrawler Lucene index
Part 3 – Multivalue search: Get items based on Metadata using Sitecore AdvancedDatabaseCrawler Lucene index

If you have not read the first article, you should read it, as it explains how to compile the module and how to set up an index that indexes your WEB database.

This example describes one of the most trivial tasks: How to retrieve a sorted list of items that is scattered over the entire website. For example, when you would like to list the latest news articles, it is fast and easy to use an index. This website (Personalestyrelsen, a Danish web site) uses this exact method to list the latest news on the front page.

This is the scenario: I have a news archive where all my news articles are stored:

An example of a news archive

An example of a news archive

Each news template contains a date field and a title field:

Sample News Article

Sample News Article

I would like to use the date field as my sort field, and then display the 3 latest news with the date and title.

My index is already set up so it indexes every field in my solution (read about the configuration of the index here) , so I only have to set up a search. And thanks to Alex Shybas AdvancedDatabaseCrawler, this is easy:

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.SharedSource.Searcher;
using Sitecore.SharedSource.Searcher.Parameters;

namespace PT.Prototype.Search
{
  public partial class _default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      DataBind();
    }

    public IEnumerable<Item> GetLatestNews(int numberOfNews)
    {
      SearchParam searchParam = new SearchParam();
      // The name of the database to search in
      searchParam.Database = "web";
      // The language to return
      searchParam.Language = "en";
      // The NewsItem template ID
      searchParam.TemplateIds = "{2983E106-CE47-40B5-8704-D299CA9BFF8F}";
      // The QueryRunner uses the name of the index as constructor.
      // I use the same name as the database name.
      QueryRunner runner = new QueryRunner("web");
      // Call the runner to get the items.
      // Parameters are:
      // - ISearchParam: The search to perform
      // - Sitecore.Search.QueryOccurance: The AND/OR/NOT clause for the search
      // - ShowAllVersions: Get all versions of an item
      // - SortField: The name of the field used as my sort
      // - Reverse: Return items in reverse order (newset first)
      // - Start: Which items to return from the search result
      // - End: Which items to return from the search result
      IEnumerable<SkinnyItem> items = runner.GetItems(searchParam, Sitecore.Search.QueryOccurance.Must, false, "date", true, 0, numberOfNews);
      return items.Select(item => Sitecore.Context.Database.GetItem(item.ItemID));
    }
  }
}

All I need to do now is to create an asp:Repeater and databind it to the function:

<%@ Register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>

<asp:Repeater ID="repNews" DataSource="<%# GetLatestNews(3) %>" runat="server">
  <HeaderTemplate>
    <ul>
  </HeaderTemplate>
  <FooterTemplate>
    </ul>
  </FooterTemplate>
  <ItemTemplate>
    <li>
      <sc:FieldRenderer ID="FieldRenderer1" FieldName="Date" runat="server" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
      <a href="<%# Sitecore.Links.LinkManager.GetItemUrl(Container.DataItem as Sitecore.Data.Items.Item) %>">
        <sc:FieldRenderer ID="FieldRenderer2" FieldName="Title" runat="server" Item="<%# Container.DataItem as Sitecore.Data.Items.Item %>" />
      </a>
    </li>
  </ItemTemplate>
</asp:Repeater>
Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6 and tagged , , , , . Bookmark the permalink.

3 Responses to Get latest news using Sitecore AdvancedDatabaseCrawler Lucene index

  1. Pingback: Using the Sitecore open source AdvancedDatabaseCrawler Lucene indexer « Brian Pedersen’s Sitecore and .NET Blog

  2. Pingback: Get items based on Metadata using Sitecore AdvancedDatabaseCrawler Lucene index « Brian Pedersen’s Sitecore and .NET Blog

  3. Thomas says:

    I’m trying to do a search of most recently updated items using the __Updated field. However, when I use the Shared Source IndexViewer, only _url, _name, and _group are populated. I’m using Alex Shyba’s Advanced Database Crawler and just got and built the latest from https://github.com/sitecorian/SitecoreSearchContrib. In my included search.config file, I have it (I think) indexing all fields: true.

    What am I missing here? When I actually do a search on contents of a field other than _url, _name, and _group, it looks like I am getting results. I’m not sure Index Viewer is working either then.

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s