private IDocument GetDocumentWithAdditionalFields(Hit hit)
        {
            List<IField> listOfFields = new List<IField>();

            var summaryField = new Field();
            summaryField.Name = PublishingConstants.FieldSummary;
            summaryField.Value = string.Empty;
            listOfFields.Add(summaryField);

            var highlight = GetHighlights(hit);
            listOfFields.Add(new Field() { Name = PublishingConstants.HighLighterResult, Value = highlight });
            listOfFields.Add(new Field() { Name = PublishingConstants.SuggestionsField, Value = this.suggestions });

            foreach (var field in hit.Fields)
            {
                if (field.Key != null)
                {
                    var currentField = new Field();
                    currentField.Name = field.Key;
                    currentField.Value = field.Value.FirstOrDefault();
                    listOfFields.Add(currentField);
                }
            }

            Document doc = new Document(listOfFields, null);
            return doc;
        }
        private static string GetHighlights(Hit hit)
        {
            var text = string.Empty;
            foreach (var field in hit.Highlights.Keys)
            {
                if (!string.IsNullOrEmpty(text))
                    text += SearchResultsSeparator;

                var value = hit.Highlights[field];
                text += value;
            }

            return text;
        }
示例#3
0
        private SI4T.Query.Models.SearchResult CreateSearchResult(Hit hit)
        {
            SI4T.Query.Models.SearchResult result = new SI4T.Query.Models.SearchResult {Id = hit.Id};

            foreach (KeyValuePair<string, List<string>> field in hit.Fields)
            {
                string type = field.Value.GetType().ToString();
                string fieldname = field.Key;
                
                switch (fieldname)
                {
                    case "publicationid":
                        result.PublicationId = Int32.Parse(field.Value.FirstOrDefault());
                        break;
                    case "title":
                        result.Title = field.Value.FirstOrDefault();
                        break;
                    case "url":
                        result.Url = field.Value.FirstOrDefault();
                        break;
                    case "summary":
                        result.Summary = field.Value.FirstOrDefault();
                        break;
                    default:
                        object data = null;
                        switch (type)
                        {
                            case "arr": //TODO: Make smarter
                                data = field.Value.ToList();
                                break;
                            default:
                                data = field.Value.FirstOrDefault();
                                break;
                        }
                        result.CustomFields.Add(fieldname, data);
                        break;
                }
            }

            if (String.IsNullOrEmpty(result.Summary) && hit.Highlights.ContainsKey("body"))
            {
                // If no summary field is present in the index, use the highlight fragment from the body field instead.
                string autoSummary = hit.Highlights["body"];
                if (autoSummary.Length > AutoSummarySize)
                {
                    // CloudSearch returns up to 10K of content and there doesn't seem to be a way to limit the size of the fragment in the Search Request.
                    // Therefore we truncate it here if needed.
                    autoSummary = autoSummary.Substring(0, AutoSummarySize) + "...";
                }
                result.Summary = autoSummary;
            }

            return result;
        }