public v5_Solr_Title_Result Map_To_Result(v5_SolrDocument thisResult, List <Complete_Item_Aggregation_Metadata_Type> DisplayFields)
        {
            // Create the results
            v5_Solr_Title_Result resultConverted = new v5_Solr_Title_Result();

            resultConverted.MaterialType = thisResult.Type;

            // Get the bibid
            resultConverted.BibID = thisResult.DID.Substring(0, 10);

            // For now...
            resultConverted.GroupThumbnail = thisResult.MainThumbnail;
            resultConverted.GroupTitle     = thisResult.Title ?? "NO TITLE";

            // These should not really be necessary
            resultConverted.Primary_Identifier      = String.Empty;
            resultConverted.Primary_Identifier_Type = String.Empty;
            resultConverted.Snippet = String.Empty;

            // Add the item
            v5_Solr_Item_Result itemResult = new v5_Solr_Item_Result();

            itemResult.VID           = thisResult.DID.Substring(11, 5);
            itemResult.Title         = thisResult.Title ?? "NO TITLE";
            itemResult.MainThumbnail = thisResult.MainThumbnail;
            resultConverted.Items.Add(itemResult);

            // Build the display results values
            List <string> display_result_fields = new List <string>();

            foreach (Complete_Item_Aggregation_Metadata_Type metadataField in DisplayFields)
            {
                display_result_fields.Add(data_from_display_field(thisResult, metadataField.SolrCode) ?? String.Empty);
            }

            resultConverted.Metadata_Display_Values = display_result_fields.ToArray();

            return(resultConverted);
        }
        public v5_Solr_Title_Result Map_To_Result(SolrNet.Group <v5_SolrDocument> Grouping, List <Complete_Item_Aggregation_Metadata_Type> DisplayFields)
        {
            // Create the results
            v5_Solr_Title_Result resultConverted = new v5_Solr_Title_Result();

            // These should not really be necessary
            resultConverted.Primary_Identifier      = String.Empty;
            resultConverted.Primary_Identifier_Type = String.Empty;
            resultConverted.Snippet = String.Empty;

            // Now add all the item info
            bool first_item = true;

            foreach (v5_SolrDocument item in Grouping.Documents)
            {
                if (first_item)
                {
                    resultConverted.MaterialType = item.Type;

                    // Get the bibid
                    resultConverted.BibID = item.DID.Substring(0, 10);

                    // Look for information about this bibid first
                    Multiple_Volume_Item titleInfo = Engine_ApplicationCache_Gateway.Title_List.Get_Title(resultConverted.BibID);
                    if (titleInfo == null)
                    {
                        resultConverted.GroupThumbnail = item.MainThumbnail;
                        resultConverted.GroupTitle     = item.Title ?? "NO TITLE";
                    }
                    else
                    {
                        if ((titleInfo.GroupThumbnailType == Group_Thumbnail_Enum.Custom_Thumbnail) && (!String.IsNullOrEmpty(titleInfo.CustomThumbnail)))
                        {
                            resultConverted.GroupThumbnail = titleInfo.CustomThumbnail;
                        }
                        else
                        {
                            resultConverted.GroupThumbnail = item.MainThumbnail;
                        }
                        resultConverted.GroupTitle = titleInfo.GroupTitle;
                    }

                    // Build the display results values
                    List <string> display_result_fields = new List <string>();
                    foreach (Complete_Item_Aggregation_Metadata_Type metadataField in DisplayFields)
                    {
                        display_result_fields.Add(data_from_display_field(item, metadataField.SolrCode) ?? String.Empty);
                    }
                    resultConverted.Metadata_Display_Values = display_result_fields.ToArray();

                    // Done with this first item
                    first_item = false;
                }

                // Add the item
                v5_Solr_Item_Result itemResult = new v5_Solr_Item_Result();
                itemResult.VID           = item.DID.Substring(11, 5);
                itemResult.Title         = item.Title ?? "NO TITLE";
                itemResult.MainThumbnail = item.MainThumbnail;
                resultConverted.Items.Add(itemResult);
            }

            return(resultConverted);
        }
