예제 #1
0
        public static JToken QuerySearch(NuGetSearcherManager searcherManager, string scheme, string q, bool countOnly, string projectType, string supportedFramework, bool includePrerelease, int skip, int take, bool includeExplanation, bool applyFilter)
        {
            IndexSearcher searcher = searcherManager.Get();

            try
            {
                Query   query = MakeQuery(q, searcherManager);
                TopDocs topDocs;

                if (applyFilter)
                {
                    Filter filter = searcherManager.GetFilter(includePrerelease, supportedFramework);

                    //TODO: uncomment these lines when we have an index that contains the appropriate @type field in every document
                    //Filter typeFilter = new CachingWrapperFilter(new TypeFilter("http://schema.nuget.org/schema#NuGetClassicPackage"));
                    //filter = new ChainedFilter(new Filter[] { filter, typeFilter }, ChainedFilter.Logic.AND);

                    topDocs = searcher.Search(query, filter, skip + take);
                }
                else
                {
                    topDocs = searcher.Search(query, skip + take);
                }

                return(MakeResult(searcher, scheme, topDocs, skip, take, searcherManager, includeExplanation, query));
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static async Task TargetFrameworks(IOwinContext context, NuGetSearcherManager searcherManager)
        {
            IndexSearcher searcher = searcherManager.Get();

            try
            {
                HashSet <string> targetFrameworks = new HashSet <string>();

                IndexReader reader = searcher.IndexReader;

                for (int i = 0; i < reader.MaxDoc; i++)
                {
                    Document document = reader[i];

                    Field[] frameworks = document.GetFields("TargetFramework");

                    foreach (Field framework in frameworks)
                    {
                        targetFrameworks.Add(framework.StringValue);
                    }
                }

                JArray result = new JArray();
                foreach (string targetFramework in targetFrameworks)
                {
                    result.Add(targetFramework);
                }

                await ServiceHelpers.WriteResponse(context, System.Net.HttpStatusCode.OK, result);
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
예제 #3
0
        static JToken AutoCompleteSearch(NuGetSearcherManager searcherManager, string q, string id, string supportedFramework, bool includePrerelease, int skip, int take, bool includeExplanation)
        {
            IndexSearcher searcher = searcherManager.Get();

            try
            {
                Filter filter = searcherManager.GetFilter(includePrerelease, supportedFramework);

                if (q != null)
                {
                    Query   query   = AutoCompleteMakeQuery(q, searcherManager);
                    TopDocs topDocs = searcher.Search(query, filter, skip + take);
                    return(AutoCompleteMakeResult(searcher, topDocs, skip, take, searcherManager, includeExplanation, query));
                }
                else
                {
                    Query   query   = new TermQuery(new Term("Id", id.ToLowerInvariant()));
                    TopDocs topDocs = searcher.Search(query, filter, 1);
                    return(AutoCompleteMakeVersionResult(searcherManager, includePrerelease, topDocs));
                }
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
예제 #4
0
        static JToken FindSearch(NuGetSearcherManager searcherManager, string id, string scheme)
        {
            if (id == null)
            {
                return(null);
            }

            IndexSearcher searcher = searcherManager.Get();

            try
            {
                string  analyzedId = id.ToLowerInvariant();
                Query   query      = new TermQuery(new Term("Id", analyzedId));
                TopDocs topDocs    = searcher.Search(query, 1);

                if (topDocs.TotalHits > 0)
                {
                    Uri     registrationBaseAddress = searcherManager.RegistrationBaseAddress[scheme];
                    JObject obj = new JObject();
                    obj["registration"] = new Uri(registrationBaseAddress, string.Format("{0}/index.json", id.ToLowerInvariant())).AbsoluteUri;
                    return(obj);
                }
                else
                {
                    return(null);
                }
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
예제 #5
0
        public static void Search(JsonWriter jsonWriter,
                                  NuGetSearcherManager searcherManager,
                                  string scheme,
                                  string q,
                                  bool includePrerelease,
                                  NuGetVersion semVerLevel,
                                  int skip,
                                  int take,
                                  string feed,
                                  bool includeExplanation)
        {
            var searcher = searcherManager.Get();

            try
            {
                Query query = MakeSearchQuery(q, searcher);

                Filter filter = null;

                if (searcher.TryGetFilter(false, includePrerelease, semVerLevel, feed, out filter))
                {
                    // Filter before running the query (make the search set smaller)
                    query = new FilteredQuery(query, filter);
                }

                TopDocs topDocs = searcher.Search(query, skip + take);

                ResponseFormatter.WriteSearchResult(jsonWriter, searcher, scheme, topDocs, skip, take, includePrerelease, includeExplanation, semVerLevel, query);
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
예제 #6
0
        public static void AutoComplete(
            JsonWriter jsonWriter,
            NuGetSearcherManager searcherManager,
            string q,
            string id,
            bool includePrerelease,
            NuGetVersion semVerLevel,
            int skip,
            int take,
            bool includeExplanation)
        {
            var searcher = searcherManager.Get();

            try
            {
                Filter filter = null;

                if (q != null)
                {
                    Query query = MakeAutoCompleteQuery(q,
                                                        searcher.DocIdMapping,
                                                        searcher.Downloads,
                                                        searcher.Rankings,
                                                        searcher.QueryBoostingContext);

                    if (searcher.TryGetFilter(false, includePrerelease, semVerLevel, null, out filter))
                    {
                        // Filter before running the query (make the search set smaller)
                        query = new FilteredQuery(query, filter);
                    }

                    TopDocs topDocs = searcher.Search(query, skip + take);
                    ResponseFormatter.WriteAutoCompleteResult(jsonWriter, searcher, topDocs, skip, take, includeExplanation, query);
                }
                else
                {
                    Query query = MakeAutoCompleteVersionQuery(id);

                    if (searcher.TryGetFilter(false, includePrerelease, semVerLevel, null, out filter))
                    {
                        // Filter before running the query (make the search set smaller)
                        query = new FilteredQuery(query, filter);
                    }

                    TopDocs topDocs = searcher.Search(query, 1);
                    ResponseFormatter.WriteAutoCompleteVersionResult(jsonWriter, searcher, includePrerelease, semVerLevel, topDocs);
                }
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static void Stats(JsonWriter jsonWriter, NuGetSearcherManager searcherManager)
        {
            var searcher = searcherManager.Get();

            try
            {
                ResponseFormatter.WriteStatsResult(jsonWriter, searcher);
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
예제 #8
0
        public static JToken AutoComplete(IOwinContext context, NuGetSearcherManager searcherManager)
        {
            IndexSearcher searcher = searcherManager.Get();

            try
            {
                int skip;
                if (!int.TryParse(context.Request.Query["skip"], out skip))
                {
                    skip = 0;
                }

                int take;
                if (!int.TryParse(context.Request.Query["take"], out take))
                {
                    take = 20;
                }

                bool includePrerelease;
                if (!bool.TryParse(context.Request.Query["prerelease"], out includePrerelease))
                {
                    includePrerelease = false;
                }

                bool includeExplanation = false;
                if (!bool.TryParse(context.Request.Query["explanation"], out includeExplanation))
                {
                    includeExplanation = false;
                }

                string supportedFramework = context.Request.Query["supportedFramework"];

                string q  = context.Request.Query["q"];
                string id = context.Request.Query["id"];

                if (q == null && id == null)
                {
                    q = string.Empty;
                }

                return(AutoCompleteSearch(searcherManager, q, id, supportedFramework, includePrerelease, skip, take, includeExplanation));
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }
        public static void Search(JsonWriter jsonWriter,
                                  NuGetSearcherManager searcherManager,
                                  string q,
                                  bool countOnly,
                                  bool includePrerelease,
                                  NuGetVersion semVerLevel,
                                  string sortBy,
                                  int skip,
                                  int take,
                                  string feed,
                                  bool ignoreFilter,
                                  bool luceneQuery)
        {
            if (jsonWriter == null)
            {
                throw new ArgumentNullException(nameof(jsonWriter));
            }

            if (searcherManager == null)
            {
                throw new ArgumentNullException(nameof(searcherManager));
            }

            var searcher = searcherManager.Get();

            try
            {
                // The old V2 search service would treat "id:" queries (~match) in the same way as it did "packageid:" (==match).
                // If "id:" is in the query, replace it.
                if (luceneQuery && !string.IsNullOrEmpty(q) && q.StartsWith("id:", StringComparison.OrdinalIgnoreCase))
                {
                    q = "packageid:" + q.Substring(3);
                }

                // Build the query
                Query query = NuGetQuery.MakeQuery(q, searcher.Owners);

                // Build filter
                bool includeUnlisted = ignoreFilter;
                includePrerelease = ignoreFilter || includePrerelease;
                feed = ignoreFilter ? null : feed;

                var combinedQuery = new BooleanQuery();
                combinedQuery.Add(query, Occur.SHOULD);

                // Add this clause to the query here so we still respect semVerLevel that is passed when ignoring filters.
                if (!SemVerHelpers.ShouldIncludeSemVer2Results(semVerLevel))
                {
                    combinedQuery.Add(
                        NuGetQuery.ConstructClauseQuery(
                            new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),
                            MetadataConstants.LuceneMetadata.SemVerLevelPropertyName,
                            new List <string> {
                        SemVerHelpers.SemVerLevelKeySemVer2
                    }),
                        Occur.MUST_NOT);
                }

                query = combinedQuery;

                Filter filter = null;
                if (!ignoreFilter && searcher.TryGetFilter(includeUnlisted, includePrerelease, semVerLevel, feed, out filter))
                {
                    // Filter before running the query (make the search set smaller)
                    query = new FilteredQuery(query, filter);
                }

                if (countOnly)
                {
                    DocumentCountImpl(jsonWriter, searcher, query);
                }
                else
                {
                    ListDocumentsImpl(jsonWriter, searcher, query, sortBy, skip, take, semVerLevel);
                }
            }
            finally
            {
                searcherManager.Release(searcher);
            }
        }