示例#1
0
        private static int DoIndex(IndexOptions opts)
        {
            using (var builder = IndexFactory.CreateBuilder(new PersistentIndexName(".", opts.DictionaryType, opts.FieldsType, opts.PostingType, opts.TextEncoding)))
            {
                builder.Start();

                var timer     = Stopwatch.StartNew();
                var documents = 0;
                foreach (var file in Directory.EnumerateFiles(opts.InputPath, opts.Filter, SearchOption.AllDirectories).Select(f => new FileInfo(f)))
                {
                    PrintConsole(ConsoleColor.Gray, $"{file.FullName}");
                    if (opts.InputType == "text")
                    {
                        builder.AddFile(
                            file.FullName,
                            "{filename:\"" + file.FullName + "\", size:\"" + file.Length + "\", created:\"" + file.CreationTime.ToString("o") + "\"}");
                    }
                    else if (opts.InputType == "name")
                    {
                        builder.AddText(
                            file.FullName,
                            "{filename:\"" + file.FullName + "\", size:\"" + file.Length + "\", created:\"" + file.CreationTime.ToString("o") + "\"}");
                    }
                    else
                    {
                        throw new Exception("Unsupported input type");
                    }
                    ++documents;
                }
                var stat = builder.StopAndWait();
                PrintConsole(ConsoleColor.White, $"Indexed documents: {documents}, terms: {stat.Terms}, occurrences: {stat.Occurrences}, time: {timer.Elapsed}");
            }

            return(0);
        }
示例#2
0
        public static IFullTextIndex AddToIndex(IIndexName indexName, string text)
        {
            using (var builder = IndexFactory.CreateBuilder(indexName))
            {
                builder.Start();
                builder.AddText(text, null);
                builder.StopAndWait();
            }

            return(IndexFactory.OpenIndex(indexName));
        }
示例#3
0
        public static IFullTextIndex PrepareIndexForSearch(IIndexName indexName)
        {
            using (var builder = IndexFactory.CreateBuilder(indexName))
            {
                builder.Start();
                builder.AddText("Hello World!", null);
                builder.AddText("Petro Petrolium Petrol", null);
                builder.AddText("This is test document for search unit tests", null);
                builder.AddText("This test document is used for search operators", null);
                builder.AddText("This full-text search only supports boolean operators: and, or", null);
                builder.AddText("Programming is very exciting. Programs can help. This is fantastic!!!", null);
                builder.StopAndWait();
            }

            return(IndexFactory.OpenIndex(indexName));
        }
示例#4
0
        static void Main(string[] args)
        {
            PrintConsole(ConsoleColor.Green, "PMS Full-Text Search (c) Petro Protsyk 2017-2018");
            if (args.Length < 1)
            {
                PrintHelp();
                return;
            }

            if (args[0] == "index")
            {
                var fieldsType = (args.Length > 3 && args[2] == "--fieldsType") ? args[3] : "List";
                using (var builder = IndexFactory.CreateBuilder(new PersistentIndexName(".", fieldsType)))
                {
                    builder.Start();

                    var timer     = Stopwatch.StartNew();
                    var documents = 0;
                    foreach (var file in Directory.EnumerateFiles(args[1], "*.txt", SearchOption.AllDirectories).Select(f => new FileInfo(f)))
                    {
                        PrintConsole(ConsoleColor.Gray, $"{file.FullName}");
                        builder.AddFile(
                            file.FullName,
                            "{filename:\"" + file.FullName + "\", size:\"" + file.Length + "\", created:\"" + file.CreationTime.ToString("o") + "\"}");
                        ++documents;
                    }
                    builder.StopAndWait();
                    PrintConsole(ConsoleColor.White, $"Indexed documents: {documents}, time: {timer.Elapsed}");
                }
            }
            else if (args[0] == "search")
            {
                var timer          = Stopwatch.StartNew();
                var documentsCount = 0;
                var matchesCount   = 0;
                using (var index = IndexFactory.OpenIndex(new PersistentIndexName(".")))
                {
                    using (var compiler = new FullTextQueryCompiler(index))
                    {
                        var searchQuery = compiler.Compile(args[1]);
                        var prevDoc     = Occurrence.NoId;
                        foreach (var match in searchQuery.AsEnumerable())
                        {
                            if (match.DocumentId != prevDoc)
                            {
                                if (prevDoc != Occurrence.NoId)
                                {
                                    PrintConsole(ConsoleColor.Gray, String.Empty);
                                }

                                PrintConsole(ConsoleColor.Gray, index.Fields.GetMetadata(match.DocumentId));
                                prevDoc = match.DocumentId;
                                documentsCount++;
                            }
                            ++matchesCount;
                            PrintConsole(ConsoleColor.Gray, $"{match} ");
                        }
                        if (prevDoc != Occurrence.NoId)
                        {
                            PrintConsole(ConsoleColor.Gray, String.Empty);
                        }
                    }
                }

                PrintConsole(ConsoleColor.White, $"Documents found: {documentsCount}, matches: {matchesCount}, time: {timer.Elapsed}");
            }
            else if (args[0] == "print")
            {
                var timer = Stopwatch.StartNew();
                var terms = 0;
                using (var index = IndexFactory.OpenIndex(new PersistentIndexName(".")))
                {
                    index.Visit(new PrintVisitor(index));
                    ++terms;
                }

                PrintConsole(ConsoleColor.White, $"Terms: {terms}, time: {timer.Elapsed}");
            }
            else if (args[0] == "lookup")
            {
                var timer      = Stopwatch.StartNew();
                var termsFound = 0;
                using (var index = IndexFactory.OpenIndex(new PersistentIndexName(".")))
                {
                    int tilda = args[1].IndexOf("~");
                    IEnumerable <DictionaryTerm> terms = null;
                    if (tilda == -1)
                    {
                        terms = index.GetTerms(args[1]);
                    }
                    else
                    {
                        terms = index.GetTerms(args[1].Substring(0, tilda), int.Parse(args[1].Substring(tilda + 1)));
                    }

                    foreach (var term in terms)
                    {
                        ++termsFound;
                        PrintConsole(ConsoleColor.Gray, term.Key);
                    }
                }
                PrintConsole(ConsoleColor.White, $"Terms found: {termsFound}, time: {timer.Elapsed}");
            }
        }