mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
remove the indexer plugin from constant search plugins (#8367)
This commit is contained in:
@@ -101,7 +101,7 @@ namespace Microsoft.Plugin.Indexer
|
||||
|
||||
// This uses the Microsoft.Search.Interop assembly
|
||||
var searchManager = new CSearchManager();
|
||||
var searchResultsList = _api.Search(searchQuery, searchManager, isFullQuery, maxCount: _settings.MaxSearchCount).ToList();
|
||||
var searchResultsList = _api.Search(searchQuery, searchManager, maxCount: _settings.MaxSearchCount).ToList();
|
||||
|
||||
// If the delayed execution query is not required (since the SQL query is fast) return empty results
|
||||
if (searchResultsList.Count == 0 && isFullQuery)
|
||||
@@ -179,7 +179,8 @@ namespace Microsoft.Plugin.Indexer
|
||||
// This function uses the Windows indexer and returns the list of results obtained. This version is required to implement the interface
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
return Query(query, false);
|
||||
// All plugins have to implement IPlugin interface. We return empty collection as we do not want any computation with constant search plugins.
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
|
||||
@@ -16,7 +16,6 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
|
||||
private readonly ISearch windowsIndexerSearch;
|
||||
|
||||
private const uint _fileAttributeHidden = 0x2;
|
||||
private static readonly Regex _likeRegex = new Regex(@"[^\s(]+\s+LIKE\s+'([^']|'')*'\s+OR\s+", RegexOptions.Compiled);
|
||||
|
||||
public WindowsSearchAPI(ISearch windowsIndexerSearch, bool displayHiddenFiles = false)
|
||||
{
|
||||
@@ -24,7 +23,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
|
||||
DisplayHiddenFiles = displayHiddenFiles;
|
||||
}
|
||||
|
||||
public List<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword, bool isFullQuery = false)
|
||||
public List<SearchResult> ExecuteQuery(ISearchQueryHelper queryHelper, string keyword)
|
||||
{
|
||||
if (queryHelper == null)
|
||||
{
|
||||
@@ -35,17 +34,6 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
|
||||
|
||||
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
|
||||
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword);
|
||||
var simplifiedQuery = SimplifyQuery(sqlQuery);
|
||||
|
||||
if (!isFullQuery)
|
||||
{
|
||||
sqlQuery = simplifiedQuery;
|
||||
}
|
||||
else if (simplifiedQuery.Equals(sqlQuery, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
// if a full query is requested but there is no difference between the queries, return empty results
|
||||
return results;
|
||||
}
|
||||
|
||||
// execute the command, which returns the results as an OleDBResults.
|
||||
List<OleDBResult> oleDBResults = windowsIndexerSearch.Query(queryHelper.ConnectionString, sqlQuery);
|
||||
@@ -141,7 +129,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
|
||||
queryHelper.QuerySorting = "System.DateModified DESC";
|
||||
}
|
||||
|
||||
public IEnumerable<SearchResult> Search(string keyword, ISearchManager manager, bool isFullQuery = false, string pattern = "*", int maxCount = 30)
|
||||
public IEnumerable<SearchResult> Search(string keyword, ISearchManager manager, string pattern = "*", int maxCount = 30)
|
||||
{
|
||||
if (manager == null)
|
||||
{
|
||||
@@ -151,12 +139,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
|
||||
ISearchQueryHelper queryHelper;
|
||||
InitQueryHelper(out queryHelper, manager, maxCount, DisplayHiddenFiles);
|
||||
ModifyQueryHelper(ref queryHelper, pattern);
|
||||
return ExecuteQuery(queryHelper, keyword, isFullQuery);
|
||||
}
|
||||
|
||||
public static string SimplifyQuery(string sqlQuery)
|
||||
{
|
||||
return _likeRegex.Replace(sqlQuery, string.Empty);
|
||||
return ExecuteQuery(queryHelper, keyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,90 +368,5 @@ namespace Wox.Test.Plugins
|
||||
// Act & Assert
|
||||
return driveDetection.DisplayWarning();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplifyQueryShouldRemoveLikeQueryWhenSQLQueryUsesLIKESyntax()
|
||||
{
|
||||
// Arrange
|
||||
string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (System.FileName LIKE 'abcd.%' OR CONTAINS(System.FileName,'\"abcd.*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Act
|
||||
var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery);
|
||||
|
||||
// Assert
|
||||
string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"abcd.*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Using InvariantCultureIgnoreCase since this relates to sql code in string form
|
||||
Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplifyQueryShouldReturnArgumentWhenSQLQueryDoesNotUseLIKESyntax()
|
||||
{
|
||||
// Arrange
|
||||
string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE CONTAINS(System.FileName,'\"abcd*\"',1033) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Act
|
||||
var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery);
|
||||
|
||||
// Assert
|
||||
// Using InvariantCultureIgnoreCase since this relates to sql code in string form
|
||||
Assert.IsTrue(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplifyQueryShouldRemoveAllOccurrencesOfLikeQueryWhenSQLQueryUsesLIKESyntaxMultipleTimes()
|
||||
{
|
||||
// Arrange
|
||||
string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\", \"System.FileExtension\" FROM \"SystemIndex\" WHERE (System.FileName LIKE 'ab.%' OR CONTAINS(System.FileName,'\"ab.*\"',1033)) AND (System.FileExtension LIKE '.cd%' OR CONTAINS(System.FileName,'\".cd*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Act
|
||||
var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery);
|
||||
|
||||
// Assert
|
||||
string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\", \"System.FileExtension\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"ab.*\"',1033)) AND (CONTAINS(System.FileName,'\".cd*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Using InvariantCultureIgnoreCase since this relates to sql code in string form
|
||||
Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplifyQueryShouldRemoveLikeQueryWhenSQLQueryUsesLIKESyntaxAndContainsEscapedSingleQuotationMarks()
|
||||
{
|
||||
// Arrange
|
||||
string sqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (System.FileName LIKE '''ab.cd''%' OR CONTAINS(System.FileName,'\"'ab.cd'*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Act
|
||||
var simplifiedSqlQuery = WindowsSearchAPI.SimplifyQuery(sqlQuery);
|
||||
|
||||
// Assert
|
||||
string expectedSqlQuery = "SELECT TOP 30 \"System.ItemUrl\", \"System.FileName\", \"System.FileAttributes\" FROM \"SystemIndex\" WHERE (CONTAINS(System.FileName,'\"'ab.cd'*\"',1033)) AND scope='file:' ORDER BY System.DateModified DESC";
|
||||
|
||||
// Using InvariantCultureIgnoreCase since this relates to sql code in string form
|
||||
Assert.IsFalse(simplifiedSqlQuery.Equals(sqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
Assert.IsTrue(simplifiedSqlQuery.Equals(expectedSqlQuery, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsSearchAPIShouldReturnEmptyResultsWhenIsFullQueryIsTrueAndTheQueryDoesNotRequireLIKESyntax()
|
||||
{
|
||||
// Arrange
|
||||
OleDBResult file1 = new OleDBResult(new List<object>() { "C:/test/path/file1.txt", DBNull.Value });
|
||||
OleDBResult file2 = new OleDBResult(new List<object>() { "C:/test/path/file2.txt", "file2.txt" });
|
||||
|
||||
List<OleDBResult> results = new List<OleDBResult>() { file1, file2 };
|
||||
var mock = new Mock<ISearch>();
|
||||
mock.Setup(x => x.Query(It.IsAny<string>(), It.IsAny<string>())).Returns(results);
|
||||
WindowsSearchAPI api = new WindowsSearchAPI(mock.Object, false);
|
||||
var searchManager = GetMockSearchManager();
|
||||
|
||||
// Act
|
||||
var windowsSearchAPIResults = api.Search("file", searchManager, true);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(!windowsSearchAPIResults.Any());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user