Exemple #1
0
        private List <ProductSearchResultItem> GetGroupItemList(SolrQueryResults <ProductSearchRecord> solrQueryResult, out int totalGroupItemCount)
        {
            List <ProductSearchResultItem>       productList = new List <ProductSearchResultItem>();
            GroupedResults <ProductSearchRecord> grouInfo    = null;

            if (solrQueryResult.Grouping.Keys.Contains("p_productgroupvalue"))
            {
                grouInfo = solrQueryResult.Grouping["p_productgroupvalue"];
            }
            else if (solrQueryResult.Grouping.Keys.Contains("p_sysno"))
            {
                grouInfo = solrQueryResult.Grouping["p_sysno"];
            }

            if (grouInfo == null)
            {
                totalGroupItemCount = 0;
                return(productList);
            }

            totalGroupItemCount = grouInfo.Ngroups.GetValueOrDefault();
            foreach (Group <ProductSearchRecord> group in grouInfo.Groups)
            {
                productList.Add(ReadProductFromSolrDocument(((IList <ProductSearchRecord>)group.Documents)[0]));
            }

            return(productList);
        }
Exemple #2
0
 private void UpdateResultGroups(DateTime[] timestamps, ParseResultUnit newResult)
 {
     lock (GroupedResultsLock)
     {
         foreach (var timestamp in timestamps)
         {
             var timestampGroup = timestamp.AddSeconds(-timestamp.Second);
             if (!GroupedResults.ContainsKey(timestampGroup))
             {
                 GroupedResults.Add(timestampGroup, new HashSet <ParseResultUnit>());
                 GroupedResults[timestampGroup].Add(newResult);
             }
             else
             {
                 if (!GroupedResults[timestampGroup].Contains(newResult))
                 {
                     GroupedResults[timestampGroup].Add(newResult);
                 }
             }
         }
     }
 }
Exemple #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);
            }
        }