Exemplo n.º 1
0
        public long GetTotalDocumentsInIndex()
        {
            // okay, open reader or searcher and get max docs
            DirectoryInfo searchDirectory = null;
            bool          hasIndexFiles   = false;

            switch (index.IndexStructure)
            {
            case IndexType.SingleIndex:
                IIndex singleIndex = (IIndex)index;
                searchDirectory = singleIndex.IndexDirectory;
                hasIndexFiles   = singleIndex.HasIndexFiles();
                break;

            case IndexType.DoubleIndex:
            case IndexType.CyclicalIndex:
                IDoubleIndex doubleIndex = (IDoubleIndex)index;
                searchDirectory = doubleIndex.GetReadDirectory();
                hasIndexFiles   = doubleIndex.HasIndexFiles();
                break;

            default:
                throw new NotImplementedException(index.IndexStructure.ToString() + " not supported");
            }

            if (!hasIndexFiles)
            {
                return(-1L);
            }

            Lucene29.Net.Store.Directory      directory = null;
            Lucene29.Net.Search.IndexSearcher searcher  = null;
            try {
                directory = Lucene29.Net.Store.FSDirectory.Open(searchDirectory);
                searcher  = new Lucene29.Net.Search.IndexSearcher(directory, true);
                return(searcher.MaxDoc());
            }
            catch {
                return(-2L);
            }
            finally {
                if (searcher != null)
                {
                    searcher.Close();
                }
                if (directory != null)
                {
                    directory.Close();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs a search against an index.
        /// </summary>
        /// <param name="builder">The query to run against the index.</param>
        /// <param name="totalResultsRequested">The total number of results to return.</param>
        /// <returns>a list of <see cref="IndexLibrary.SearchResult"/>, one for each result found.</returns>
        /// <remarks>
        /// Designed to be used for the majority of searches. The functionality contained in this
        /// method is used for a normal search, i.e. enter a term, search, get results.
        /// </remarks>
        public virtual IEnumerable <SearchResult> Search(QueryBuilder builder, int totalResultsRequested)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder", "builder cannot be null");
            }
            List <SearchResult> results = new List <SearchResult>();

            if (OnBeginSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Normal, SearchMethodLocation.Beginning, null)))
            {
                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Normal, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Normal, 0, true));
                return(results);
            }

            this.index.Refresh();
            DirectoryInfo searchDirectory = null;
            bool          hasIndexFiles   = GetIndexReadDirectory(out searchDirectory);

            if ((totalResultsRequested < -1 || totalResultsRequested == 0) || (!hasIndexFiles))
            {
                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Normal, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Normal, 0, true));
                return(results);
            }

            Lucene29.Net.Store.Directory      directory = null;
            Lucene29.Net.Search.IndexSearcher searcher  = null;
            int  hitsLength = 0;
            bool canceled   = false;

            try {
                directory = Lucene29.Net.Store.FSDirectory.Open(searchDirectory);
                searcher  = new Lucene29.Net.Search.IndexSearcher(directory, true);
                Lucene29.Net.Search.TopDocs topDocs = searcher.Search(builder.GetLuceneQuery, (totalResultsRequested == -1) ? StaticValues.DefaultDocsToReturn : totalResultsRequested);

                hitsLength = (totalResultsRequested == -1) ? topDocs.totalHits : Math.Min(topDocs.totalHits, totalResultsRequested);
                if (hitsLength > 5000)
                {
                    hitsLength = 5000;
                }
                int resultsFound = 0;
                for (int i = 0; i < hitsLength; i++)
                {
                    int      docID    = topDocs.scoreDocs[i].doc;
                    Document document = searcher.Doc(docID);
                    System.Collections.IList    fieldList = document.GetFields();
                    Dictionary <string, string> values    = new Dictionary <string, string>();
                    int totalValues = 0;
                    int totalFields = fieldList.Count;
                    for (int j = 0; j < totalFields; j++)
                    {
                        if (fieldList[j] == null)
                        {
                            continue;
                        }
                        Field  field       = fieldList[j] as Field;
                        string name        = field.Name();
                        int    renameCount = 1;
                        while (values.ContainsKey(name))
                        {
                            name = field.Name() + "(" + renameCount.ToString() + ")";
                            renameCount++;
                        }
                        values.Add(name, field.StringValue());
                        ++totalValues;
                    }
                    if (totalValues > 0)
                    {
                        SearchResult result = new SearchResult(values, this.index.IndexDirectory.Name, topDocs.scoreDocs[i].score);
                        results.Add(result);
                        resultsFound++;
                        if (OnSearchResultFound(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Normal, SearchMethodLocation.ResultFound, result)))
                        {
                            canceled = true;
                            break;
                        }
                    }
                }
            }
            finally {
                if (searcher != null)
                {
                    searcher.Close();
                }
                if (directory != null)
                {
                    directory.Close();
                }

                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Normal, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Normal, hitsLength, canceled));
            }

            return(results);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Performs a quick search against an index.
        /// </summary>
        /// <param name="builder">The query to run against the index.</param>
        /// <param name="fieldName">Name of the field to return from the index.</param>
        /// <param name="totalResultsRequested">The total number of results to return.</param>
        /// <returns>A list of strings, one for each result found</returns>
        /// <remarks>
        /// Designed to be used for 'quick' type searches such as those used for type-ahead features or
        /// searches where only a single column needs to be returned.
        /// </remarks>
        public IEnumerable <string> QuickSearch(QueryBuilder builder, string fieldName, int totalResultsRequested)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder", "builder cannot be null");
            }
            List <string> results = new List <string>();

            if (OnBeginSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Quick, SearchMethodLocation.Beginning, null)))
            {
                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Quick, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Quick, 0, true));
                return(results);
            }
            this.index.Refresh();

            DirectoryInfo searchDirectory = null;
            bool          hasIndexFiles   = GetIndexReadDirectory(out searchDirectory);

            if ((totalResultsRequested < -1 || totalResultsRequested == 0) || (!hasIndexFiles))
            {
                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Quick, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Quick, 0, true));
                return(results);
            }

            Lucene29.Net.Store.Directory      directory = null;
            Lucene29.Net.Search.IndexSearcher searcher  = null;
            int  hitsLength = 0;
            bool canceled   = false;

            try {
                directory = Lucene29.Net.Store.FSDirectory.Open(searchDirectory);
                searcher  = new Lucene29.Net.Search.IndexSearcher(directory, true);
                Lucene29.Net.Search.TopDocs topDocs = searcher.Search(builder.GetLuceneQuery, (totalResultsRequested == -1) ? StaticValues.DefaultDocsToReturn : totalResultsRequested);

                hitsLength = (totalResultsRequested == -1) ? topDocs.totalHits : Math.Min(topDocs.totalHits, totalResultsRequested);
                if (hitsLength > 5000)
                {
                    hitsLength = 5000;
                }
                int resultsFound = 0;
                for (int i = 0; i < hitsLength; i++)
                {
                    int      docID    = topDocs.scoreDocs[i].doc;
                    Document document = searcher.Doc(docID);
                    System.Collections.IList fieldList = document.GetFields();
                    //Dictionary<string, string> values = new Dictionary<string, string>();
                    int   totalFields    = fieldList.Count;
                    Field collectedField = null;
                    for (int j = 0; j < totalFields; j++)
                    {
                        if (fieldList[j] == null)
                        {
                            continue;
                        }
                        collectedField = fieldList[j] as Field;
                        if (collectedField.Name().Equals(fieldName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            break;
                        }
                        collectedField = null;
                    }
                    if (collectedField != null)
                    {
                        results.Add(collectedField.StringValue());
                        resultsFound++;
                        if (OnSearchResultFound(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Quick, SearchMethodLocation.ResultFound, collectedField.Name(), collectedField.StringValue(), topDocs.scoreDocs[i].score)))
                        {
                            canceled = true;
                            break;
                        }
                    }
                }
            }
            finally {
                if (searcher != null)
                {
                    searcher.Close();
                }
                if (directory != null)
                {
                    directory.Close();
                }

                OnEndSearch(new SearcherEventArgs(this.index.IndexDirectory.Name, this.index.IndexStructure, SearchMethodType.Quick, SearchMethodLocation.Ending, null));
                LibraryAnalysis.Fire(new SearchInfo(this.index.IndexDirectory.Name, builder.ToString(), SearchMethodType.Quick, hitsLength, canceled));
            }

            return(results);
        }