Outlook IsInstantSearchEnabled属性异常致ci_phrasematch搜索失败问询
尝试用ci_phrasematch替代LIKE操作符加速Outlook高级搜索,已知ci_phrasematch仅在IsInstantSearchEnabled为true时可用。但遇到以下问题:
- Outlook已启动时,首次执行代码,
IsInstantSearchEnabled返回false; - 不关闭Outlook再次运行代码,该属性变为true,但此时
AdvancedSearch抛出异常:System.Runtime.InteropServices.COMException: 'Cannot use content indexer keywords in the restriction. Instant search is not enabled on this store.'
环境为Outlook 365搭配KOFF(Offline Kerio Outlook Connector)同步服务器,首次使用Microsoft.Office.Interop.Outlook,询问是否遗漏了关键步骤。
using Outlook = Microsoft.Office.Interop.Outlook; Type officeType = Type.GetTypeFromProgID("Outlook.Application"); if (officeType != null) //outlook is installed { emails = new DataTable(); emails.Columns.Add("subject", typeof(string)); emails.Columns.Add("from", typeof(string)); emails.Columns.Add("to", typeof(string)); emails.Columns.Add("date", typeof(DateTime)); emails.Columns.Add("body", typeof(string)); Outlook.Application outApp = null; // Check whether there is an Outlook process running. if (Process.GetProcessesByName("OUTLOOK").Count() > 0) { outApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application"); } else { outApp = new Outlook.Application(); Outlook.NameSpace nameSpace = outApp.GetNamespace("MAPI"); nameSpace.Logon("", "", Missing.Value, Missing.Value); nameSpace = null; } string scope = "'" + outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).FolderPath + "','" + outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).FolderPath + "'"; string email = "test@email.com"; string filter; string contactName = GetConatactName(email); if (outApp.Session.DefaultStore.IsInstantSearchEnabled) { if (contactName.Trim() == "") { filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')" + " OR (urn:schemas:httpmail:displayto ci_phrasematch '" + email + "' AND urn:schemas:httpmail:date > '8/1/2022')"; } else { filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')" + " OR ((urn:schemas:httpmail:displayto ci_phrasematch '" + email + "' OR urn:schemas:httpmail:displayto ci_phrasematch '" + contactName + "')" + " AND urn:schemas:httpmail:date > '8/1/2022')"; } } else { if (contactName.Trim() == "") { filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')" + " OR (urn:schemas:httpmail:displayto LIKE '%" + email + "%' AND urn:schemas:httpmail:date > '8/1/2022')"; } else { filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')" + " OR ((urn:schemas:httpmail:displayto LIKE '%" + email + "%' OR urn:schemas:httpmail:displayto LIKE '%" + contactName + "%')" + " AND urn:schemas:httpmail:date > '8/1/2022')"; } } outApp.AdvancedSearchComplete += OutApp_AdvancedSearchComplete; outApp.AdvancedSearch(scope, filter, true, "Search"); }
检查目标文件夹所属商店的即时搜索状态:当前仅检查了默认商店的
IsInstantSearchEnabled,但搜索范围包含的收件箱、已发送邮件可能属于KOFF对应的Kerio离线商店,而非默认商店。需要遍历搜索范围内的每个文件夹,检查其所属Store的IsInstantSearchEnabled属性,确保所有目标商店都启用了即时搜索。确认KOFF对即时搜索的支持性:Kerio的Offline Outlook Connector(KOFF)可能不完全兼容Outlook的即时搜索功能,即使
IsInstantSearchEnabled返回true,实际内容索引可能并未生效。需查阅Kerio官方文档,确认KOFF是否支持Outlook内容索引及ci_phrasematch操作符。处理即时搜索属性的延迟生效问题:首次运行时属性为false,再次运行变为true,可能是Outlook加载商店索引的延迟导致。此时即使属性显示启用,索引可能未完成初始化,无法使用
ci_phrasematch。可尝试添加延迟,或检查Store的IsInstantSearchReady属性(若可用),确认索引就绪后再使用索引相关操作符。优化搜索范围的构造方式:当前用
FolderPath拼接搜索范围存在格式风险,Outlook的AdvancedSearch更推荐使用文件夹的EntryID构造范围,避免路径格式问题导致的搜索异常。修改示例:Outlook.Folder inbox = outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; Outlook.Folder sentMail = outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder; string scope = $"'{inbox.EntryID}','{sentMail.EntryID}'";添加异常降级处理:即使
IsInstantSearchEnabled返回true,仍需捕获COM异常,在异常发生时自动降级为LIKE操作符,避免程序崩溃。
内容的提问来源于stack exchange,提问作者Cody

