Contact Us

In a world where users expect relevance amidst clutter, search is an integral component. A website's search feature should be much more than any search bar - whether it's e-commerce, a legal or travel domain. A website with a top-notch search is always appealing and enhances engagement with the user.

So what's the best way to make search more efficient? Let's take a look at some effective ways:

    Never use.GetItem() from search results

    Several times, when we need multiple properties from our search results, instead of returning only the properties we need, we return the entire Item object. What happens is this leads to a drastic reduction in performance. While returning the entire item object using GetItem() method, we make a database call using Sitecore API, and any read operation from the database is quite expensive if we compare it with any read operation from a text file.

    In such a case, the best way to implement this is to make use of base search object class, which Sitecore provides, i.e. SearchResultItem class present in Sitecore. ContentSearch.SearchTypes namespace. If any additional field is required, then we can make our own custom base search class, on top of SearchResultItem class provided by Sitecore (given below).

    img

    Make use of separate expression class

    It is often said, "Anyone can write a code that a machine can understand, but only good programmers write a code that humans can understand". This shows the importance of clean code. To make our code clean and enhance its reusability (as per our business needs), it is always advised to have a separate expression class overwriting the entire code.

    For example:

    Separate class for weightage expressions:

    img

    img

    Calling these expressions in our code while writing predicates conditions:

    img

    Do not index all fields:

    In a fresh Sitecore instance, IndexAllFields property has been set to true, due to which all fields are indexed. This is good in a way-everything we have on our template will automatically get indexed, while publishing or indexing.
    However, there are a few issues that we might come across due to indexing everything, therefore, unless it is essential, we must refrain from indexing all fields, and this property must be set to false.

    img

    Issues that crop up when indexing all fields:

    Performance: This can drastically reduce the performance of a Content Search operation. As our content grows, it can increase the index size in large solutions. Therefore, in large solutions, it is highly recommended to index only those fields that are required.

    1000 field limit: As we all know, an Azure search engine has a limit of 1000 fields per index, and any fresh Sitecore 9 instance generally creates around 900+ fields (by default) to support some internal search functionality like backend search, etc., therefore there is very less count left for our own fields. Hence, we should not index all the fields and ensure to maintain a field count of less than 1000 fields per index.

    Use.Select() to control what all fields are to be returned: Most of the time, we need only a few properties to be displayed from our search result. Still, we end up returning all the properties/fields from our search results. Consider an example of autosuggest, where we generally need a title field, a link field, and maybe a brief about the item. Still if we go all out and return all indexed fields, which we're converting to.ToList() and finally iterating them in our code. The best way of doing this is to use a Select statement and to query only for a few fields that we need.

    img

    Indexed vs. stored fields: Setting Indexed="true" makes the field searchable and more efficient.
    For example: If for any field Indexed="true", then we can search for any text using the below snippet:
    q= title_t:"james"
    Here we're searching for all the documents which have "James" in their title field.
    Setting Stored="true" means we can get this value from the search query and can then make use of this value for displaying it anywhere on our site.

    Use a copy field: There might be a scenario where we'd like to use some existing fields at different places in a different way.
    For example: If we refer to the below screenshots we'll notice that "personsearchname" field of type "text_general", which was already indexed in solr, is being used to create a new copy field, i.e. "personsearchnamengrm" of type "txt_ppl_global', to be used specifically for Global search with some additional analysers, to cater to some specific Global search requirements.

    Creating of copy field:

    img

    Assigning different types to both these fields:

    img

    Defining different analysers to these types:

    img

    img

    Use facets for group results: If we want to group our results based on some field or category, then facets can be a good option. Facets not only help in categorising the results, but also provides us with the document/results count per category.

    img

    Make use of custom indexes: Rather than having everything in one index, it is always advisable to have separate indexes for different sections of the website. We should also avoid using the default indexes provided by Sitecore. If we talk about a legal domain website, we can have different indexes for each of these: Biographies, Articles, Practices, and other sections of the website. This could help in increasing the performance of our search query, as it reduces the documents count within each index.

    All in all, using Sitecore Search effectively and efficiently is the key to enhancing user experience.

Need Help?