Example #1
0
        /// <summary>
        ///  searches docs by query
        /// </summary>
        internal void searchDocs()
        {
            mutableQuery = new Dictionary <string, int>();
            string[] splitedQuery = queryText.Split(' ');
            string   mutableTerm;

            for (int i = 0; i < splitedQuery.Length; i++)
            {
                if (splitedQuery[i].Length > 2 && splitedQuery[i][0] == '%' && splitedQuery[i][splitedQuery[i].Length - 1] == '%')
                {
                    mutableTerm = splitedQuery[i].Substring(1, splitedQuery[i].Length - 2);
                    if (mutableQuery.ContainsKey(mutableTerm))
                    {
                        mutableQuery[mutableTerm] += 1;
                    }
                    else
                    {
                        mutableQuery[mutableTerm] = 1;
                    }
                    splitedQuery[i] = "";
                }
            }
            queryText             = string.Join(" ", splitedQuery);
            Parse.d_abNumTerms    = new SortedDictionary <string, Term>();
            Parse.d_cfTerms       = new SortedDictionary <string, Term>();
            Parse.d_gmTerms       = new SortedDictionary <string, Term>();
            Parse.d_nrTerms       = new SortedDictionary <string, Term>();
            Parse.d_szTerms       = new SortedDictionary <string, Term>();
            Parse.d_docs["Query"] = new Doc("Query", "");
            Parse.d_docs["Query"].d_TermsCount = new Dictionary <string, int>();
            Parse.getTermsFromString(" " + queryText + " ");
            d_queryTerms = new SortedDictionary <string, QueryTerm>();
            Thread[] a_Threads = new Thread[5];
            a_Threads[0] = new Thread(() => getReleventDocs(Parse.d_abNumTerms, _indexer.mainIndexList1, "abNumsPosting.txt"));
            a_Threads[1] = new Thread(() => getReleventDocs(Parse.d_cfTerms, _indexer.mainIndexList2, "cfPosting.txt"));
            a_Threads[2] = new Thread(() => getReleventDocs(Parse.d_gmTerms, _indexer.mainIndexList3, "gmPosting.txt"));
            a_Threads[3] = new Thread(() => getReleventDocs(Parse.d_nrTerms, _indexer.mainIndexList4, "nrPosting.txt"));
            a_Threads[4] = new Thread(() => getReleventDocs(Parse.d_szTerms, _indexer.mainIndexList5, "szPosting.txt"));
            foreach (Thread t in a_Threads)
            {
                t.Start();
            }
            foreach (Thread t in a_Threads)
            {
                t.Join();
            }
            //remove irrelevant dates and creates queryDocs list
            queryDocs = new Dictionary <string, QueryDoc>();
            List <QueryTerm> l_temp;
            DateTime         date;

            if (_kill)
            {
                return;
            }
            foreach (var stringQTermPair in d_queryTerms)
            {
                if (_kill)
                {
                    return;
                }
                foreach (var docNameLocationsPair in stringQTermPair.Value.term.d_locations)
                {
                    if (!queryDocs.ContainsKey(docNameLocationsPair.Key))
                    {
                        if (Parse.d_docs[docNameLocationsPair.Key].date.Length < 5)//no date
                        {
                            l_temp = new List <QueryTerm>();
                            l_temp.Add(stringQTermPair.Value);
                            queryDocs[docNameLocationsPair.Key] = new QueryDoc(docNameLocationsPair.Key, l_temp);
                            //Console.WriteLine(docNameLocationsPair.Key + " doc was added");
                        }
                        else
                        {
                            try
                            {
                                string sdate = Parse.d_docs[docNameLocationsPair.Key].date.Split(delimiterChars)[1].ToLower();
                                if (months.ContainsKey(sdate) && (toMonth == 0 || months[sdate] <= toMonth) && months[sdate] >= fromMonth)
                                {
                                    l_temp = new List <QueryTerm>();
                                    l_temp.Add(stringQTermPair.Value);
                                    queryDocs[docNameLocationsPair.Key] = new QueryDoc(docNameLocationsPair.Key, l_temp);
                                }
                                else if (!months.ContainsKey(sdate))
                                {
                                    date = Convert.ToDateTime(Parse.d_docs[docNameLocationsPair.Key].date);
                                    if ((toMonth == 0 || date.Month <= toMonth) && date.Month >= fromMonth)
                                    {
                                        l_temp = new List <QueryTerm>();
                                        l_temp.Add(stringQTermPair.Value);
                                        queryDocs[docNameLocationsPair.Key] = new QueryDoc(docNameLocationsPair.Key, l_temp);
                                        //Console.WriteLine(docNameLocationsPair.Key + " doc was added");
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                l_temp = new List <QueryTerm>();
                                l_temp.Add(stringQTermPair.Value);
                                queryDocs[docNameLocationsPair.Key] = new QueryDoc(docNameLocationsPair.Key, l_temp);
                            }
                        }
                    }
                    else
                    {
                        queryDocs[docNameLocationsPair.Key].queryTerm.Add(stringQTermPair.Value);
                        //Console.WriteLine(docNameLocationsPair.Key + " doc has a term added");
                    }
                }
            }

            /*
             * using (StreamReader sr = new StreamReader(_indexer.postingPath + "\\ans.txt"))
             * {
             *  string line  = sr.ReadLine();
             *  int j = 0;
             *  if (!queryDocs.ContainsKey(line))
             *  {
             *      j++;
             *      Console.WriteLine(j+") missing: " + line);
             *  }
             * } */

            result = ranker.StartRanking(queryDocs, Parse.d_docs);

            /*
             * using (StreamWriter sw = File.AppendText(_indexer.postingPath + "\\result.txt"))
             * {
             *  foreach (var item in result)
             *  {
             *      sw.WriteLine("11 0 " + item + " 0 42.38 mt");
             *  }
             * }
             */
            SearcherChanged(2, "");
        }