Пример #1
0
        private string GetSql(string aqs)
        {
            CSearchManager        searchManager  = new CSearchManager();
            CSearchCatalogManager catalogManager = searchManager.GetCatalog("SystemIndex");
            CSearchQueryHelper    queryHelper    = catalogManager.GetQueryHelper();

            return(queryHelper.GenerateSQLFromUserQuery(aqs));
        }
Пример #2
0
        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);
        }
Пример #3
0
        public static void RemoveSearchFolder(string strPath)
        {
            if (strPath == null || strPath.Length == 0)
            {
                return;
            }

            CSearchManager           manager            = new CSearchManager();
            CSearchCatalogManager    catalogManager     = manager.GetCatalog("SystemIndex");
            CSearchCrawlScopeManager searchScopeManager = catalogManager.GetCrawlScopeManager();

            searchScopeManager.RevertToDefaultScopes();
            searchScopeManager.RevertToDefaultScopes();
            searchScopeManager.RemoveScopeRule("file:///" + strPath);
        }
Пример #4
0
        public void ReIndexFolder(string strPath)
        {
            if (strPath == null || strPath.Length == 0)
            {
                return;
            }

            String                   searchPath         = "file:///" + strPath.ToLower();
            CSearchManager           manager            = new CSearchManager();
            CSearchCatalogManager    catalogManager     = manager.GetCatalog("SystemIndex");
            CSearchCrawlScopeManager searchScopeManager = catalogManager.GetCrawlScopeManager();

            // create search root
            uint             rootCnt = 0;
            CEnumSearchRoots roots   = searchScopeManager.EnumerateRoots();
            bool             exist   = false;

            do
            {
                CSearchRoot aRoot = new CSearchRoot();

                roots.Next(1, out aRoot, ref rootCnt);
                if (rootCnt != 0)
                {
                    roots.Skip(1);
                    if (aRoot.RootURL == searchPath)
                    {
                        exist = true;
                    }
                }
            } while (rootCnt != 0);

            if (!exist)
            {
                CSearchRoot r = new CSearchRoot();
                r.RootURL = searchPath;
                searchScopeManager.AddRoot(r);
                searchScopeManager.SaveAll();
            }

            searchScopeManager.RevertToDefaultScopes();
            //if (searchScopeManager.IncludedInCrawlScope(searchPath) == 0)
            //{
            searchScopeManager.AddUserScopeRule(searchPath, 1, 1, 0);
            searchScopeManager.SaveAll();
            //}
            catalogManager.ReindexSearchRoot(searchPath);
        }
Пример #5
0
        // strPath: c:\folder\subfolder\
        public static void AddSearchFolder(string strPath)
        {
            if (strPath == null || strPath.Length == 0)
            {
                return;
            }

            CSearchManager           manager            = new CSearchManager();
            CSearchCatalogManager    catalogManager     = manager.GetCatalog("SystemIndex");
            CSearchCrawlScopeManager searchScopeManager = catalogManager.GetCrawlScopeManager();

            searchScopeManager.RevertToDefaultScopes();
            if (searchScopeManager.IncludedInCrawlScope("file:///" + strPath) == 0)
            {
                searchScopeManager.AddUserScopeRule("file:///" + strPath, 1, 1, 0);
                searchScopeManager.SaveAll();
            }
        }
Пример #6
0
        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);
        }
Пример #7
0
        /// <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));
        }
Пример #8
0
        //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);
        }