private string GetSql(string aqs) { CSearchManager searchManager = new CSearchManager(); CSearchCatalogManager catalogManager = searchManager.GetCatalog("SystemIndex"); CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); return(queryHelper.GenerateSQLFromUserQuery(aqs)); }
static void Main(string[] args) { CSearchManager manager = new CSearchManager(); CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); string sql = queryHelper.GenerateSQLFromUserQuery("text kind:music"); Console.WriteLine(sql); }
static void PerformSearch(string libPath, string query) { string sqlQuery; CSearchManager srchMgr = null; CSearchCatalogManager srchCatMgr = null; CSearchQueryHelper queryHelper = null; try { srchMgr = new CSearchManager(); srchCatMgr = srchMgr.GetCatalog("SystemIndex"); queryHelper = srchCatMgr.GetQueryHelper(); sqlQuery = queryHelper.GenerateSQLFromUserQuery(query); } finally { if (queryHelper != null) { Marshal.FinalReleaseComObject(queryHelper); queryHelper = null; } if (srchCatMgr != null) { Marshal.FinalReleaseComObject(srchCatMgr); srchCatMgr = null; } if (srchMgr != null) { Marshal.FinalReleaseComObject(srchMgr); srchMgr = null; } } Console.Error.WriteLine(sqlQuery); Console.Error.WriteLine(); PerformQuery(libPath, sqlQuery); }
/// <summary> /// Performs a full-text search against the Windows Search Index, by a given file system <paramref name="path"/> /// and <paramref name="query"/>. This generates an SQL query that is passed to the Windows Search query processor. /// <para>If you know in advance the SQL query you want to pass, use <see cref="PerformQuery"/>.</para> /// </summary> /// <param name="path">A path in the local file system.</param> /// <param name="query">Any full-text search string or search operators accepted by the Windows Search query engine.</param> /// <param name="progress">Optionally provide an <see cref="IProgress{T}"/> object to view progress output.</param> public static List <SearchResult> PerformSearch(string path, string query, IProgress <string> progress = null) { string sqlQuery; CSearchManager srchMgr = null; CSearchCatalogManager srchCatMgr = null; CSearchQueryHelper queryHelper = null; try { srchMgr = new CSearchManager(); srchCatMgr = srchMgr.GetCatalog("SystemIndex"); queryHelper = srchCatMgr.GetQueryHelper(); sqlQuery = queryHelper.GenerateSQLFromUserQuery(query); } finally { if (queryHelper != null) { Marshal.FinalReleaseComObject(queryHelper); queryHelper = null; } if (srchCatMgr != null) { Marshal.FinalReleaseComObject(srchCatMgr); srchCatMgr = null; } if (srchMgr != null) { Marshal.FinalReleaseComObject(srchMgr); srchMgr = null; } } progress?.Report($"Full query: {sqlQuery}"); return(PerformQuery(path, sqlQuery, progress)); }
//strPath: e.g. c:/disk public List <CSearchResultItem> SearchFolder(string strContent, string strPath) { List <CSearchResultItem> items = new List <CSearchResultItem>(); if (strContent == null || strContent.Length == 0 || strPath == null || strPath.Length == 0) { return(items); } // Thie uses SearchAPI interop assembly CSearchManager manager = new CSearchManager(); // the SystemIndex catalog is the default catalog that windows uses CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); queryHelper.QueryContentLocale = 2052; queryHelper.QueryMaxResults = 100; queryHelper.QueryKeywordLocale = 2052; //queryHelper. // set the columns we want,只检索文件的名字和内容,不然属性太多了,会出现狗屁不通的结果。 queryHelper.QuerySelectColumns = "System.ItemPathDisplay,System.Search.Rank,System.ItemNameDisplay"; queryHelper.QueryWhereRestrictions = "AND scope='file:" + strPath + "'"; queryHelper.QuerySorting = "System.ItemPathDisplay ASC "; // queryHelper.s queryHelper.QueryContentProperties = "System.Search.Contents,System.ItemNameDisplay"; string sqlQuery = queryHelper.GenerateSQLFromUserQuery(strContent); // --- Perform the query --- // create an OleDbConnection object which connects to the indexer provider with the windows application System.Data.OleDb.OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString); try { // open it conn.Open(); // now create an OleDB command object with the query we built above and the connection we just opened. OleDbCommand command = new OleDbCommand(sqlQuery, conn); // execute the command, which returns the results as an OleDbDataReader. OleDbDataReader WDSResults = command.ExecuteReader(); while (WDSResults.Read()) { CSearchResultItem aResult = new CSearchResultItem(); aResult.FullPath = WDSResults.GetString(0); aResult.Rank = WDSResults.GetInt32(1); aResult.DispName = WDSResults.GetString(2); items.Add(aResult); } WDSResults.Close(); } catch (Exception e) { } finally { conn.Close(); } return(items); }