public static string GetSegments(PackageSearcherManager searcherManager)
        {
            searcherManager.MaybeReopen();

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                IndexReader indexReader = searcher.IndexReader;

                JArray segments = new JArray();
                foreach (ReadOnlySegmentReader segmentReader in indexReader.GetSequentialSubReaders())
                {
                    JObject segmentInfo = new JObject();
                    segmentInfo.Add("segment", segmentReader.SegmentName);
                    segmentInfo.Add("documents", segmentReader.NumDocs());
                    segments.Add(segmentInfo);
                }
                return(segments.ToString());
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static string KeyRangeQuery(PackageSearcherManager searcherManager, int minKey, int maxKey)
        {
            //  for range queries we always want the IndexReader to be absolutely up to date

            searcherManager.MaybeReopen();

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                NumericRangeQuery <int> numericRangeQuery = NumericRangeQuery.NewIntRange("Key", minKey, maxKey, true, true);

                List <DocumentKey> pairs = new List <DocumentKey>();
                searcher.Search(numericRangeQuery, new KeyCollector(pairs));

                // Group by key
                IEnumerable <IGrouping <int, DocumentKey> > groups = pairs.GroupBy(p => p.PackageKey);

                // De-duplicate
                IEnumerable <DocumentKey> deduped = groups.Select(g => g.First());

                JObject keys = new JObject();
                keys.Add(deduped.Select(p => new JProperty(p.PackageKey.ToString(), p.Checksum)));
                return(keys.ToString());
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static string Analyze(PackageSearcherManager searcherManager)
        {
            searcherManager.MaybeReopen();

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                IndexReader indexReader = searcher.IndexReader;

                JObject report = new JObject();

                report.Add("NumDocs", indexReader.NumDocs());
                report.Add("SearcherManagerIdentity", searcherManager.Id.ToString());

                AzureDirectory azDir = indexReader.Directory() as AzureDirectory;
                if (azDir != null)
                {
                    report.Add("Index", azDir.BlobContainer.Name);
                }
                else
                {
                    SimpleFSDirectory fsDir = indexReader.Directory() as SimpleFSDirectory;
                    if (fsDir != null)
                    {
                        report.Add("Index", fsDir.Directory.Name);
                    }
                }

                report.Add("RankingsUpdated", searcherManager.RankingsUpdatedUtc);
                report.Add("DownloadCountsUpdated", searcherManager.DownloadCountsUpdatedUtc);

                if (indexReader.CommitUserData != null)
                {
                    JObject commitUserdata = new JObject();
                    foreach (KeyValuePair <string, string> userData in indexReader.CommitUserData)
                    {
                        commitUserdata.Add(userData.Key, userData.Value);
                    }
                    report.Add("CommitUserData", commitUserdata);
                }

                // Moved segments to their own command since they can take a while to calculate in Azure

                return(report.ToString());
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        // Doesn't return JSON because consumers will want to make monitoring decisions based on this data as well as saving it/returning it from APIs
        public static IndexConsistencyReport GetIndexConsistency(PackageSearcherManager searcherManager, int databasePackageCount)
        {
            searcherManager.MaybeReopen();

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                IndexReader indexReader = searcher.IndexReader;

                // Get the number of documents
                int numDocs = indexReader.NumDocs();

                // Build the report
                return(new IndexConsistencyReport(numDocs, databasePackageCount));
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static string GetDistinctStoredFieldNames(PackageSearcherManager searcherManager)
        {
            searcherManager.MaybeReopen();

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                IndexReader indexReader = searcher.IndexReader;

                HashSet <string> distinctFieldNames = new HashSet <string>();

                for (int i = 0; i < indexReader.MaxDoc; i++)
                {
                    if (!indexReader.IsDeleted(i))
                    {
                        Document           document = indexReader.Document(i);
                        IList <IFieldable> fields   = document.GetFields();
                        foreach (IFieldable field in fields)
                        {
                            distinctFieldNames.Add(field.Name);
                        }
                    }
                }

                JArray array = new JArray();
                foreach (string fieldName in distinctFieldNames)
                {
                    array.Add(fieldName);
                }

                return(array.ToString());
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static string Search(PackageSearcherManager searcherManager, Query q, bool countOnly, string projectType, bool includePrerelease, string feed, string sortBy, int skip, int take, bool includeExplanation, bool ignoreFilter)
        {
            IndexSearcher searcher;

            try
            {
                if ((DateTime.UtcNow - WarmTimeStampUtc) > TimeSpan.FromMinutes(1))
                {
                    WarmTimeStampUtc = DateTime.UtcNow;

                    // Re-open on a background thread. We can safely continue to use the old reader while this happens.
                    Task.Factory
                    .StartNew(() => searcherManager.MaybeReopen())
                    .ContinueWith(t =>
                    {
                        // Log and suppress the exception to prevent taking down the whole process
                        if (t.IsFaulted)
                        {
                            Trace.WriteLine("Exception reopening searcher: {0}", t.Exception.ToString());

                            // Return a completed task indicating everything is A-OK :)
                            return(Task.FromResult(0));
                        }
                        return(t);
                    });
                }

                // Get the current reader. If a re-open is in progress but not yet complete, this will return the current reader.
                searcher = searcherManager.Get();
            }
            catch (Exception e)
            {
                throw new CorruptIndexException("Exception on (re)opening", e);
            }

            try
            {
                // ignoreFilter = true, don't filter by framework, feed or latest(stable)
                Filter filter = null;
                if (!ignoreFilter)
                {
                    // So if false, set up the filter and adjust the query for the framework if needed
                    filter = GetFilter(feed);
                }

                if (countOnly)
                {
                    return(DocumentCountImpl(searcher, q, filter));
                }
                else
                {
                    IDictionary <string, int> rankings = searcherManager.GetRankings(projectType);

                    return(ListDocumentsImpl(searcher, q, rankings, filter, sortBy, skip, take, includeExplanation, searcherManager));
                }
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }