Improving Solr performance in Sitecore - Part 2

Posted by Roberto De Almeida - Sr. Software Engineer

<< Read Part 1

In my previous article, I talked about what are the best practices you need to keep in mind when building a new index and some points that you should avoid if you need a performance improvement. Here I'm going to share some code snippets that you can use to improve your Solr queries.

When we are working with databases, even if they are non-relational databases like Solr, it's highly recommended to have a class that defines your indexed document. In the example below, we have an index named Offices, here is the model definition:

Code snippet 1 - Solr

Once you have your model defined, now we can call Solr through the Sitecore server to retrieve all documents using a Solr query:

Code snippet 2 - Solr

Let's explore some points above.

  • First, we are getting the Solr context.
  • With the context created, we are calling the GetQueryable method, passing our model, and using the Where clause to refine which documents we want.
  • Then we are using the order by method to order the final documents found during the query execution.
  • Finally, with all necessary documents on the plate, we are calling the Sitecore server to get each item.

Have some attention to the following tips:

  • Only use the Sitecore server to get the items if this is extremely necessary. Remember, when you call the method GetItem, Sitecore is going to search that item through the context database. It's fine to run this query when we are taking a few items only but in a big context, this can decrease your server performance significantly.

    Let's say you have a market store, and you created a search component for a specific context on your application. This page will be accessed by a thousand users, which means this component will be called a thousand times. Reducing the complexity of it will decrease the time response to the final users as well.
  • Do not traffic a thousand documents in your network. Even if a complex query will return a hundred items, you will not display all of them on the same page, pagination is the answer to avoid this situation, use the Solr properties: "rows" and "start".
  • Solr is a powerful engine, which means that we can run complex queries too. Look at the example below:
    Code snippet 3 - Solr
  • In the example above, we are using the same search term, trying to find it in different fields, using the predicate builder.

Do you remember in the last article that I spoke about how we can intercept the Sitecore thread to process an item before sending its values to Solr? Let’s understand how we can achieve this.

First, we need to tell Solr that we have a computed index right now:

Code snippet 4 - Solr

Look at the <fields> property. Once we have the defined field, let's take a look at the code:

Code snippet 5 - Solr

To overwrite the ComputeFieldValue method, you must inherit the IComputedIndexField interface. Before Sitecore triggers the Solr index action, it will pass through this statement.

In this article I covered some important steps to build and code a good application with the best practices related to the use of Solr systems.

I hope you enjoyed this content!


X
Cookies help us improve your website experience.
By using our website, you agree to our use of cookies.
Confirm