Example #1
0
        public static async Task CreateIndex(string baseIndexPath, Func<IDocumentationService> serviceFactory)
        {
            using (var service = serviceFactory())
            {
                var analyzer = new NuDocAnalyzer();
                foreach (var proj in await service.GetProjects())
                {
                    var dirPath = IO.Path.Combine(baseIndexPath, proj.Name);

                    if (proj.Indexed && IO.Directory.Exists(dirPath))
                        continue;

                    var directory = FSDirectory.Open(IO.Path.Combine(baseIndexPath, proj.Name));
                    var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

                    foreach (var asm in await service.GetAssemblies(proj))
                    {
                        writer.AddDocument(CreateDocument(asm));
                        foreach (var ns in await service.GetNamespaces(asm))
                        {
                            writer.AddDocument(CreateDocument(ns));
                            foreach (var type in await service.GetTypes(ns))
                            {
                                writer.AddDocument(CreateDocument(type));
                                foreach (var member in await service.GetMembers(type))
                                {
                                    writer.AddDocument(CreateDocument(member));
                                }
                            }
                        }
                    }

#if !DEBUG
                    proj.Indexed = true;
#endif
                    await service.UpdateProject(proj);
                    writer.Optimize();
                    writer.Dispose();
                }
            }
        }
Example #2
0
        public static IEnumerable<string> Search(string path, string query)
        {
            if (!IO.Directory.Exists(path))
                throw new IO.DirectoryNotFoundException("Directory not found");

            var analyzer = new NuDocAnalyzer();
            var directory = FSDirectory.Open(path);
            var searcher = new IndexSearcher(directory);

            QueryParser title = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", analyzer);
            QueryParser summary = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Summary", analyzer);
            QueryParser details = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Details", analyzer);

            title.FuzzyMinSim = 0.5f;
            title.FuzzyPrefixLength = 1;

            var tt = title.Parse(query);

            var tq = title.Parse(query + "*");
            var sq = summary.Parse(query);
            var dq = details.Parse(query);

            dq.Boost = 0.5f;

            BooleanQuery bq = new BooleanQuery();
            bq.Add(tq, Occur.SHOULD);
            bq.Add(sq, Occur.SHOULD);
            bq.Add(dq, Occur.SHOULD);

            var hits = searcher.Search(tt, 15).ScoreDocs.Select(d => d.Doc)
                .Union(searcher.Search(bq, 15).ScoreDocs.Select(d => d.Doc))
                .Distinct()
                .Take(15);
            
            foreach (var doc in hits)
            {
                Document d = searcher.Doc(doc);
                yield return d.GetField("Id").StringValue;
            }
        }