Пример #3
0
        /// <summary> Run a solr query against the solr document index </summary>
        /// <param name="QueryString"> Solr query string </param>
        /// <param name="SearchOptions"> Options related to this search, like the page, results per page, facets, fields, etc.. </param>
        /// <param name="UserMembership"> User-specific membership information, related to a search, which can be used to determine which items this user can discover</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 Run_Query(string QueryString, Search_Options_Info SearchOptions, Search_User_Membership_Info UserMembership, Custom_Tracer Tracer, out Search_Results_Statistics Complete_Result_Set_Info, out List <iSearch_Title_Result> Paged_Results)
        {
            // If the query string is empty, then set it back to *:*
            if (QueryString.Trim().Length == 0)
            {
                QueryString = "*:*";
            }

            // 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
                int pageNumber = SearchOptions.Page;
                if (pageNumber <= 0)
                {
                    pageNumber = 1;
                }

                // Get and clean the solr document url
                string solrDocumentUrl = Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL;
                if ((!String.IsNullOrEmpty(solrDocumentUrl)) && (solrDocumentUrl[solrDocumentUrl.Length - 1] == '/'))
                {
                    solrDocumentUrl = solrDocumentUrl.Substring(0, solrDocumentUrl.Length - 1);
                }

                // Create the solr worker to query the document index
                var solrWorker = Solr_Operations_Cache <v5_SolrDocument> .GetSolrOperations(solrDocumentUrl);

                // Get the list of fields
                List <string> fields = new List <string> {
                    "did", "mainthumb", "title"
                };
                fields.AddRange(SearchOptions.Fields.Select(MetadataField => MetadataField.SolrCode));

                // Create the query options
                QueryOptions options = new QueryOptions
                {
                    Rows   = SearchOptions.ResultsPerPage,
                    Start  = (pageNumber - 1) * SearchOptions.ResultsPerPage,
                    Fields = fields
                };

                // Was there full text search in that?
                if ((QueryString.Contains("(fulltext:")) && (SearchOptions.IncludeFullTextSnippets))
                {
                    options.Highlight = new HighlightingParameters {
                        Fields = new[] { "fulltext" }, Fragsize = 255
                    };
                    options.ExtraParams = new Dictionary <string, string> {
                        { "hl.useFastVectorHighlighter", "true" }, { "wt", "xml" }
                    };
                }
                else
                {
                    // We still need to instruct SOLR to return the results as XML for solr to parse it
                    options.ExtraParams = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("wt", "xml") };
                }

                // If the search stats are needed, let's get the facets
                if ((SearchOptions.Facets != null) && (SearchOptions.Facets.Count > 0))
                {
                    // Create the query facters
                    options.Facet = new FacetParameters();
                    foreach (Complete_Item_Aggregation_Metadata_Type facet in SearchOptions.Facets)
                    {
                        options.Facet.Queries.Add(new SolrFacetFieldQuery(facet.SolrCode)
                        {
                            MinCount = 1, Limit = 100
                        });
                    }
                }

                // Set the sort value
                if (SearchOptions.Sort != 0)
                {
                    options.OrderBy.Clear();
                    switch (SearchOptions.Sort)
                    {
                    case 1:
                        options.OrderBy.Add(new SortOrder("title.sort", Order.ASC));
                        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("date.gregorian", Order.ASC));
                        break;

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

                    case 12:
                        options.OrderBy.Add(new SortOrder("timeline_date", Order.ASC));

                        // If sorting by this, only get records with timeline date
                        QueryString = "(" + QueryString + ") AND timeline_date:[* TO *]";
                        break;
                    }
                }

                // Should this be grouped?
                bool grouped_results = false;
                if ((SearchOptions.GroupItemsByTitle) && (SearchOptions.Sort < 10) && (QueryString.IndexOf("fulltext") < 0))
                {
                    if (Tracer != null)
                    {
                        Tracer.Add_Trace("v5_Solr_Documents_Searcher.Run_Query", "Grouping search request by bibid");
                    }

                    grouped_results = true;

                    GroupingParameters groupingParams = new GroupingParameters
                    {
                        Fields = new[] { "bibid" },

                        Format = GroupingFormat.Grouped,

                        Limit = 10,

                        Ngroups = true
                    };

                    options.Grouping = groupingParams;
                }

                // Log the search term
                if (Tracer != null)
                {
                    Tracer.Add_Trace("v5_Solr_Documents_Searcher.Run_Query", "Solr Query: " + QueryString);
                }

                if (Tracer != null)
                {
                    Tracer.Add_Trace("v5_Solr_Documents_Searcher.Run_Query", "Perform the search");
                }

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


                if (Tracer != null)
                {
                    Tracer.Add_Trace("v5_Solr_Documents_Searcher.Run_Query", "Build the results object");
                }

                // Create the search statistcs (this part assumes no grouping, and then we fix the count shortly)
                List <string> metadataLabels = SearchOptions.Fields.Select(MetadataType => MetadataType.DisplayTerm).ToList();
                Complete_Result_Set_Info = new Search_Results_Statistics(metadataLabels)
                {
                    Total_Titles = results.NumFound,
                    Total_Items  = results.NumFound,
                    QueryTime    = results.Header.QTime
                };

                // If the search stats were needed, get the facets out
                if ((SearchOptions.Facets != null) && (SearchOptions.Facets.Count > 0))
                {
                    // Copy over all the facets
                    foreach (Complete_Item_Aggregation_Metadata_Type facetTerm in SearchOptions.Facets)
                    {
                        // Create the collection and and assifn the metadata type id
                        Search_Facet_Collection thisCollection = new Search_Facet_Collection(facetTerm.ID);

                        // Add each value
                        foreach (var facet in results.FacetFields[facetTerm.SolrCode])
                        {
                            thisCollection.Facets.Add(new Search_Facet(facet.Key, facet.Value));
                        }

                        // If there was an id and facets added, save this to the search statistics
                        if ((thisCollection.MetadataTypeID > 0) && (thisCollection.Facets.Count > 0))
                        {
                            Complete_Result_Set_Info.Facet_Collections.Add(thisCollection);
                        }
                    }
                }

                // Build the results mapper object
                v5_SolrDocument_Results_Mapper mapper = new v5_SolrDocument_Results_Mapper();

                // Build the results differently, depending on whether they were grouped or not
                if (grouped_results)
                {
                    // Get the grouped results (only grouped by bibid)
                    GroupedResults <v5_SolrDocument> title_groupings = results.Grouping["bibid"];

                    // Now step through each group (i.e., titles/bibs) in the groups
                    foreach (Group <v5_SolrDocument> grouping in title_groupings.Groups)
                    {
                        // Convert the grouping to the new result
                        v5_Solr_Title_Result newResult = mapper.Map_To_Result(grouping, SearchOptions.Fields);

                        Paged_Results.Add(newResult);
                    }

                    // Now, fix the stats as well
                    Complete_Result_Set_Info.Total_Items  = title_groupings.Matches;
                    Complete_Result_Set_Info.Total_Titles = title_groupings.Ngroups.Value;
                }
                else
                {
                    // Pass all the results into the List and add the highlighted text to each result as well
                    foreach (v5_SolrDocument thisResult in results)
                    {
                        // Convert to the new result
                        v5_Solr_Title_Result newResult = mapper.Map_To_Result(thisResult, SearchOptions.Fields);

                        // Add the highlight snippet, if applicable
                        if ((results.Highlights != null) && (results.Highlights.ContainsKey(thisResult.DID)) && (results.Highlights[thisResult.DID].Count > 0) && (results.Highlights[thisResult.DID].ElementAt(0).Value.Count > 0))
                        {
                            newResult.Snippet = results.Highlights[thisResult.DID].ElementAt(0).Value.ElementAt(0);
                        }

                        Paged_Results.Add(newResult);
                    }
                }

                return(true);
            }
            catch (Exception ee)
            {
                return(false);
            }
        }