Ejemplo n.º 1
0
        /// <summary>
        /// Searches an index for the specified string.
        /// </summary>
        /// <param name="indexId">The index id.</param>
        /// <param name="wsId">The ws id.</param>
        /// <param name="text">The text.</param>
        /// <returns>The search results.</returns>
        public IEnumerable <T> Search(int indexId, int wsId, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(Enumerable.Empty <T>());
            }

            SortKeyIndex index = GetIndex(indexId, wsId);

            switch (m_type)
            {
            case SearchType.Exact:
            case SearchType.Prefix:
            {
                byte[] sortKey = m_sortKeySelector(wsId, text);
                var    lower   = new byte[text.Length * SortKeyFactor];
                Collator.GetSortKeyBound(sortKey, UColBoundMode.UCOL_BOUND_LOWER, ref lower);
                var upper = new byte[text.Length * SortKeyFactor];
                Collator.GetSortKeyBound(sortKey,
                                         m_type == SearchType.Exact
                                                                                                ? UColBoundMode.UCOL_BOUND_UPPER
                                                                                                : UColBoundMode.UCOL_BOUND_UPPER_LONG, ref upper);

                return(index.GetItems(lower, upper));
            }

            case SearchType.FullText:
                IEnumerable <T> results = null;
                string[]        tokens  = RemoveWhitespaceAndPunctTokens(m_tokenizer(wsId, text)).ToArray();
                for (int i = 0; i < tokens.Length; i++)
                {
                    byte[] sortKey = m_sortKeySelector(wsId, tokens[i]);
                    var    lower   = new byte[tokens[i].Length * SortKeyFactor];
                    Collator.GetSortKeyBound(sortKey, UColBoundMode.UCOL_BOUND_LOWER, ref lower);
                    var upper = new byte[tokens[i].Length * SortKeyFactor];
                    Collator.GetSortKeyBound(sortKey,
                                             i < tokens.Length - 1
                                                                                                ? UColBoundMode.UCOL_BOUND_UPPER
                                                                                                : UColBoundMode.UCOL_BOUND_UPPER_LONG, ref upper);
                    IEnumerable <T> items = index.GetItems(lower, upper);
                    results = results == null ? items : results.Intersect(items);
                }
                return(results);
            }

            return(Enumerable.Empty <T>());
        }