예제 #1
0
        public DataSet Search(string searchString, bool ignoreCase, bool autoAndConsecutiveLiterals, List <string> indexNames, string resolverName, int offset, int limit, DataSet ds, string options)
        {
            IEnumerable <int>            ids     = new List <int>().AsEnumerable();
            List <Index <TKey, TValue> > indexes = new List <Index <TKey, TValue> >();

            if (indexNames == null || indexNames.Count == 0)
            {
                // add all that are currently loaded
                foreach (string key in _indexes.Keys)
                {
                    if (_indexes[key].Enabled && _indexes[key].Loaded && _indexes[key].IsConfigurationValid)
                    {
                        indexes.Add(_indexes[key]);
                    }
                }
            }
            else
            {
                foreach (string iname in indexNames)
                {
                    Index <TKey, TValue> idx = null;
                    if (_indexes.TryGetValue(iname.ToLower(), out idx))
                    {
                        ;
                        if (idx != null)
                        {
                            if (idx.Enabled && idx.Loaded && idx.IsConfigurationValid)
                            {
                                indexes.Add(idx);
                            }
                        }
                    }
                }
            }

            // since the caller can specify how many items they want back,
            // we enforce an absolute upper bound so one client can't choke our server
            // by passing a really high limit
            int serverMaxLimit = Toolkit.GetSetting("MaximumItemCount", 100001);

            if (limit > serverMaxLimit || limit == 0)
            {
                limit = serverMaxLimit;
            }

            ds = appendTablesToSearchDataSet(ds);

            var opts = SearchOptions.Parse(Toolkit.ParsePairs <string>(("" + options).ToLower()));

            //if (Debugger.IsAttached) {
            //    opts.Add("parseonly", "true");
            //}

            var cmdQueue = SearchCommand <TKey, TValue> .Parse(searchString, ignoreCase, autoAndConsecutiveLiterals, indexes, resolverName, ds, opts, this);

            if (!opts.ParseOnly)
            {
                ids = SearchCommand <TKey, TValue> .ExecuteAndResolve(cmdQueue, indexes, ds, opts);
            }

            IEnumerable <int> uniqueIDs = ids.Distinct().OrderBy(i => i);

            var dtQuery = ds.Tables["SearchQuery"];

            if (dtQuery != null)
            {
                dtQuery.Rows[0]["totalFound"] = uniqueIDs.Count();
            }

            uniqueIDs = uniqueIDs.Skip(offset).Take(limit);

            var dt = ds.Tables["SearchResult"];

            foreach (var id in uniqueIDs)
            {
                dt.Rows.Add(id);
            }

            return(ds);
        }