/// <summary> Optimize the solr/lucene core used for searching within a single document </summary>
        /// <param name="SolrPageUrl"> URL for the solr/lucene core used for searching within a single document for matching pages </param>
        public static void Optimize_Page_Index(string SolrPageUrl)
        {
            // Create the solr worker
            var solrPageWorker = Solr_Operations_Cache <SolrPage> .GetSolrOperations(SolrPageUrl);

            try
            {
                solrPageWorker.Optimize();
            }
            catch (Exception)
            {
                // Do not do anything here.  It may throw an exception when it runs very longs
            }
        }
        /// <summary> Deletes an existing resource from both solr/lucene core indexes </summary>
        /// <param name="SolrDocumentUrl"> URL for the solr/lucene core used for searching for a single document within the library </param>
        /// <param name="SolrPageUrl"> URL for the solr/lucene core used for searching within a single document for matching pages </param>
        /// <param name="BibID"> Bibliographic identifier for the item to remove from the solr/lucene indexes </param>
        /// <param name="VID"> Volume identifier for the item to remove from the solr/lucene indexes </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool Delete_Resource_From_Index(string SolrDocumentUrl, string SolrPageUrl, string BibID, string VID)
        {
            try
            {
                // Create the solr workers
                var solrDocumentWorker = Solr_Operations_Cache <SolrDocument> .GetSolrOperations(SolrDocumentUrl);

                var solrPageWorker = Solr_Operations_Cache <SolrPage> .GetSolrOperations(SolrPageUrl);

                // For the object, we can use the unique identifier
                solrDocumentWorker.Delete(BibID + ":" + VID);

                // For the pages, we need to search by id
                solrPageWorker.Delete(new SolrQuery("did:\"" + BibID + ":" + VID + "\""));

                // Comit the changes to the solr/lucene index
                try
                {
                    solrDocumentWorker.Commit();
                }
                catch
                {
                    Thread.Sleep(10 * 60 * 1000);
                }

                try
                {
                    solrPageWorker.Commit();
                }
                catch
                {
                    Thread.Sleep(10 * 60 * 1000);
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /// <summary> Perform an search for documents with matching parameters </summary>
        /// <param name="AggregationCode"> Aggregation code within which to search </param>
        /// <param name="QueryString"> Quert string for the actual search to perform aggainst the Solr/Lucene engine </param>
        /// <param name="ResultsPerPage"> Number of results to display per a "page" of results </param>
        /// <param name="Page_Number"> Which page of results to return ( one-based, so the first page is page number of one )</param>
        /// <param name="Sort"> Sort to apply before returning the results of the search </param>
        /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering </param>
        /// <param name="Complete_Result_Set_Info"> [OUT] Information about the entire set of results </param>
        /// <param name="Paged_Results"> [OUT] List of search results for the requested page of results </param>
        /// <returns> Page search result object with all relevant result information </returns>
        public static bool Search(string AggregationCode, string QueryString, int ResultsPerPage, int Page_Number, ushort Sort, Custom_Tracer Tracer, out Search_Results_Statistics Complete_Result_Set_Info, out List <iSearch_Title_Result> Paged_Results)
        {
            if (Tracer != null)
            {
                Tracer.Add_Trace("Solr_Documents_Searcher.Search", String.Empty);
            }

            // Set output initially to null
            Paged_Results            = new List <iSearch_Title_Result>();
            Complete_Result_Set_Info = null;

            try
            {
                // Ensure page is not erroneously set to zero or negative
                if (Page_Number <= 0)
                {
                    Page_Number = 1;
                }

                // Create the solr worker to query the document index
                var solrWorker = Solr_Operations_Cache <Solr_Document_Result> .GetSolrOperations(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL);

                // Create the query options
                QueryOptions options = new QueryOptions
                {
                    Rows      = ResultsPerPage,
                    Start     = (Page_Number - 1) * ResultsPerPage,
                    Fields    = new[] { "did", "score", "url", "aleph", "donor", "edition", "format", "holdinglocation", "sourceinstitution", "maintitle", "materialtype", "oclc", "pubdate_display", "author_display", "publisher_display", "mainthumbnail" },
                    Highlight = new HighlightingParameters {
                        Fields = new[] { "fulltext" },
                    },
                    ExtraParams = new Dictionary <string, string> {
                        { "hl.useFastVectorHighlighter", "true" }
                    }
                };

                // Set the sort value
                if (Sort != 0)
                {
                    options.OrderBy.Clear();
                    switch (Sort)
                    {
                    case 1:
                        options.OrderBy.Add(new SortOrder("maintitle_sort"));
                        break;

                    case 2:
                        options.OrderBy.Add(new SortOrder("bibid", Order.ASC));
                        break;

                    case 3:
                        options.OrderBy.Add(new SortOrder("bibid", Order.DESC));
                        break;

                    case 10:
                        options.OrderBy.Add(new SortOrder("pubdate", Order.ASC));
                        break;

                    case 11:
                        options.OrderBy.Add(new SortOrder("pubdate", Order.DESC));
                        break;
                    }
                }

                // If there was an aggregation code included, put that at the beginning of the search
                if ((AggregationCode.Length > 0) && (AggregationCode.ToUpper() != "ALL"))
                {
                    QueryString = "(aggregation_code:" + AggregationCode.ToUpper() + ")AND(" + QueryString + ")";
                }

                // Perform this search
                SolrQueryResults <Solr_Document_Result> results = solrWorker.Query(QueryString, options);

                // Create the search statistcs
                List <string> metadataLabels = new List <string> {
                    "Author", "Publisher", "Format", "Edition", "Institution", "Donor"
                };

                Complete_Result_Set_Info = new Search_Results_Statistics(metadataLabels)
                {
                    Total_Titles = results.NumFound,
                    Total_Items  = results.NumFound,
                    QueryTime    = results.Header.QTime
                };

                // Pass all the results into the List and add the highlighted text to each result as well
                foreach (Solr_Document_Result thisResult in results)
                {
                    // Add the highlight snipper
                    if ((results.Highlights.ContainsKey(thisResult.DID)) && (results.Highlights[thisResult.DID].Count > 0) && (results.Highlights[thisResult.DID].ElementAt(0).Value.Count > 0))
                    {
                        thisResult.Snippet = results.Highlights[thisResult.DID].ElementAt(0).Value.ElementAt(0);
                    }

                    // Add this results
                    Paged_Results.Add(thisResult);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        /// <summary> Indexes a single digital resource within a SobekCM library </summary>
        /// <param name="SolrDocumentUrl"> URL for the solr/lucene core used for searching for a single document within the library </param>
        /// <param name="SolrPageUrl"> URL for the solr/lucene core used for searching within a single document for matching pages </param>
        /// <param name="Resource"> Digital resource to index</param>
        /// <param name="Include_Text"> Flag indicates whether to look for and include full text </param>
        public static void Update_Index(string SolrDocumentUrl, string SolrPageUrl, SobekCM_Item Resource, bool Include_Text)
        {
            // Create the solr workers
            var solrDocumentWorker = Solr_Operations_Cache <SolrDocument> .GetSolrOperations(SolrDocumentUrl);

            var solrPageWorker = Solr_Operations_Cache <SolrPage> .GetSolrOperations(SolrPageUrl);

            // Get the list of all items in this collection
            List <SolrDocument> index_files = new List <SolrDocument>();
            List <SolrPage>     index_pages = new List <SolrPage>();

            // Add this document to the list of documents to index
            index_files.Add(new SolrDocument(Resource, Resource.Source_Directory));

            bool document_success  = false;
            int  document_attempts = 0;

            while (!document_success)
            {
                try
                {
                    solrDocumentWorker.Add(index_files);
                    document_success = true;
                }
                catch (Exception)
                {
                    if (document_attempts > 5)
                    {
                        throw;
                    }
                    document_attempts++;
                    Console.WriteLine(@"ERROR {0}", document_attempts);
                    Thread.Sleep(document_attempts * 1000);
                }
            }

            // Add each page to be indexed
            foreach (SolrDocument document in index_files)
            {
                index_pages.AddRange(document.Solr_Pages);
            }


            bool page_success  = false;
            int  page_attempts = 0;

            while (!page_success)
            {
                try
                {
                    solrPageWorker.Add(index_pages);
                    page_success = true;
                }
                catch (Exception)
                {
                    if (page_attempts > 5)
                    {
                        throw;
                    }
                    page_attempts++;
                    Thread.Sleep(page_attempts * 1000);
                }
            }

            // Comit the changes to the solr/lucene index
            try
            {
                solrDocumentWorker.Commit();
            }
            catch
            {
                Thread.Sleep(10 * 60 * 1000);
            }

            try
            {
                solrPageWorker.Commit();
            }
            catch
            {
                Thread.Sleep(10 * 60 * 1000);
            }
        }
        /// <summary> Perform an in-document search for pages with matching full-text </summary>
        /// <param name="BibID"> Bibliographic identifier (BibID) for the item to search </param>
        /// <param name="VID"> Volume identifier for the item to search </param>
        /// <param name="Search_Terms"> Terms to search for within the page text </param>
        /// <param name="ResultsPerPage"> Number of results to display per a "page" of results </param>
        /// <param name="ResultsPage"> Which page of results to return ( one-based, so the first page is page number of one )</param>
        /// <param name="Sort_By_Score"> Flag indicates whether to sort the results by relevancy score, rather than the default page order </param>
        /// <returns> Page search result object with all relevant result information </returns>
        public static Solr_Page_Results Search(string BibID, string VID, List <string> Search_Terms, int ResultsPerPage, int ResultsPage, bool Sort_By_Score)
        {
            // Ensure page is not erroneously set to zero or negative
            if (ResultsPage <= 0)
            {
                ResultsPage = 1;
            }

            // Create the solr worker to query the page index
            var solrWorker = Solr_Operations_Cache <Solr_Page_Result> .GetSolrOperations(Engine_ApplicationCache_Gateway.Settings.Servers.Page_Solr_Index_URL);

            // Create the query options
            QueryOptions options = new QueryOptions
            {
                Rows      = ResultsPerPage,
                Start     = (ResultsPage - 1) * ResultsPerPage,
                Fields    = new [] { "pageid", "pagename", "pageorder", "score", "thumbnail" },
                Highlight = new HighlightingParameters {
                    Fields = new[] { "pagetext" },
                },
                ExtraParams = new Dictionary <string, string> {
                    { "hl.useFastVectorHighlighter", "true" }
                }
            };

            // If this is not the default Solr sort (by score) request sort by the page order
            if (!Sort_By_Score)
            {
                options.OrderBy = new[] { new SortOrder("pageorder", Order.ASC) }
            }
            ;

            // Build the query string
            StringBuilder queryStringBuilder = new StringBuilder("(bibid:" + BibID + ")AND(vid:" + VID + ")AND(");
            bool          first_value        = true;

            foreach (string searchTerm in Search_Terms)
            {
                if (searchTerm.Length > 1)
                {
                    // Skip any AND NOT for now
                    if (searchTerm[0] != '-')
                    {
                        // Find the joiner
                        if (first_value)
                        {
                            if (searchTerm.IndexOf(" ") > 0)
                            {
                                if ((searchTerm[0] == '+') || (searchTerm[0] == '=') || (searchTerm[0] == '-'))
                                {
                                    queryStringBuilder.Append("(pagetext:\"" + searchTerm.Substring(1).Replace(":", "") + "\")");
                                }
                                else
                                {
                                    queryStringBuilder.Append("(pagetext:\"" + searchTerm.Replace(":", "") + "\")");
                                }
                            }
                            else
                            {
                                if ((searchTerm[0] == '+') || (searchTerm[0] == '=') || (searchTerm[0] == '-'))
                                {
                                    queryStringBuilder.Append("(pagetext:" + searchTerm.Substring(1).Replace(":", "") + ")");
                                }
                                else
                                {
                                    queryStringBuilder.Append("(pagetext:" + searchTerm.Replace(":", "") + ")");
                                }
                            }
                            first_value = false;
                        }
                        else
                        {
                            if ((searchTerm[0] == '+') || (searchTerm[0] == '=') || (searchTerm[0] == '-'))
                            {
                                queryStringBuilder.Append(searchTerm[0] == '=' ? " OR " : " AND ");

                                if (searchTerm.IndexOf(" ") > 0)
                                {
                                    queryStringBuilder.Append("(pagetext:\"" + searchTerm.Substring(1).Replace(":", "") + "\")");
                                }
                                else
                                {
                                    queryStringBuilder.Append("(pagetext:" + searchTerm.Substring(1).Replace(":", "") + ")");
                                }
                            }
                            else
                            {
                                if (searchTerm.IndexOf(" ") > 0)
                                {
                                    queryStringBuilder.Append(" AND (pagetext:\"" + searchTerm.Replace(":", "") + "\")");
                                }
                                else
                                {
                                    queryStringBuilder.Append(" AND (pagetext:" + searchTerm.Replace(":", "") + ")");
                                }
                            }
                        }
                    }
                }
            }
            queryStringBuilder.Append(")");


            // Perform this search
            SolrQueryResults <Solr_Page_Result> results = solrWorker.Query(queryStringBuilder.ToString(), options);

            // Create the results object to pass back out
            var searchResults = new Solr_Page_Results
            {
                QueryTime     = results.Header.QTime,
                TotalResults  = results.NumFound,
                Query         = queryStringBuilder.ToString(),
                Sort_By_Score = Sort_By_Score,
                Page_Number   = ResultsPage
            };

            // Pass all the results into the List and add the highlighted text to each result as well
            foreach (Solr_Page_Result thisResult in results)
            {
                // Add the highlight snipper
                if ((results.Highlights.ContainsKey(thisResult.PageID)) && (results.Highlights[thisResult.PageID].Count > 0) && (results.Highlights[thisResult.PageID].ElementAt(0).Value.Count > 0))
                {
                    thisResult.Snippet = results.Highlights[thisResult.PageID].ElementAt(0).Value.ElementAt(0);
                }

                // Add this results
                searchResults.Add_Result(thisResult);
            }

            return(searchResults);
        }
    }