Exemple #1
0
        /// <summary>
        /// Used to fill the cache with an array of
        /// All keywords are stored lowercase
        /// </summary>
        /// <returns></returns>
        public async Task <List <KeyValuePair <string, int> > > Inflate()
        {
            if (_cache.TryGetValue(nameof(SearchSuggestionsService), out _))
            {
                return(new Dictionary <string, int>().ToList());
            }

            var allFilesList = new List <KeyValuePair <string, int> >();

            try
            {
                allFilesList = await _context.FileIndex.GroupBy(i => i.Tags)
                                                           // ReSharper disable once UseMethodAny.1
                               .Where(x => x.Count() >= 1) // .ANY is not supported by EF Core
                               .TagWith("Inflate SearchSuggestionsService")
                               .Select(val => new KeyValuePair <string, int>(val.Key, val.Count())).ToListAsync();
            }
            catch (Exception exception)
            {
                if (!exception.Message.Contains("Unknown column"))
                {
                    _logger.LogError(exception, "mysql search suggest exception catch-ed");
                }
                return(allFilesList);
            }

            var suggestions = new Dictionary <string, int>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var tag in allFilesList)
            {
                if (string.IsNullOrEmpty(tag.Key))
                {
                    continue;
                }

                var keywordsHashSet = HashSetHelper.StringToHashSet(tag.Key.Trim());

                foreach (var keyword in keywordsHashSet)
                {
                    if (suggestions.ContainsKey(keyword))
                    {
                        suggestions[keyword] += tag.Value;
                    }
                    else
                    {
                        suggestions.Add(keyword, tag.Value);
                    }
                }
            }

            var suggestionsFiltered = suggestions
                                      .Where(p => p.Value >= 10)
                                      .OrderByDescending(p => p.Value)
                                      .ToList();

            _cache.Set(nameof(SearchSuggestionsService), suggestionsFiltered,
                       new TimeSpan(100, 0, 0));

            return(suggestionsFiltered);
        }
Exemple #2
0
        public void HashSetHelperTest_HashSetSingleComma()
        {
            // testing with double commas those are not supported by the c# exif read tool
            var result        = HashSetHelper.StringToHashSet("test,test1");
            var expetedresult = HashSetHelper.HashSetToString(result);

            Assert.AreEqual("test, test1", expetedresult);
        }
Exemple #3
0
        public void HashSetHelperTest_HashSetDuplicateContent()
        {
            // hashsets does not support duplicates, but test the comma implementation
            var result        = HashSetHelper.StringToHashSet("test, test");
            var expetedresult = HashSetHelper.HashSetToString(result);

            Assert.AreEqual("test", expetedresult);
        }
Exemple #4
0
        public void HashSetHelperTest_DoubleSpaces()
        {
            var hashSetResult = HashSetHelper.StringToHashSet("test0,   test1 , test2,  test3");

            Assert.AreEqual(4, hashSetResult.Count);

            Assert.AreEqual("test0", hashSetResult.ToList()[0]);
            Assert.AreEqual("test1", hashSetResult.ToList()[1]);
            Assert.AreEqual("test2", hashSetResult.ToList()[2]);
            Assert.AreEqual("test3", hashSetResult.ToList()[3]);
        }