Shazwazza / Examine

A .NET indexing and search engine powered by Lucene.Net
https://shazwazza.github.io/Examine/
373 stars 123 forks source link

TotalItemCount property performance #324

Open Sven883 opened 1 year ago

Sven883 commented 1 year ago

Hello!

We're using Umbraco v8.14.1 with Examine dll v1.2.2.0 & Lucene v3.0.3. We're also using this package for our search functionality of the website: https://github.com/skttl/umbraco-fulltextsearch8 I've been debugging for a while using the Umbraco mini profiler and I've noticed one line of code slowing down the whole code by a lot.

First we get the results, no issues there with a maxResults of 12 var results = searcher.CreateQuery().NativeQuery(query.ToString()).Execute(_search.PageLength * _currentPage);

results type is 'Examine.ISearchResults'

The slow code: result.TotalResults = results.TotalItemCount;

Our indexCreator:

public class NewsIndexCreator : LuceneIndexCreator
    {
        private readonly IProfilingLogger _profilingLogger;
        private readonly ILocalizationService _localizationService;
        private readonly IPublicAccessService _publicAccessService;

        public NewsIndexCreator(IProfilingLogger profilingLogger,
            ILocalizationService localizationService,
            IPublicAccessService publicAccessService
        )
        {
            _profilingLogger = profilingLogger;
            _localizationService = localizationService;
            _publicAccessService = publicAccessService;
        }

        public override IEnumerable<IIndex> Create()
        {

            var fieldDefinitionCollection = new FieldDefinitionCollection();

            foreach(var language in _localizationService.GetAllLanguages())
            {
                fieldDefinitionCollection.AddOrUpdate(new FieldDefinition($"updateDate_{language.CultureInfo}_sort", FieldDefinitionTypes.DateTime));
                fieldDefinitionCollection.AddOrUpdate(new FieldDefinition($"createDate_{language.CultureInfo}_sort", FieldDefinitionTypes.DateTime));
                fieldDefinitionCollection.AddOrUpdate(new FieldDefinition($"sortDate_{language.CultureInfo}_sort", FieldDefinitionTypes.DateTime));
            }

            var index = new UmbracoContentIndex("NewsIndex",
                CreateFileSystemLuceneDirectory("NewsIndex"),
                fieldDefinitionCollection,
                new StandardAnalyzer(Version.LUCENE_30),
                _profilingLogger,
                _localizationService,
                new ContentValueSetValidator(true, false, _publicAccessService, includeItemTypes: new string[] { "newsItem" }));
            return new[] { index };

        }
    }

Any ideas how we could fix this problem? Thanks in advance and happy holidays!

Shazwazza commented 1 year ago

Hi, Examine will execute the Lucene query lazily one time when either TotalItemCount is resolved, or when the search results are enumerated. My gut feeling is that you are resolving TotalItemCount first which will seem to indicate the performance problem is reading that property, however what is most likely occurring is that it is executing the search at that time.

I'm not sure if you are doing paging correctly, here's an example: https://github.com/Shazwazza/Examine/blob/release/2.0/src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs#L2372