Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/QueryConstructor.cs

87 lines
3.1 KiB
C#
Raw Normal View History

using Microsoft.Search.Interop;
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
{
public class QueryConstructor
{
private Settings _settings;
2020-05-11 20:47:45 +00:00
private const string SystemIndex = "SystemIndex";
public QueryConstructor(Settings settings)
{
_settings = settings;
}
2020-05-11 20:47:45 +00:00
public CSearchQueryHelper CreateBaseQuery()
{
2020-05-18 11:31:28 +00:00
var baseQuery = CreateQueryHelper();
2020-05-11 20:47:45 +00:00
// Set the number of results we want. Don't set this property if all results are needed.
baseQuery.QueryMaxResults = _settings.MaxResult;
// Set list of columns we want to display, getting the path presently
2020-05-19 08:46:42 +00:00
baseQuery.QuerySelectColumns = "System.FileName, System.ItemPathDisplay, System.ItemType";
2020-05-11 20:47:45 +00:00
2020-05-18 11:31:28 +00:00
// Filter based on file name
2020-05-11 20:47:45 +00:00
baseQuery.QueryContentProperties = "System.FileName";
// Set sorting order
//baseQuery.QuerySorting = "System.ItemType DESC";
return baseQuery;
}
2020-05-18 11:31:28 +00:00
internal CSearchQueryHelper CreateQueryHelper()
{
// This uses the Microsoft.Search.Interop assembly
var manager = new CSearchManager();
// SystemIndex catalog is the default catalog in Windows
var catalogManager = manager.GetCatalog(SystemIndex);
// Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer
var queryHelper = catalogManager.GetQueryHelper();
return queryHelper;
}
///<summary>
2020-05-18 10:13:31 +00:00
/// Set the required WHERE clause restriction to search on the first level of a specified directory.
///</summary>
public string QueryWhereRestrictionsForTopLevelDirectorySearch(string path)
{
return $"directory='file:{path}'";
}
2020-05-18 10:13:31 +00:00
///<summary>
/// Search will be performed on all folders and files on the first level of a specified directory.
///</summary>
public string QueryForTopLevelDirectorySearch(string folderPath)
{
string query = "SELECT TOP " + _settings.MaxResult + $" {CreateBaseQuery().QuerySelectColumns} FROM {SystemIndex} WHERE ";
query += QueryWhereRestrictionsForTopLevelDirectorySearch(folderPath);
return query;
}
///<summary>
/// Search will be performed on all folders and files based on user's search keywords.
///</summary>
public string QueryForAllFilesAndFolders(string userSearchString)
{
2020-05-18 11:31:28 +00:00
// Generate SQL from constructed parameters, converting the userSearchString from AQS->WHERE clause
return CreateBaseQuery().GenerateSQLFromUserQuery(userSearchString) + " AND " + QueryWhereRestrictionsForAllFilesAndFoldersSearch();
}
///<summary>
/// Set the required WHERE clause restriction to search for all files and folders.
///</summary>
public string QueryWhereRestrictionsForAllFilesAndFoldersSearch()
{
return $"scope='file:'";
}
}
}