/// <summary> /// Parse highlighting snippets for each field. /// </summary> /// <param name="nodes"></param> /// <returns></returns> public HighlightedSnippets ParseHighlightingFields(IEnumerable <XElement> nodes) { var fields = new HighlightedSnippets(); foreach (var field in nodes) { var fieldName = field.Attribute("name").Value; var snippets = new List <string>(); foreach (var str in field.Elements("str")) { snippets.Add(str.Value); } fields.Add(fieldName, snippets); } return(fields); }
internal void SetBody(QueryResponse queryResponse, SolrQueryResults <Movie> solrResults) { queryResponse.Results = (List <Movie>)solrResults; foreach (Movie movie in queryResponse.Results) { if (solrResults.Highlights.ContainsKey(movie.MovieId)) { HighlightedSnippets snippets = solrResults.Highlights[movie.MovieId]; if (snippets.ContainsKey("title")) { movie.Title = snippets["title"].FirstOrDefault(); } } } }
internal void SetBody(QueryResponse queryResponse, SolrQueryResults <EHSDoc> solrResults) { queryResponse.Results = solrResults; foreach (EHSDoc doc in queryResponse.Results) { if (solrResults.Highlights.ContainsKey(doc.Id)) { HighlightedSnippets snippets = solrResults.Highlights[doc.Id]; if (snippets.ContainsKey("answers")) { doc.ValueAnswers = snippets["answers"].FirstOrDefault(); } } } }
/// <summary> /// Parse highlighting snippets for each field. /// </summary> /// <param name="nodes"></param> /// <returns></returns> public static HighlightedSnippets ParseHighlightingFields(IEnumerable <XElement> nodes) { var fields = new HighlightedSnippets(); foreach (var field in nodes) { var fieldName = field.Attribute("name").Value; ICollection <string> snippets = field.Elements("str") .Select(str => str.Value) .ToList(); if (snippets.Count == 0 && !string.IsNullOrEmpty(field.Value)) { snippets = new[] { field.Value } } ; fields.Add(fieldName, snippets); } return(fields); } }
public void SetBody(QueryResponse queryResponse, SolrQueryResults <Course> solrResult) { queryResponse.Result = solrResult as List <Course>; foreach (var course in queryResponse.Result) { if (solrResult.Highlights.ContainsKey(course.CourseId)) { HighlightedSnippets snippets = solrResult.Highlights[course.CourseId]; if (snippets.ContainsKey("coursetitle")) { course.CourseTitle = snippets["coursetitle"].FirstOrDefault(); } if (snippets.ContainsKey("description")) { course.Description = snippets["description"].FirstOrDefault(); } } } }
public static List <SolrDoc> search(Guid applicationId, string phrase, List <SearchDocType> docTypes, List <Guid> typeIds, List <string> types, bool additionalId, bool title, bool description, bool tags, bool content, bool fileContent, bool forceHasContent, int count, int lowerBoundary, bool highlight, ref int totalCount) { ISolrOperations <SolrDoc> solr = get_solr_operator(); QueryTerms searchTerms = new QueryTerms(phrase); List <KeyValuePair <string, double> > fieldBoosts = new List <KeyValuePair <string, double> >(); docTypes = (docTypes == null ? new List <SearchDocType>() : docTypes.Where(d => d != SearchDocType.All)).Distinct().ToList(); if (title) { fieldBoosts.Add(new KeyValuePair <string, double>("title", 5)); } if (tags) { fieldBoosts.Add(new KeyValuePair <string, double>("tags", 4)); } if (description) { fieldBoosts.Add(new KeyValuePair <string, double>("description", 3)); } if (content) { fieldBoosts.Add(new KeyValuePair <string, double>("content", 2)); } if (fileContent) { fieldBoosts.Add(new KeyValuePair <string, double>("file_content", 1)); } if (additionalId) { fieldBoosts.Add(new KeyValuePair <string, double>("additional_id", 0)); } string query = searchTerms.get_query(fieldBoosts, docTypes, typeIds, types, forceHasContent); QueryOptions queryOptions = new QueryOptions() { Rows = count + (count / 2), StartOrCursor = new StartOrCursor.Start(Math.Max(0, lowerBoundary)), ExtraParams = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("_route_", applicationId.ToString() + "!") }, Fields = new[] { "id", "search_doc_type", "type_id", "type", "additional_id", "title", "no_content", "deleted" } }; if (highlight) { queryOptions.Highlight = new HighlightingParameters() { Fields = fieldBoosts.Where(b => b.Key != "additional_id").Select(b => b.Key).ToArray() } } ; SolrQueryResults <SolrDoc> results = solr.Query(query, queryOptions); totalCount = results.NumFound; if (highlight) { results.Where(d => results.Highlights.ContainsKey(d.ID)).ToList().ForEach(doc => { HighlightedSnippets snippets = results.Highlights[doc.ID]; doc.Description = string.Join(" ", snippets.Values.Select(v => string.Join(" ", v))) .Replace("<em>", "<b>").Replace("</em>", "</b>"); }); } return(results.ToList()); }