/// <summary>
        /// Parses an input string that contains Structured Query keywords (using Advanced Query Syntax
        /// or Natural Query Syntax) and produces a SearchCondition object.
        /// </summary>
        /// <param name="query">The query to be parsed</param>
        /// <param name="cultureInfo">The culture used to select the localized language for keywords.</param>
        /// <returns>Search condition resulting from the query</returns>
        /// <remarks>For more information on structured query syntax, visit http://msdn.microsoft.com/en-us/library/bb233500.aspx and
        /// http://www.microsoft.com/windows/products/winfamily/desktopsearch/technicalresources/advquery.mspx</remarks>
        public static SearchCondition ParseStructuredQuery(string query, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException(nameof(query));
            }

            IQueryParserManager nativeQueryParserManager = (IQueryParserManager) new QueryParserManagerCoClass();
            IQueryParser        queryParser   = null;
            IQuerySolution      querySolution = null;
            ICondition          result        = null;

            IEntity         mainType        = null;
            SearchCondition searchCondition = null;

            try
            {
                // First, try to create a new IQueryParser using IQueryParserManager
                Guid    guid = new Guid(ShellIIDGuid.IQueryParser);
                HResult hr   = nativeQueryParserManager.CreateLoadedParser(
                    "SystemIndex",
                    cultureInfo == null ? (ushort)0 : (ushort)cultureInfo.LCID,
                    ref guid,
                    out queryParser);

                if (!CoreErrorHelper.Succeeded(hr))
                {
                    throw new ShellException(hr);
                }

                if (queryParser != null)
                {
                    // If user specified natural query, set the option on the query parser
                    using (PropVariant optionValue = new PropVariant(true))
                    {
                        hr = queryParser.SetOption(StructuredQuerySingleOption.NaturalSyntax, optionValue);
                    }

                    if (!CoreErrorHelper.Succeeded(hr))
                    {
                        throw new ShellException(hr);
                    }

                    // Next, try to parse the query.
                    // Result would be IQuerySolution that we can use for getting the ICondition and other
                    // details about the parsed query.
                    hr = queryParser.Parse(query, null, out querySolution);

                    if (!CoreErrorHelper.Succeeded(hr))
                    {
                        throw new ShellException(hr);
                    }

                    if (querySolution != null)
                    {
                        // Lastly, try to get the ICondition from this parsed query
                        hr = querySolution.GetQuery(out result, out mainType);

                        if (!CoreErrorHelper.Succeeded(hr))
                        {
                            throw new ShellException(hr);
                        }
                    }
                }

                searchCondition = new SearchCondition(result);
                return(searchCondition);
            }
            catch
            {
                if (searchCondition != null)
                {
                    searchCondition.Dispose();
                }

                throw;
            }
            finally
            {
                if (nativeQueryParserManager != null)
                {
                    Marshal.ReleaseComObject(nativeQueryParserManager);
                }

                if (queryParser != null)
                {
                    Marshal.ReleaseComObject(queryParser);
                }

                if (querySolution != null)
                {
                    Marshal.ReleaseComObject(querySolution);
                }

                if (mainType != null)
                {
                    Marshal.ReleaseComObject(mainType);
                }
            }
        }
Exemplo n.º 2
0
        public static SearchCondition ParseStructuredQuery(string query, CultureInfo cultureInfo)
        {
            Contract.Requires <ArgumentException>(!String.IsNullOrEmpty(query));

            ICondition result = null;

            var            nativeQueryParserManager = (IQueryParserManager) new QueryParserManagerCoClass();
            IQueryParser   queryParser   = null;
            IQuerySolution querySolution = null;

            IEntity         mainType        = null;
            SearchCondition searchCondition = null;

            try
            {
                // IQueryParser作成
                var guid = new Guid(SearchIID.IQueryParser);
                var hr   = nativeQueryParserManager.CreateLoadedParser(
                    "SystemIndex",
                    cultureInfo == null ? (ushort)0 : (ushort)cultureInfo.LCID,
                    ref guid,
                    out queryParser);
                if (HRESULT.Failed(hr))
                {
                    throw ShellException.FromHRESULT(hr);
                }

                if (queryParser != null)
                {
                    var optionValue = PropVariant.FromObject(true);
                    try
                    {
                        hr = queryParser.SetOption(STRUCTURED_QUERY_SINGLE_OPTION.SQSO_NATURAL_SYNTAX, optionValue);
                        if (HRESULT.Failed(hr))
                        {
                            throw ShellException.FromHRESULT(hr);
                        }
                    }
                    finally
                    {
                        optionValue.Clear();
                    }

                    hr = queryParser.Parse(query, null, out querySolution);
                    if (HRESULT.Failed(hr))
                    {
                        throw ShellException.FromHRESULT(hr);
                    }

                    if (querySolution != null)
                    {
                        hr = querySolution.GetQuery(out result, out mainType);
                        if (HRESULT.Failed(hr))
                        {
                            throw ShellException.FromHRESULT(hr);
                        }
                    }
                }

                searchCondition = new SearchCondition(result);
            }
            catch
            {
                searchCondition?.Dispose();
                throw;
            }
            finally
            {
                Marshal.ReleaseComObject(nativeQueryParserManager);

                if (queryParser != null)
                {
                    Marshal.ReleaseComObject(queryParser);
                }

                if (querySolution != null)
                {
                    Marshal.ReleaseComObject(querySolution);
                }

                if (mainType != null)
                {
                    Marshal.ReleaseComObject(mainType);
                }
            }

            return(searchCondition);
        }