move index exclude method to IndexSearch class and add async

This commit is contained in:
Jeremy Wu 2021-03-16 21:33:40 +11:00
parent e73b2a05ce
commit 7aa1396821
2 changed files with 42 additions and 37 deletions

View file

@ -65,10 +65,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
if (!querySearch.IsLocationPathString() && !isEnvironmentVariablePath)
{
var filteredResults = FilterOutResultsFromWindowsIndexExclusionList(
await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
results.UnionWith(filteredResults);
results.UnionWith(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
return results.ToList();
}
@ -112,6 +109,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
return await IndexSearch.WindowsIndexSearchAsync(querySearchString,
queryConstructor.CreateQueryHelper().ConnectionString,
queryConstructor.QueryForFileContentSearch,
settings.IndexSearchExcludedSubdirectoryPaths,
query,
token).ConfigureAwait(false);
}
@ -147,6 +145,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
return await IndexSearch.WindowsIndexSearchAsync(querySearchString,
queryConstructor.CreateQueryHelper().ConnectionString,
queryConstructor.QueryForAllFilesAndFolders,
settings.IndexSearchExcludedSubdirectoryPaths,
query,
token).ConfigureAwait(false);
}
@ -158,41 +157,11 @@ namespace Flow.Launcher.Plugin.Explorer.Search
return await IndexSearch.WindowsIndexSearchAsync(path,
queryConstructor.CreateQueryHelper().ConnectionString,
queryConstructor.QueryForTopLevelDirectorySearch,
settings.IndexSearchExcludedSubdirectoryPaths,
query,
token).ConfigureAwait(false);
}
private HashSet<Result> FilterOutResultsFromWindowsIndexExclusionList(List<Result> results)
{
var indexExclusionList = settings.IndexSearchExcludedSubdirectoryPaths;
var indexExclusionListCount = indexExclusionList.Count;
if (indexExclusionListCount == 0)
return results.ToHashSet();
var filteredResults = new HashSet<Result>(PathEqualityComparator.Instance);
for (var index = 0; index < results.Count; index++)
{
var excludeResult = false;
for (var i = 0; i < indexExclusionListCount; i++)
{
if (results[index].SubTitle.StartsWith(indexExclusionList[i].Path, StringComparison.OrdinalIgnoreCase))
{
excludeResult = true;
break;
}
}
if (!excludeResult)
filteredResults.Add(results[index]);
}
return filteredResults;
}
private bool UseWindowsIndexForDirectorySearch(string locationPath)
{
var pathToDirectory = FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath);

View file

@ -1,4 +1,5 @@
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
using Microsoft.Search.Interop;
using System;
using System.Collections.Generic;
@ -83,7 +84,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
}
internal async static Task<List<Result>> WindowsIndexSearchAsync(string searchString, string connectionString,
Func<string, string> constructQuery, Query query,
Func<string, string> constructQuery,
List<AccessLink> exclusionList,
Query query,
CancellationToken token)
{
var regexMatch = Regex.Match(searchString, reservedStringPattern);
@ -92,8 +95,41 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
return new List<Result>();
var constructedQuery = constructQuery(searchString);
return await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token);
return await RemoveResultsInExclusionList(
await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token),
exclusionList);
}
private async static Task<List<Result>> RemoveResultsInExclusionList(List<Result> results, List<AccessLink> exclusionList)
{
var indexExclusionListCount = exclusionList.Count;
if (indexExclusionListCount == 0)
return results;
var filteredResults = new List<Result>();
await Task.Run(() =>
{
for (var index = 0; index < results.Count; index++)
{
var excludeResult = false;
for (var i = 0; i < indexExclusionListCount; i++)
{
if (results[index].SubTitle.StartsWith(exclusionList[i].Path, StringComparison.OrdinalIgnoreCase))
{
excludeResult = true;
break;
}
}
if (!excludeResult)
filteredResults.Add(results[index]);
}
});
return filteredResults;
}
internal static bool PathIsIndexed(string path)