fede_rosario
fede_rosario

Reputation: 105

Using Lucene to improve performance in Sitecore

I´m currently working with Sitecore, where we made a Create New Content part, which opens a pop up showing the 8 most used templates, with the number of uses.

The problem is, it takes too long when the number of templates is too high (currently, the highest one is over 11k).

Here is the code I´m using to get the 8 most used templates:

I get all items from the database.

var allItems = db.GetItem("/sitecore/content").Axes.GetDescendants();

And then I get the 8 most used.

var mostUsedTemplates = allItems.GroupBy(x => x.TemplateID)
                .Select(x => new { TemplateID = x.Key, Count = x.Count() })
                .OrderByDescending(x => x.Count).Take(8);

We have Lucene implemented, and I really don´t have idea how to use it.

I tried searching for ways to get all the templates, counting them, and then get the 8 most used, but I found nothing.

In short, I need to count all templates used to create the items in content, and recover the 8 with the highest count.

Any help would be greatly appreciated. Thanks.

Expanding on this: This is the config I´m currently making. I´m trying to include all templates, and be able to count them.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<search>
  <configuration>
    <indexes>
      <index id="usage_template_count" type="Sitecore.Search.Index, Sitecore.Kernel">
        <param desc="name">$(id)</param>
        <param desc="folder">usage_template_count</param>
        <Analyzer ref="search/analyzer" />
        <locations hint="list:AddCrawler">   

        For what I understand, here is what I specify what to index.
        From what I read, I know how to include some templates, or excludes others, but no idea how to include ALL.
        Also don´t know if I have to set up something in the config to be able to count the results.

        </locations>
      </index>
    </indexes>
  </configuration>
</search>
</sitecore>
</configuration>   

Thanks again!

Upvotes: 1

Views: 642

Answers (1)

nickwesselman
nickwesselman

Reputation: 6890

First off, never iterate or retrieve the whole content tree and expect it to perform. It's just not a reasonable expectation.

You could potentially do this in lucene, but it would require indexing the templates themselves and adding a field which includes a count of the instances of a template. (Look at scSearchContrib to make this easier.) However you would need to have a scheduled full rebuild of this index, as the template items will never get reindexed unless they themselves change.

The Links DB will likely get you much better performance, since references to templates should be included there. However you'd still need to loop through all your templates, and check the number of references to each.

With either solution, I'd definitely still recommend implementing a caching layer.

In the end though, why is it necessary to do this dynamically? Will the eight most used or most useful templates on your installation change that often? Why not just make a configuration element somewhere in the content tree for this, and update it periodically based on reports of template usage? You could use something like the Sitecore Powershell Console to run the report. If you really need to automate it, write a Sitecore Powershell script that does the query, then updates your configuration element automatically. Schedule the script to run every day.

Upvotes: 7

Related Questions