Created a helper class to use indexer and return search results

This commit is contained in:
Alekhya Reddy Kommuru 2020-03-16 14:37:28 -07:00
parent dacd13ee30
commit 8e45c41f20
4 changed files with 116 additions and 17 deletions

View file

@ -154,6 +154,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wox.Plugin.Folder", "src\mo
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Wox.Launcher", "src\modules\launcher\Wox.Launcher\Wox.Launcher.vcxproj", "{E364F67B-BB12-4E91-B639-355866EBCD8B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wox.Plugin.Indexer", "src\modules\launcher\Plugins\Wox.Plugin.Indexer\Wox.Plugin.Indexer.csproj", "{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -378,6 +380,14 @@ Global
{E364F67B-BB12-4E91-B639-355866EBCD8B}.Release|Any CPU.ActiveCfg = Release|x64
{E364F67B-BB12-4E91-B639-355866EBCD8B}.Release|x64.ActiveCfg = Release|x64
{E364F67B-BB12-4E91-B639-355866EBCD8B}.Release|x64.Build.0 = Release|x64
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Debug|x64.ActiveCfg = Debug|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Debug|x64.Build.0 = Debug|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Release|Any CPU.Build.0 = Release|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Release|x64.ActiveCfg = Release|Any CPU
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -419,6 +429,7 @@ Global
{C21BFF9C-2C99-4B5F-B7C9-A5E6DDDB37B0} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{787B8AA6-CA93-4C84-96FE-DF31110AD1C4} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{E364F67B-BB12-4E91-B639-355866EBCD8B} = {C140A3EF-6DBF-4084-9D4C-4EB5A99FEE68}
{63C3CEA8-51FE-472E-B97C-B58F8B17DD51} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}

View file

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using Microsoft.Search.Interop;
namespace Wox.Plugin.Indexer.SearchHelper
{
public class WindowsSearchAPI
{
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int offset = 0, int maxCount = 100)
{
// This uses the Microsoft.Search.Interop assembly
CSearchManager manager = new CSearchManager();
// SystemIndex catalog is the default catalog in Windows
ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
// Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer
ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
// Set the number of results we want. Don't set this property if all results are needed.
queryHelper.QueryMaxResults = maxCount;
// Set list of columns we want to display, getting the path presently
queryHelper.QuerySelectColumns = "System.ItemPathDisplay";
// Set additional query restriction
queryHelper.QueryWhereRestrictions = "AND scope='file:'";
// convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files.
if (pattern != "*")
{
pattern = pattern.Replace("*", "%");
pattern = pattern.Replace("?", "_");
if (pattern.Contains("%") || pattern.Contains("_"))
{
queryHelper.QueryWhereRestrictions += " AND System.FileName LIKE '" + pattern + "' ";
}
else
{
// if there are no wildcards we can use a contains which is much faster as it uses the index
queryHelper.QueryWhereRestrictions += " AND Contains(System.FileName, '" + pattern + "') ";
}
}
// Set sorting order
queryHelper.QuerySorting = "System.DateModified DESC";
// Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(keyword);
// --- Perform the query ---
// create an OleDbConnection object which connects to the indexer provider with the windows application
using (System.Data.OleDb.OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString))
{
// open the connection
conn.Open();
// now create an OleDB command object with the query we built above and the connection we just opened.
using (OleDbCommand command = new OleDbCommand(sqlQuery, conn))
{
// execute the command, which returns the results as an OleDbDataReader.
using (OleDbDataReader WDSResults = command.ExecuteReader())
{
while (WDSResults.Read())
{
// col 0 is our path in display format
Console.WriteLine("{0}", WDSResults.GetString(0));
var result = new SearchResult { Path = WDSResults.GetString(0) };
yield return result;
}
}
}
}
}
}
}

View file

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>63c3cea8-51fe-472e-b97c-b58f8b17dd51</ProjectGuid>
<ProjectGuid>{63C3CEA8-51FE-472E-B97C-B58F8B17DD51}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Wox.Plugin.Indexer</RootNamespace>
@ -31,24 +31,27 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Search.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f748985a6e9a7cb, processorArchitecture=MSIL">
<HintPath>..\..\..\..\..\packages\tlbimp-Microsoft.Search.Interop.1.0.0\lib\net45\Microsoft.Search.Interop.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SearchHelper\SearchResult.cs" />
<Compile Include="SearchHelper\WindowsSearchAPI.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="tlbimp-Microsoft.Search.Interop" version="1.0.0" targetFramework="net472" />
</packages>