internal static void PrintResult(FullTextSearchResult result)
 {
     for (int i = 0; i < result.Hits.Length; i++)
     {
         Console.WriteLine(result.Hits[i].Rank + "\t" + ((SourceFile)result.Hits[i].Document).name);
     }
 }
Exemple #2
0
        public IFullTextSearchResult Search(ISearch search, int currentPage = 1)
        {
            _search = search;
            SetDefaults();

            _searchProperties = SetSearchProperties();
            _currentPage      = currentPage;

            var result  = new FullTextSearchResult();
            var results = GetResults();

            result.CurrentPage  = currentPage;
            result.TotalPages   = search.PageLength > 0 && results.TotalItemCount > 0 ? (int)Math.Ceiling((decimal)results.TotalItemCount / (decimal)search.PageLength) : 1;
            result.TotalResults = results.TotalItemCount;
            result.Results      = (search.PageLength > 0 ? results.Skip(search.PageLength * (currentPage - 1)) : results).Select(x => GetFullTextSearchResultItem(x));

            return(result);
        }
    void SearchDatabase()
    {
        ReadOnlyTransaction t = db.StartReadWriteTransaction();

        DateTime start  = DateTime.Now;
        int      nBooks = nAuthors * nBooksPerAutor / nCoauthors;

        for (int i = 0, j = 0; i < nBooks; i++)
        {
            // find authors of the book
            Item[] authors = Enumerable.ToArray(t.Find(Predicate.Value("-author").In(Predicate.Value("title") == GenerateTitle(i))));
            Debug.Assert(authors.Length == nCoauthors);
            for (int k = 0; k < nCoauthors; k++)
            {
                Debug.Assert(authors[k].GetString("name") == GenerateName(j++ % nAuthors));
            }
        }
        for (int i = 0; i < nAuthors; i++)
        {
            // find book written by this author
            Item[] books = Enumerable.ToArray(t.Find(Predicate.Value("author").In(Predicate.Value("name") == GenerateName(i))));
            Debug.Assert(books.Length == nBooksPerAutor);
        }
        Console.WriteLine("Elapsed time for searching database " + (DateTime.Now - start));

        start = DateTime.Now;
        for (int i = 0, mask = 0; i < nBooks; i++, mask = ~mask)
        {
            // find book using full text search part of book title and ISDN
            FullTextSearchResult result = t.FullTextSearch(GenerateWord(i ^ mask) + " " + GenerateISBN(i), MAX_FULL_TEXT_SEARCH_RESULTS, MAX_FULL_TEXT_SEARCH_TIME);
            Debug.Assert(result.Hits.Length == 1);
        }
        for (int i = 0, mask = 0; i < nAuthors; i++, mask = ~mask)
        {
            // find authors using full text search of author's name
            FullTextSearchResult result = t.FullTextSearch(GenerateName(i ^ mask), MAX_FULL_TEXT_SEARCH_RESULTS, MAX_FULL_TEXT_SEARCH_TIME);
            Debug.Assert(result.Hits.Length == 1);
        }
        Console.WriteLine("Elapsed time for full text search " + (DateTime.Now - start));

        t.Commit();
    }
Exemple #4
0
        public void run()
        {
            int     i;
            string  tid     = "Thread" + id + ":";
            Storage storage = db.Storage;

            for (i = 0; i < nRecords; i++)
            {
                db.BeginTransaction();
                Record rec = new Record();
                rec.key   = tid + toStr(i) + ".Id";
                rec.value = "Thread" + id + " Key" + i;
                db.AddRecord(rec);
                db.CommitTransaction();
            }

            db.BeginTransaction();
            i = 0;
            foreach (Record rec in db.Select(typeof(Record), "key like '" + tid + "%'"))
            {
                Debug.Assert(rec.key.Equals(tid + toStr(i) + ".Id"));
                i += 1;
            }
            Debug.Assert(i == nRecords);

            FullTextSearchResult result = db.Search("Thread" + id, null, 10, 1000);

            Debug.Assert(result.Hits.Length == 10 && result.Estimation == nRecords);
            db.CommitTransaction();

            for (i = 0; i < nRecords; i++)
            {
                db.BeginTransaction();
                string key = tid + toStr(i) + ".ID";
                int    n   = 0;
                foreach (Record rec in db.Select(typeof(Record), "key='" + key + "'"))
                {
                    Debug.Assert(String.Compare(rec.key, key, true) == 0);
                    n += 1;
                }
                Debug.Assert(n == 1);

                result = db.Search("Thread" + id + " Key" + i, null, 10, 1000);
                Debug.Assert(result.Hits.Length == 1 && result.Estimation == 1 &&
                             String.Compare(((Record)result.Hits[0].Document).key, key, true) == 0);

                db.CommitTransaction();
            }

            for (i = 0; i < nRecords; i++)
            {
                db.BeginTransaction();
                string key = tid + toStr(i) + ".id";
                int    n   = 0;
                foreach (Record rec in db.Select(typeof(Record), "key='" + key + "'", true))
                {
                    Debug.Assert(String.Compare(rec.key, key, true) == 0);
                    db.DeleteRecord(rec);
                    n += 1;
                    break;
                }
                Debug.Assert(n == 1);
                db.CommitTransaction();
            }
        }
    public static void Main(string[] args)
    {
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("searchengine.dbs", PAGE_POOL_SIZE);
        Workspace ws = (Workspace)db.Root;

        if (ws == null)
        {
            ws      = new Workspace(db);
            db.Root = ws;
        }
        if (args.Length != 0)
        {
            DateTime start  = DateTime.Now;
            int      nFiles = ws.files.Count;
            for (int i = 0; i < args.Length; i++)
            {
                addFiles(ws, args[i]);
            }
            db.Commit();
            Console.WriteLine((ws.files.Count - nFiles) + " files are imported to the workspace in "
                              + (DateTime.Now - start));
        }
        while (true)
        {
            switch (input("-------------------------------------\n" +
                          "Menu:\n" +
                          "1. Index files\n" +
                          "2. Search\n" +
                          "3. Statistic\n" +
                          "4. Exit\n\n>>"))
            {
            case "1":
            {
                DateTime start  = DateTime.Now;
                int      nFiles = ws.files.Count;
                addFiles(ws, input("Root directory: "));
                db.Commit();
                Console.WriteLine((ws.files.Count - nFiles) + " files are imported to the workspace in "
                                  + (DateTime.Now - start));
                break;
            }

            case "2":
            {
                DateTime             start  = DateTime.Now;
                FullTextSearchResult result = ws.index.Search(input("Query: "), LANGUAGE, MAX_RESULTS, SEARCH_TIME_LIMIT);
                for (int i = 0; i < result.Hits.Length; i++)
                {
                    Console.WriteLine(result.Hits[i].Rank + "\t" + ((TextFile)result.Hits[i].Document).path);
                }
                Console.WriteLine("Elapsed search time for " + result.Hits.Length + " matched resuts: " + (DateTime.Now - start));

                break;
            }

            case "3":
            {
                Console.WriteLine("Number of indexed documents: " + ws.index.NumberOfDocuments);
                Console.WriteLine("Number total number of words: " + ws.index.NumberOfWords);
                break;
            }

            case "4":
            {
                db.Close();
                return;
            }
            }
            skip("Press ENTER to continue...");
        }
    }
 public FullTextSearchResultEnumerator(FullTextSearchResult result)
 {
     this.result = result;
 }