示例#1
0
        /// Executes the query.
        //  Preprocesses the query text entered by the user
        //  and queries the index.
        //  Calculates the total time to run the query
        //  and sets some text variables for later use.
        public int RunQuery(string text, bool preproc, out string qText)
        {
            // start timer...
            DateTime start = DateTime.Now;

            // get the query settings from the collection
            IRQueryParams queryParams = myCollection.GetQueryParams();

            string[] queryFields      = queryParams.Fields;
            float[]  queryFieldBoosts = queryParams.FieldBoosts;

            // build field boost dictionary
            IDictionary <string, float> boosts = new Dictionary <string, float>();

            for (int i = 0; i < queryFields.Length; i++)
            {
                boosts.Add(queryFields[i], queryFieldBoosts[i]);
            }

            // setup searcher, query and parser
            CreateSearcher();
            Query query;

            parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30,
                                               queryFields, analyzer, boosts);

            // preprocess query (if required)
            if (preproc == true)
            {
                query = PreprocessQuery(text, parser);
            }
            else
            {
                // no preprocessing
                query = parser.Parse(text);
            }

            // print query text to form
            qText = query.ToString();

            // execute the search
            searchResults = searcher.Search(query, maxResults);

            // end timer and calculate total time
            DateTime end      = DateTime.Now;
            TimeSpan duration = end - start;

            queryTime = duration.Seconds + (float)duration.Milliseconds / 1000;

            CleanUpSearcher();

            return(searchResults.TotalHits);
        }