3

如何检查位置是否已编入索引?我发现以下代码可以索引 Windows 中的一个位置,该位置工作正常,但我想在将其编入索引之前检查它是否已编入索引。

Uri path = new Uri(location);
string indexingPath = path.AbsoluteUri;

CSearchManager csm = new CSearchManager();
CSearchCrawlScopeManager manager =  csm.GetCatalog("SystemIndex").GetCrawlScopeManager();

manager.AddUserScopeRule(indexingPath, 1, 1, 0);
manager.SaveAll();

伙计们,我找到了一种方法来检查该位置是否已通过使用 IncludedInCrawlScope 进行索引。

CSearchManager csm = new CSearchManager();
CSearchCrawlScopeManager manager = csm.GetCatalog("SystemIndex").GetCrawlScopeManager();

if (manager.IncludedInCrawlScope(indexingPath) == 0)
{
    manager.AddUserScopeRule(indexingPath, 1, 1, 0);
    manager.SaveAll();
}

但它只检查它是否已被添加用于索引,而不是索引是否完整。由于我将查询 SystemIndex,我需要确保该位置已被索引。

4

2 回答 2

0

我遇到了类似的需求,这就是我想出的。就我而言,我有某些文件扩展名最终会被发送到文档管理系统。

我有两种方法,一种使用 System.IO 来获取目录中包含列表扩展名的文件列表。

 public IEnumerable<string> DirectoryScan(string directory)
    {

        List<string> extensions = new List<string>
        {
        "docx","xlsx","pptx","docm","xlsm","pptm","dotx","xltx","xlw","potx","ppsx","ppsm","doc","xls","ppt","doct","xlt","xlm","pot","pps"
        };
        IEnumerable<string> myFiles =
            Directory.GetFiles(directory, "*", SearchOption.AllDirectories)
                .Where(s => extensions.Any(s.EndsWith))
                .ToList();
        return myFiles;
    }`

第二种方法使用windows索引搜索Microsoft.Search.Interop

 public IEnumerable<string> QueryWindowsDesktopSearch(string directory)
    {

        List<string> extensions = new List<string>
        { "docx","xlsx","pptx","docm","xlsm","pptm","dotx","xltx","xlw","potx","ppsx","ppsm","doc","xls","ppt","doct","xlt","xlm","pot","pps"};

        string userQuery = "*";
        Boolean fShowQuery = true;
        List<string> list = new List<string>();
        CSearchManager manager = new CSearchManager();
        CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
        CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
        queryHelper.QueryWhereRestrictions = string.Format("AND (\"SCOPE\" = 'file:{0}')", directory);

        if (extensions != null)
        {
            queryHelper.QueryWhereRestrictions += " AND Contains(System.ItemType,'";
            bool fFirst = true;
            foreach (string ext in extensions)
            {
                if (!fFirst)
                {
                    queryHelper.QueryWhereRestrictions += " OR ";
                }
                queryHelper.QueryWhereRestrictions += "\"" + ext + "\"";
                fFirst = false;
            }
            queryHelper.QueryWhereRestrictions += "') ";
        }

        string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery);

        using (OleDbConnection connection = new OleDbConnection(queryHelper.ConnectionString))
        {
            using (OleDbCommand command = new OleDbCommand(sqlQuery, connection))
            {
                connection.Open();
                OleDbDataReader dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    var file = dataReader.GetString(0);
                    if (file != null)
                    {
                        list.Add(file.Replace("file:", ""));
                    }
                }
            }
        }
        return list;
    }

我从另一个方法调用这两个方法,该方法获取两个结果并比较它们并返回一个布尔值,指示它们是否两个列表匹配。如果它们不匹配,则该文件夹没有被完全索引。

如果您在尚未编制索引的文件夹上调用 QueryWindowsDesktopSearch,它将返回零个文件。您可以将其用作该文件夹不在索引中的指示,因为该文件可能已添加到索引中,但文件索引已停止。

你可以通过调用这样的东西来检查状态

 CSearchManager manager = new CSearchManager();
 CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
 _CatalogPausedReason pReason;
 _CatalogStatus pStatus;
 catalogManager.GetCatalogStatus(out pStatus, out pReason);

这可能会返回类似 pStatus = CATALOG_STATUS_PAUSED 和 pReason = CATALOG_PAUSED_REASON_USER_ACTIVE

你会知道索引没有运行。您可以做的另一件事是调用以下命令

int incrementalCount, notificationQueue, highPriorityQueue;
catalogManager.NumberOfItemsToIndex(out incrementalCount, out notificationQueue, out highPriorityQueue);

这将返回 plIncrementalCount 值,该值将列出整个 SystemIndex 已排队等待索引的文件数。

于 2015-03-27T22:18:23.250 回答
-1

从文档管理系统检查此实现:

https://code.google.com/p/olakedms/source/browse/SearchEngine/CSearchDAL.cs?r=171

于 2014-05-26T12:45:28.723 回答